<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="projectId">
        <el-select v-model="queryType" style="width: 100px;">
          <el-option label="项目编号" value="1"></el-option>
          <el-option label="项目名称" value="2"></el-option>
        </el-select>
        <el-select v-model="queryParams.projectId" clearable filterable remote reserve-keyword placeholder="请输入关键字"
          :remote-method="remoteMethod" :loading="loading" style="width: 300px;">
          <el-option v-for="project in projectList" :key="project.projectId"
            :label="project.projectNumber + '-' + project.projectName" :value="project.projectId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="核算时间" prop="checkTime">
        <el-date-picker clearable v-model="queryParams.checkTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择核算时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['oa:check:export']">全部导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="项目编号" align="center" prop="project.projectNumber" />
      <el-table-column label="项目名称" align="center" prop="project.projectName" />
      <el-table-column label="核算人" align="center" prop="checkUser.nickName" />
      <el-table-column label="核算时间" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核算说明" align="center" prop="checkComment" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['oa:check:query']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-download" @click="downloadInfo(scope.row)"
          v-hasPermi="['oa:check:query']">导出</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改cmc项目核算对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <budget-adjust :taskForm="taskForm" :taskName="''"></budget-adjust>
    </el-dialog>
  </div>
</template>

<script>
import { listCheck, getCheck, delCheck, addCheck, updateCheck } from "@/api/oa/budget/check";
import { listProject } from '@/api/oa/project/project';
import budgetAdjust from '@/views/flowable/form/budget/adjust/budgetAdjust.vue';

export default {
  name: "Check",
  components: { budgetAdjust },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc项目核算表格数据
      checkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        budgetId: null,
        projectId: null,
        checker: null,
        checkTime: null,
        checkComment: null,
        cwUserId: null,
        cwTime: null,
        cwComment: null,
        managerUserId: null,
        managerTime: null,
        managerComment: null,
        zjlUserId: null,
        zjlTime: null,
        zjlComment: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      taskForm: {
        formId: '',
        budgetId:''
      },
      queryType: '1',
      projectList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc项目核算列表 */
    getList() {
      this.loading = true;
      listCheck(this.queryParams).then(response => {
        this.checkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkId: null,
        budgetId: null,
        projectId: null,
        checker: null,
        checkTime: null,
        checkComment: null,
        cwUserId: null,
        cwTime: null,
        cwComment: null,
        managerUserId: null,
        managerTime: null,
        managerComment: null,
        zjlUserId: null,
        zjlTime: null,
        zjlComment: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    remoteMethod(val) {
      let params1 = {
        pageNum: 1,
        pageSize: 20,
        projectNumber: val
      }
      let params2 = {
        pageNum: 1,
        pageSize: 20,
        projectName: val
      }
      let params = {};
      if (this.queryType == '1') {
        params = params1
      } else {
        params = params2
      }
      listProject(params).then(res => {
        this.projectList = res.rows;
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.open = true;
      this.title = "查看cmc项目核算";
      this.form = row;
      this.$set(this.taskForm, 'formId', row.checkId);
      this.$set(this.taskForm, 'budgetId', row.budgetId);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加cmc项目核算";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const checkId = row.checkId || this.ids
      getCheck(checkId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc项目核算";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.checkId != null) {
            updateCheck(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheck(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkIds = row.checkId || this.ids;
      this.$modal.confirm('是否确认删除cmc项目核算编号为"' + checkIds + '"的数据项？').then(function () {
        return delCheck(checkIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    downloadInfo(row){
      this.download('oa/budget/export', {
        'budgetId': row.budgetId
      }, `budget_${row.project.projectNumber}_${row.project.projectName}_${new Date().getTime()}.xlsx`);
      this.download('oa/budgetCar/export', {
        'budgetId': row.budgetId
      }, `budget_Car_${row.project.projectNumber}_${row.project.projectName}_${new Date().getTime()}.xlsx`);
      this.download('oa/budgetDevice/export', {
        'budgetId': row.budgetId
      }, `budget_Device_${row.project.projectNumber}_${row.project.projectName}_${new Date().getTime()}.xlsx`);
      this.download('oa/budgetSettle/export', {
        'budgetId': row.budgetId
      }, `budget_Settle_${row.project.projectNumber}_${row.project.projectName}_${new Date().getTime()}.xlsx`);
      this.download('oa/budgetStaff/export', {
        'budgetId': row.budgetId
      }, `budget_Staff_${row.project.projectNumber}_${row.project.projectName}_${new Date().getTime()}.xlsx`);
      this.download('oa/budgetOther/export', {
        'budgetId': row.budgetId
      }, `budget_Other_${row.project.projectNumber}_${row.project.projectName}_${new Date().getTime()}.xlsx`);
      this.download('oa/budgetSite/export', {
        'budgetId': row.budgetId
      }, `budget_Site_${row.project.projectNumber}_${row.project.projectName}_${new Date().getTime()}.xlsx`);
      this.download('oa/projectWork/export', {
        'projectId': row.projectId
      }, `project_Work_${row.project.projectNumber}_${row.project.projectName}_${new Date().getTime()}.xlsx`)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/check/export', {
        ...this.queryParams
      }, `check_${new Date().getTime()}.xlsx`);
      this.download('oa/budget/export', {
        ...this.queryParams
      }, `budget_${new Date().getTime()}.xlsx`);
      this.download('oa/budgetCar/export', {
        ...this.queryParams
      }, `budget_Car_${new Date().getTime()}.xlsx`);
      this.download('oa/budgetDevice/export', {
        ...this.queryParams
      }, `budget_Device_${new Date().getTime()}.xlsx`);
      this.download('oa/budgetSettle/export', {
        ...this.queryParams
      }, `budget_Settle_${new Date().getTime()}.xlsx`);
      this.download('oa/budgetStaff/export', {
        ...this.queryParams
      }, `budget_Staff${new Date().getTime()}.xlsx`);
      this.download('oa/budgetOther/export', {
        ...this.queryParams
      }, `budget_Other${new Date().getTime()}.xlsx`);
      this.download('oa/budgetSite/export', {
        ...this.queryParams
      }, `budget_Site${new Date().getTime()}.xlsx`);
      this.download('oa/projectWork/export', {
        ...this.queryParams
      }, `project_Work${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
