<template>
  <basic-container>
    <avue-crud
      v-bind="bindVal"
      v-on="onEvent"
      v-model="form"
      :before-open="handleBeforeOpen"
    >
      <template slot="menuLeft">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-download"
          @click="handleDownAll"
        >
          一键下载
        </el-button>
        <!-- <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          @click="handleExport"
        >
          一键导出
        </el-button> -->
      </template>

      <template slot="body">
        <h3 class="page-title">{{ pageTitle }}</h3>
        <year-month-select v-model="params.year" :showMonth="false"></year-month-select>
      </template>

      <template slot="menu" slot-scope="{row}">
        <el-button type="text" icon="el-icon-download" size="mini" @click="handleRowDown(row)">下载</el-button>
      </template>

      <template slot="fileList" slot-scope="{row}">
        <el-link
          v-for="(item, index) of row.fileList"
          :key="index"
          type="primary"
          @click="handleDownFile(item)"
          :style="{marginRight: index === row.fileList.length - 1 ? 0 : '16px'}"
        >
          {{ item.label }}
        </el-link>
      </template>
      
    </avue-crud>

    <div class="file-temp-list">
      <div class="row"><el-link @click="handleDownTemp('template1')">模板：当年和前两个会计年度研发费用总额及占同期销售收入比例的说明.doc</el-link></div>
      <div class="row"><el-link @click="handleDownTemp('template2')">模板：年度职工和科技人员情况说明.doc</el-link></div>
      <div class="row"><el-link @click="handleDownTemp('template3')">模板：年度主要产品(服务)发挥核心支持作用的技术属于《国家重点支持的高新技术领域》规定范围的说明.doc</el-link></div>
      <div class="row"><el-link @click="handleDownTemp('template4')">模版：研发费用结构明细表.xls</el-link></div>
    </div>
  </basic-container>
</template>

<script>
import YearMonthSelect from "@/components/year-month-select";
import {exportBlob} from "@/api/common";
import {getToken} from "@/util/auth";
import {downloadXls, downloadFileByUrl} from "@/util/util";
import moment from "moment";
import { getDictValueByKey } from '@/api/system/dict';
import { mapGetters } from "vuex";


export default window.$crudCommon({
  components: {
    YearMonthSelect,
  },
  data() {
    return {
      params: {
        year: "",
      },
      isSelAnnual: false,

      wideTableColumns: [],
      printTitle: "",

      currIndex: 0
    };
  },
  watch: {
    'params.year'() {
      this.getList();
    },
  },
  computed: {
    ...mapGetters(['tag']),
    pageTitle() {
      let yearCN = this.params.year ? moment(this.params.year).format('yyyy年') : ''
      return `${yearCN}${this.tag.label}`
    }
  },
  created() {
    this.option.column.forEach(item => {
      if (item.prop == 'fileList') {
        item.uploadBefore = (file, done, loading) => {
          if (this.form.fileList.some(item => item.label == file.name)) {
            this.$message.error(`${file.name}文件已存在!`);
            return;
          }
          done();
        }
      }
    })
  },
  methods: {
    loadData() {
      this.loading = true;
      this.api.getList(this.getSearchParams()).then(({ data }) => {
        
        if (data.code == 200) {
          let list = [{
            id: data.data.id,
            fileName: '高新项目技术鉴定资料',
            fileList: JSON.parse(data.data.d1 || '[]')
          }, {
            id: data.data.id,
            fileName: '加计扣除项目技术鉴定资料',
            fileList: JSON.parse(data.data.d2 || '[]')
          }, {
            id: data.data.id,
            fileName: '高新技术企业认定资料',
            fileList: JSON.parse(data.data.d3 || '[]')
          }, {
            id: data.data.id,
            fileName: '年度主要产品(服务)发挥核心支持作用的技术属于《国家重点支持的高新技术领域》规定范围的说明',
            fileList: JSON.parse(data.data.d4 || '[]')
          }, {
            id: data.data.id,
            fileName: '高新技术产品(服务)及对应收入资料',
            fileList: JSON.parse(data.data.d5 || '[]')
          }, {
            id: data.data.id,
            fileName: '年度职工和科技人员情况说明',
            fileList: JSON.parse(data.data.d6 || '[]')
          }, {
            id: data.data.id,
            fileName: '当年和前两个会计年度研发费用总额及占同期销售收入比例的说明',
            fileList: JSON.parse(data.data.d7 || '[]')
          }, {
            id: data.data.id,
            fileName: '研发管理制度',
            fileList: JSON.parse(data.data.d8 || '[]')
          }, {
            id: data.data.id,
            fileName: '研发费用结构明细表',
            fileList: JSON.parse(data.data.d9 || '[]')
          }, {
            id: data.data.id,
            fileName: '加计扣除情况说明',
            fileList: JSON.parse(data.data.d10 || '[]')
          }, {
            id: data.data.id,
            fileName: '其他高新备查资料',
            fileList: JSON.parse(data.data.d11 || '[]')
          }, {
            id: data.data.id,
            fileName: '其他加计扣除备查资料',
            fileList: JSON.parse(data.data.d12 || '[]')
          }];
          this.data = list;
        }


        this.loading = false;
      })
    },
    handleBeforeOpen(done) {
      this.currIndex = this.form.$index + 1;
      done();
    },
    // handleExport() {
    //   exportBlob(`/api/kd-scientific/fjbczl/export?${this.website.tokenHeader}=${getToken()}`, this.params).then(res => {
    //     downloadXls(res.data, `${this.params.year}附件补充.xlsx`);
    //   });
    // },
    handleDownFile(file) {
      downloadFileByUrl(file.value, file.label);
    },
    handleDownAll() {
      let downParams = { year: this.params.year };
      exportBlob(`/api/kd-scientific/archive/fjbczl/download?${this.website.tokenHeader}=${getToken()}`, downParams).then(res => {
        downloadXls(res.data, `${this.pageTitle}.zip`);
      });
    },
    handleRowDown() {
      this.$message.warning("功能建设中...");
    },
    getFormData() {
      return { id: this.form.id, yearAndMonth: this.params.year, [`d${this.currIndex}`]: JSON.stringify(this.form.fileList) }
    },
    handleDownTemp(templateKey) {
      getDictValueByKey('fjbczl-template', templateKey).then(({ data }) => {
        if (data.code == 200) {
          if (typeof data.data === 'object') {
            this.$message.error('暂未配置模板');
            return;
          }
          let tempObj = {
            'template1': '当年和前两个会计年度研发费用总额及占同期销售收入比例的说明.doc',
            'template2': '年度职工和科技人员情况说明.doc',
            'template3': '年度主要产品(服务)发挥核心支持作用的技术属于《国家重点支持的高新技术领域》规定范围的说明.doc',
            'template4': '研发费用结构明细表.xls'
          };

          downloadFileByUrl(data.data, tempObj[templateKey]);
        }
      })
    },
  },
}, {
  // 模块路径
  name: 'achiveManage/fileSupplement',
});
</script>

<style lang="scss" scoped>
.file-temp-list {
  margin-top: 20px;
  .row {
    line-height: 24px;
  }
}
</style>
