<template>
  <div>
    <div style="margin-top: 6px;">
      <el-form :inline="true">
        <el-form-item label="研发项目名称">
          <project-select v-model="params.xmId" @change="handleProjectChange"></project-select>
        </el-form-item>
        <el-form-item label="研发项目编号">
          {{ selProject.xmbh }}
        </el-form-item>
      </el-form>
    </div>
    <avue-crud
      ref="crud"
      v-bind="bindVal"
      v-on="onEvent"
      v-model="form"
      :page.sync="page"
      :before-open="handleBeforeOpen"
    >
      <template slot="menuLeft">
        <el-button
          v-if="!isSelAnnual"
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          @click="handleDelete"
        >
          删 除
        </el-button>
        <el-button
          v-if="!isSelAnnual"
          type="success"
          size="small"
          plain
          icon="el-icon-upload2"
          @click="handleImport"
        >
          导入
        </el-button>
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          @click="handleExport"
        >
          导出
        </el-button>
        <print-table-btn @click="printTable" />
      </template>

      <template slot="technicianIdForm">
        <project-person-select v-model="form.technicianId" valueKey="id" :startDate="projectStartMonth" />
      </template>
      
    </avue-crud>

    <upload-excel-dialog ref="uploadExcelDialog" templateKey="预算-人员人工费用" templateName="预算-人员人工费用-导入模板" :uploadAfter="uploadAfter"/>

    <WideTablePrinter
      ref="printWideTable"
      :columns="wideTableColumns"
      :data="data"
      :print-title="printTitle"
      :rows-per-page="30"
      :default-landscape="true"
    />
  </div>
</template>

<script>
import {exportBloByPost} from "@/api/common";
import UploadExcelDialog from "@/components/upload-excel-dialog";
import projectSelect from "@/components/project-select";
import projectPersonSelect from "@/components/project-person-select";
import {getToken} from "@/util/auth";
import {downloadXls} from "@/util/util";
import Decimal from "decimal.js";
import moment from "moment";


export default window.$crudCommon({
  props: {
    projectId: [String, Number]
  },
  components: {
    projectSelect,
    projectPersonSelect,
    UploadExcelDialog,
  },
  data() {
    return {
      params: {
        xmId: ''
      },
      selProject: {},

      wideTableColumns: [],
      printTitle: "",

      projectStartMonth: ""
    };
  },
  created() {
    this.params.xmId = this.projectId;
    this.option.height = window.innerHeight - 340;
  },
  watch: {
    "params.xmId"() {
      this.page.currentPage = 1;
      this.getList(this.page);
    }
  },
  methods: {
    handleBeforeOpen(done, type) {
      if (!this.params.xmId) {
        this.$message.warning("请选择项目!");
        return;
      }

      done();
    },
    getList() {
      if (!this.params.xmId) {
        return;
      }
      this.loadData();
    },
    getFormData() {
      return { ...this.form, xmId: this.params.xmId }
    },
    handleProjectChange(data) {
      this.selProject = data;

      this.projectStartMonth = moment(data.xmkssj).format("yyyyMM");
    },
    handleImport() {
      if (!this.params.xmId) {
        this.$message.warning("请选择项目!");
        return;
      }
      let excelParams = {xmId: this.params.xmId };
      this.$refs.uploadExcelDialog.open('/api/kd-scientific/ys-ryrgfy/import', excelParams);
    },
    uploadAfter() {
      this.page.currentPage = 1;
      this.getList(this.page);
    },
    handleExport() {
      exportBloByPost(`/api/kd-scientific/ys-ryrgfy/export?${this.website.tokenHeader}=${getToken()}`, this.params).then(res => {
        downloadXls(res.data, `${this.selProject.xmmc}人员人工费用.xlsx`);
      });
    },
    /**
     * 打印表格
     * @param isLandscape 是否横向打印
     */
    printTable(isLandscape) {
      this.wideTableColumns = this.$refs.crud.columnOption;
      this.printTitle = `${this.selProject.xmmc}人员人工费用`;
      this.$nextTick(() => {
        this.$refs.printWideTable.printTable(isLandscape);
      })
    },
  },
}, {
  // 模块路径
  name: 'projectManage/personCost',
  res: ({ data }) => {
    data.records = data.records.map(item => {
      let zzsc = new Decimal(item.zzsc || 0);
      let pjrgfy = new Decimal(item.pjrgfy || 0);

      item.technicianId = item.technicianId.toString();
      item.name = item.technician.name;
      item.number = item.technician.number;
      item.idCard = item.technician.idCard;
      item.pjrgfy = (item.pjrgfy || 0).toFixed(2);
      item.total = zzsc.mul(pjrgfy).toFixed(2);
      item.isWp = item.nature === '在职' ? '是' : '否';
      return item;
    });

    return data;
  },
});
</script>
