<template>
  <basic-container>
    <avue-crud
      v-bind="bindVal"
      v-on="onEvent"
      v-model="form"
      :page.sync="page"
      :permission="permissionList"
    >
      <template slot="menuLeft">
         <el-button
          v-if="!isSelAnnual"
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          @click="handleDelete"
        >
          删 除
        </el-button>
        <el-button
          v-if="!isSelAnnual"
          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
          v-if="!isSelAnnual"
          type="primary"
          size="small"
          @click="handleReadyDevData"
        >
          直接引用
        </el-button>
        <print-table-btn @click="printTable" />
      </template>

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

      <template slot="sysmSearch">
        <div style="display: flex; align-items: center;">
          <span>使用寿命：</span>
          <avue-input-number v-model="extraParams.sysmMin" :min="0" style="width: 90px !important;"></avue-input-number>
          <span style="width: 20px; text-align: center;">至</span>
          <avue-input-number v-model="extraParams.sysmMax" :min="0" style="width: 90px !important;"></avue-input-number>
        </div>
      </template>

      <template slot="zcyzSearch">
        <div style="display: flex; align-items: center;">
          <span>资产原值：</span>
          <avue-input-number v-model="extraParams.zcyzMin" :min="0" style="width: 90px !important;"></avue-input-number>
          <span style="width: 20px; text-align: center;">至</span>
          <avue-input-number v-model="extraParams.zcyzMax" :min="0" style="width: 90px !important;"></avue-input-number>
        </div>
      </template>

      <template slot="ysysmSearch">
        <div style="display: flex; align-items: center;">
          <span>已使用月数：</span>
          <avue-input-number v-model="extraParams.ysysmMin" :min="0" style="width: 90px !important;"></avue-input-number>
          <span style="width: 20px; text-align: center;">至</span>
          <avue-input-number v-model="extraParams.ysysmMax" :min="0" style="width: 90px !important;"></avue-input-number>
        </div>
      </template>

      <template slot="yzjeSearch">
        <div style="display: flex; align-items: center;">
          <span>月折旧额(每台/套)：</span>
          <avue-input-number v-model="extraParams.yzjeMin" :min="0" style="width: 90px !important;"></avue-input-number>
          <span style="width: 20px; text-align: center;">至</span>
          <avue-input-number v-model="extraParams.yzjeMax" :min="0" style="width: 90px !important;"></avue-input-number>
        </div>
      </template>

      <template slot="xmIdForm">
        <project-select v-model="form.xmId" :params="{ yearAndMonth: params.yearAndMonth }"></project-select>
      </template>

      <template slot="assetIdForm">
        <asset-select
          v-model="form.assetId"
          :params="{ yearAndMonth: params.yearAndMonth, zclb: '房屋建筑物' }"
          @change="handleAssetChange"
        ></asset-select>
      </template>
      
    </avue-crud>

    <upload-excel-dialog ref="uploadExcelDialog" :uploadAfter="uploadAfter"/>

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

<script>
import { fetchDeviceData } from "@/api/yfCostManage/basicDataSetting/houseDetailList"
import YearMonthSelect from "@/components/year-month-select";
import UploadExcelDialog from "@/components/upload-excel-dialog";
import projectSelect from "@/components/project-select";
import assetSelect from "@/components/asset-select";
import moment from "moment";
import Decimal from "decimal.js";
import { mapGetters } from "vuex";


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

      wideTableColumns: [],
      wideTableData: [],
    };
  },
  watch: {
    'params.yearAndMonth'(newVal) {
      if (newVal.length > 4) {
        // 是否勾选未全年
        this.isSelAnnual = false;
      } else {
        this.isSelAnnual = true;
      }
      this.page.currentPage = 1;
      this.getList(this.page);
    },
  },
  computed: {
    ...mapGetters(['tag']),
    permissionList() {
      return {
        addBtn: !this.isSelAnnual,
        editBtn: !this.isSelAnnual,
        delBtn: !this.isSelAnnual,
      };
    },
    pageTitle() {
      let yearAndMonthCN = '';
      if (this.params.yearAndMonth) {
        let formatTemp = this.isSelAnnual ? "yyyy年" : "yyyy年MM月";
        yearAndMonthCN = moment(this.params.yearAndMonth).format(formatTemp);
      }

      return `${yearAndMonthCN}${this.tag.label}`.replace(/每月/g, '');
    }
  },
  methods: {
    handleImport() {
      let excelParams = { yearAndMonth: this.params.yearAndMonth };
      this.$refs.uploadExcelDialog.open('/api/kd-scientific/asset/fangwujianzhuwu/import', excelParams);
    },
    uploadAfter() {
      this.page.currentPage = 1;
      this.getList(this.page);
    },
    handleExport() {
      exportBloByPost(`/api/kd-scientific/asset/fangwujianzhuwu/export?${this.website.tokenHeader}=${getToken()}`, this.params).then(res => {
        downloadXls(res.data, `${this.pageTitle}.xlsx`);
      });
    },
    /**
     * 打印表格
     * @param isLandscape 是否横向打印
     */
    printTable(isLandscape) {
      this.wideTableColumns = this.printOption.column;
      this.wideTableData = [...this.$refs.crud.list];
      this.$nextTick(() => {
        this.$refs.printWideTable.printTable(isLandscape);
      })
    },
    handleReadyDevData() {
      fetchDeviceData({ yearAndMonth: this.params.yearAndMonth}).then(res => {
        let data = res.data;
        if (data.success) {

          this.$message.success('引用成功!');
          this.page.currentPage = 1;
          this.getList(this.page);
        }
      });
    },
    handleAssetChange({ zcbm }) {
      this.form = { ...this.form, zcbm };
    },
    getFormData() {
      const { xmId, assetId, zcbm } = this.form;
      return { xmId, assetId, zcbm, yearAndMonth: this.params.yearAndMonth };
    },
  },
}, {
  // 模块路径
  name: 'yfCostManage/basicDataSetting/houseDetailList',
  res: ({ data }) => {
    data.records = data.records.map(item => {
      item.zcmc = item.asset ? item.asset.zcmc : '';
      item.zclb = item.asset ? item.asset.zclb : '';
      item.gl = item.asset ? item.asset.gl : '';
      item.yt = item.asset ? item.asset.yt : '';
      item.kssj = item.asset ? item.asset.kssj : '';
      item.sysm = item.asset ? item.asset.sysm : '';
      item.ysysm = item.asset ? item.asset.ysysm : '';
      item.zcyz = item.asset ? item.asset.zcyz : '';
      item.jcz = item.asset ? new Decimal(item.asset.jcz || 0).mul(new Decimal(100)) : '';
      item.yzje = item.asset ? item.asset.yzje : '';
      item.status = item.status ? item.asset.status : '';
      return item;
    });
    return data;
  },
});
</script>
