<template>
  <div style="height: 70vh; overflow: auto;">
    <a-table :dataSource="tableData" :columns="columns" :rowKey="(r, i) => i" :pagination="false" :loading="loading" bordered>
      <template slot="evaluationInfo" slot-scope="record">
        <a-tag v-if="!!record.auditState && record.auditState == 2" :color="'green'">{{record.totalScore}}</a-tag>
        <a-tag v-else :color="'red'">未评审</a-tag>
      </template>
    </a-table>
  </div>
</template>

<script>
import { getType } from '@/views/utils/auth'
import { mergeRow } from '@/views/utils/common'

export default {
  name: "evaluationStatistic",
  data() {
    return {
      loading: false,
      columns: [
        { title: '项目编号', dataIndex: 'projNo', align: 'center', customRender: (text, row) => { return { children: `${text}`, attrs: { rowSpan: row.projNoRowSpan } } } },
        { title: '项目名称', dataIndex: 'projName', align: 'center', customRender: (text, row) => { return { children: row.projName, attrs: { rowSpan: row.projNoRowSpan } } } },
        { title: '证件号', dataIndex: 'certId', align: 'center' },
        { title: '专家姓名', dataIndex: 'personName', align: 'center' },
        { title: '手机号', dataIndex: 'mobile', align: 'center' },
        { title: '评分', scopedSlots: { customRender: 'evaluationInfo' }, align: 'center' },
        { title: '评审内容', dataIndex: 'remark', align: 'center', ellipsis: true },
        { title: '平均分', dataIndex: 'averageScore', align: 'center', customRender: (text, row) => { return { children: row.averageScore, attrs: { rowSpan: row.projNoRowSpan } } } },
      ],
      tableData: [],
      mergeList: [],
      reportYear: null,
    };
  },
  created() {
    this.getYear()
  },
  methods: {
    getYear () {
      this.$api.batch.getCurrentYearBatch({ type: 1, projType: getType(), timeType: 1  }).then(({ data = {} }) => {
        if (data) {
          this.reportYear = data.year
          this.getEvaluationStatistic()
        }
      }).catch(() => { })
    },
    getEvaluationStatistic () {
      this.loading = true
      const rowMarks = [ 'A', 'B', 'H' ]
      this.$api.statistical.getEvaluationExportExcel({ reportYear: this.reportYear, startRow: 3, rowMarks: rowMarks }).then(({ data = {} }) => {
        if (data) {
          this.tableData = data.evaluationList
          this.mergeList = data.mergeList
          this.loading = false
        }
      }).catch(() => { this.loading = false })
    },
    exportData() {
      import('@/views/utils/Export2Excel').then(excel => {
        const multiHeader = [[ this.reportYear + '项目评审结果列表', '','']] // 标题
        const header = ['项目编号', '项目名称', '证件号', '专家姓名', '手机号', '评分', '评审内容', '平均分'] // 表头
        const filterVal = ['projNo', 'projName', 'certId', 'personName', 'mobile', 'totalScore', 'remark', 'averageScore'] // 数据属性
        const list = this.tableData //请求来的数据
        const merges = ['A1:H1'].concat(this.mergeList) //需要合并的单元格
        const data = list.map(item => filterVal.map(j => item[j])) // 转换二维数组
        const filename = '项目评审结果列表'
        excel.exportJsonToExcel({
          multiHeader, // 标题--非必要
          header, // 表头
          data,  // 具体数据--二维数组
          merges, // 合并--非必要
          filename, // 下载文件名
          autoWidth: true,  // 不自动调整列宽
        })
      })
    }
  },
  watch: {
    tableData: {
      handler (value) {
        mergeRow('projNo', this.tableData) // 合并projNo
      }
    }
  }
};
</script>