<template>
  <basic-container>
    <avue-crud
      v-bind="bindVal"
      v-on="onEvent"
      v-model="form"
      :page.sync="page"
      :span-method="spanMethod"
    >
      <template slot="menuLeft">
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          @click="handleExport"
        >
          导出
        </el-button>
        <div>
          <year-month-select v-model="params.yearAndMonth"></year-month-select>
        </div>
      </template>
      
    </avue-crud>
  </basic-container>
</template>

<script>
import {exportBloByPost} from "@/api/common";
import YearMonthSelect from "@/components/year-month-select";
import moment from "moment";
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import {getToken} from "@/util/auth";
import {downloadXls} from "@/util/util";


export default window.$crudCommon({
  components: {
    YearMonthSelect,
  },
  data() {
    return {
      params: {
        yearAndMonth: moment(new Date()).format('YYYYMM'),
        spanArr: [
          { prop: 'projectName', span: [] },
        ]
      },
    };
  },
  watch: {
    'params.yearAndMonth'(newVal) {
      this.page.currentPage = 1;
      this.getList(this.page);
    }
  },
  methods: {
    getList() {
      this.data = [{
        projectName: '项目1'
      }, {
        projectName: '项目1'
      }, {
        projectName: '项目1'
      }, {
        projectName: '项目2'
      }, {
        projectName: '项目3'
      }];
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      let fields = ["projectName"];
      let cellValue = row[column.property];
      if (cellValue && fields.includes(column.property)) {
        let prevRow = this.data[rowIndex - 1];
        let nextRow = this.data[rowIndex + 1];
        if (prevRow && prevRow[column.property] === cellValue) {
          return { rowspan: 0, colspan: 0 };
        } else {
          let countRowspan = 1;
          while (nextRow && nextRow[column.property] === cellValue) {
            nextRow = this.data[++countRowspan + rowIndex];
          }
          if (countRowspan > 1) {
            row.hbFlag = true;
            return { rowspan: countRowspan, colspan: 1 };
          }
        }
      }
    },
    handleExport() {
      this.$confirm("是否导出吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        NProgress.start();
        exportBloByPost(`/api/kd-scientific/technician/export?${this.website.tokenHeader}=${getToken()}`, this.params).then(res => {
          downloadXls(res.data, `项目参研人员汇总表${this.params.yearAndMonth}.xlsx`);
          NProgress.done();
        })
      });
    },
  },
}, {
  // 模块路径
  name: 'cyPersonSumList'
});
</script>
