<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>
      
    </avue-crud>

    <upload-excel-dialog ref="uploadExcelDialog" :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 {getToken} from "@/util/auth";
import {downloadXls} from "@/util/util";


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

      wideTableColumns: [],
      printTitle: "",
    };
  },
  created() {
    this.params.xmId = this.projectId;
    this.option.height = window.innerHeight - 340;
  },
  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.page.currentPage = 1;
      this.getList(this.page);
    },
    handleImport() {
      this.$refs.uploadExcelDialog.open('/api/kd-scientific/ys-wtwbyjkffy/import');
    },
    uploadAfter() {
      this.page.currentPage = 1;
      this.getList(this.page);
    },
    handleExport() {
      exportBloByPost(`/api/kd-scientific/ys-wtwbyjkffy/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/outsourcingCost',
  res: ({ data }) => {
    data.records = data.records.map(item => {
      item.wwfy = (item.wwfy || 0).toFixed(2);

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