<template>
  <basic-container>
    <avue-crud
      v-bind="bindVal"
      v-on="onEvent"
      v-model="form"
      :page.sync="page"
    >
      <template slot="menuLeft">
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          @click="handleDelete"
        >
          删 除
        </el-button>
        <el-button
          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>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-printer"
          @click="printTable"
        >
          打印
        </el-button>

        <div class="tip">提示：企业可自行上传全部历史专利信息和资料，如专利与系统中已建档的研究项目无关，则研发项目编号和研发项目名称非必填。</div>
      </template>

      <template slot="kfwcrqSearch">
        <span>开发完成时间：</span>
        <el-date-picker
          v-model="extraParams.kfwcrq"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          style="width: 220px !important;"
        >
        </el-date-picker>
      </template>

      <template slot="scfbrqSearch">
        <span>首次发布时间：</span>
        <el-date-picker
          v-model="extraParams.scfbrq"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          style="width: 220px !important;"
        >
        </el-date-picker>
      </template>

      <template slot="djpzrqSearch">
        <span>登记批准日期：</span>
        <el-date-picker
          v-model="extraParams.djpzrq"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          style="width: 220px !important;"
        >
        </el-date-picker>
      </template>

      <template slot="xmIdForm">
        <project-select v-model="form.xmId" @change="handleProjectChange"></project-select>
      </template>

      <template slot="attachment" slot-scope="{ row }">
        <div v-for="(file, index) of row.attachment" :key="index">
          <el-link @click="handleDownFile(file)">{{ file.label }}</el-link>
        </div>
      </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"
    />
  </basic-container>
</template>

<script>
import {exportBlob} from "@/api/common";
import UploadExcelDialog from "@/components/upload-excel-dialog";
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import {getToken} from "@/util/auth";
import {downloadXls} from "@/util/util";
import projectSelect from "@/components/project-select";


export default window.$crudCommon({
  components: {
    UploadExcelDialog,
    projectSelect
  },
  data() {
    return {
      wideTableColumns: [],
      printTitle: "",
    };
  },
  methods: {
    getSearchParams() {
      const newParams = { ...this.params, ...this.extraParams };
      if (newParams.kfwcrq && newParams.kfwcrq.length) {
        newParams.kfwcrqMin = newParams.kfwcrq[0];
        newParams.kfwcrqMax = newParams.kfwcrq[1];
      }
      if (newParams.scfbrq && newParams.scfbrq.length) {
        newParams.scfbrqMin = newParams.scfbrq[0];
        newParams.scfbrqMax = newParams.scfbrq[1];
      }
      if (newParams.djpzrq && newParams.djpzrq.length) {
        newParams.djpzrqMin = newParams.djpzrq[0];
        newParams.djpzrqMax = newParams.djpzrq[1];
      }
      delete newParams.kfwcrq;
      delete newParams.scfbrq;
      delete newParams.djpzrq;
      return newParams;
    },
    getFormData() {
      let newFormData = { ...this.form };
      if (!!newFormData.attachment) {
        newFormData.attachment = JSON.stringify(newFormData.attachment);
      }
      return newFormData;
    },
    handleProjectChange(data){
      this.form = {
        ...this.form,
        xmmc: data.xmmc,
        xmbh: data.xmbh
      }
    },
    handleImport() {
      this.$refs.uploadExcelDialog.open('/api/kd-scientific/kycg/rjzzq/import', {});
    },
    uploadAfter() {
      this.page.currentPage = 1;
      this.getList(this.page);
    },
    handleExport() {
      exportBlob(`/api/kd-scientific/kycg/rjzzq/export?${this.website.tokenHeader}=${getToken()}`, {}).then(res => {
        downloadXls(res.data, `软件著作列表.xlsx`);
        NProgress.done();
      })
    },
    printTable() {
      this.wideTableColumns = this.$refs.crud.columnOption;
      this.printTitle = `软件著作列表`;
      this.$nextTick(() => {
        this.$refs.printWideTable.printTable(false);
      })
    },
    handleDownFile(file) {
      window.open(file.value, "_blank");
    }
  },
}, {
  // 模块路径
  name: 'achievement/softWorks',
  res: ({ data }) => {
    data.records = data.records.map(item => {
      item.xmId = item.xmId === 0 ? '' : item.xmId;
      item.attachment = !!item.attachment ? JSON.parse(item.attachment) : [];
      return item;
    });

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