<template>
  <basic-container>
    <avue-crud
      v-bind="bindVal"
      v-on="onEvent"
      v-model="form"
      :page.sync="page"
    >
      <template slot="menuLeft">
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          @click="handleExport"
        >
          导出
        </el-button>
        <print-table-btn @click="printTable" />
        
        <div style="display: flex; align-items: center;">
          <year-month-select v-model="params.yearAndMonth" :showAllYear="false"></year-month-select>
        </div>
      </template>
    </avue-crud>

    <WideTablePrinter
      ref="printWideTable"
      :columns="wideTableColumns"
      :data="data"
      :print-title="printTitle"
      :rows-per-page="30"
      :default-landscape="true"
    />
  </basic-container>
</template>

<script>
import YearMonthSelect from "@/components/year-month-select";
import {exportBlob} from "@/api/common";
import {getToken} from "@/util/auth";
import {downloadXls} from "@/util/util";
import moment from "moment";
import Decimal from "decimal.js";


export default window.$crudCommon({
  components: {
    YearMonthSelect,
  },
  data() {
    return {
      params: {
        yearAndMonth: moment(new Date()).format('YYYYMM'),
      },

      wideTableColumns: [],
      printTitle: "",
    };
  },
  watch: {
    'params.yearAndMonth'(newVal) {
      this.page.currentPage = 1;
      this.getList(this.page);
    }
  },
  methods: {
    handleExport() {
      exportBlob(`/api/kd-scientific/export?${this.website.tokenHeader}=${getToken()}`, this.params).then(res => {
        downloadXls(res.data, `${this.params.yearAndMonth}高新研发费用辅助账汇总表.xlsx`);
      });
    },
    /**
     * 打印表格
     * @param isLandscape 是否横向打印
     */
    printTable(isLandscape) {
      this.wideTableColumns = this.$refs.crud.columnOption;
      this.printTitle = `${this.params.yearAndMonth}高新研发费用辅助账汇总表.xlsx`;
      this.$nextTick(() => {
        this.$refs.printWideTable.printTable(isLandscape);
      })
    },
  },
}, {
  // 模块路径
  name: 'externalReports/gxAuxiliarySumList',
  res: ({ data }) => {
    data.records = data.records.map(item => {
      item.ryrgTotal = item.ryrgTotal.toFixed(2);
      item.zjtrTotal = item.zjtrTotal.toFixed(2);
      item.zjfyycqdtfyTotal = item.zjfyycqdtfyTotal.toFixed(2);
      item.sjfTotal = item.sjfTotal.toFixed(2);
      item.wxzctxTotal = item.wxzctxTotal.toFixed(2);
      item.zbtsfyysyfyTotal = item.zbtsfyysyfyTotal.toFixed(2);
      item.qtfyTotal = item.qtfyTotal.toFixed(2);
      item.qtfyLimit = item.qtfyLimit.toFixed(2);
      item.qtfyAdjust = item.qtfyAdjust.toFixed(2);
      item.wwyfTotal = item.wwyfTotal.toFixed(2);

      // 合计
      let amount = 0;
      amount = new Decimal(item.ryrgTotal)
        .add(new Decimal(item.zjtrTotal))
        .add(new Decimal(item.zjfyycqdtfyTotal))
        .add(new Decimal(item.sjfTotal))
        .add(new Decimal(item.wxzctxTotal))
        .add(new Decimal(item.zbtsfyysyfyTotal))
        .add(new Decimal(item.qtfyAdjust))
        .add(new Decimal(item.wwyfTotal));
      item.amount = amount;


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