<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" />
        
        <div style="display: flex; align-items: center;">
          <year-month-select v-model="params.yearAndMonth"></year-month-select>
        </div>
      </template>

      <template slot="sysmSearch">
        <div style="display: flex; align-items: center;">
          <span>使用寿命：</span>
          <avue-input-number v-model="params.sysmMin" :min="0" style="width: 90px !important;"></avue-input-number>
          <span style="width: 20px; text-align: center;">至</span>
          <avue-input-number v-model="params.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="params.zcyzMin" :min="0" style="width: 90px !important;"></avue-input-number>
          <span style="width: 20px; text-align: center;">至</span>
          <avue-input-number v-model="params.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="params.ysysmMin" :min="0" style="width: 90px !important;"></avue-input-number>
          <span style="width: 20px; text-align: center;">至</span>
          <avue-input-number v-model="params.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="params.yzjeMin" :min="0" style="width: 90px !important;"></avue-input-number>
          <span style="width: 20px; text-align: center;">至</span>
          <avue-input-number v-model="params.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="data"
      :print-title="printTitle"
      :rows-per-page="30"
      :default-landscape="true"
    />
  </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";


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

      wideTableColumns: [],
      printTitle: "",
    };
  },
  watch: {
    'params.yearAndMonth'(newVal) {
      if (newVal.length > 4) {
        // 是否勾选未全年
        this.isSelAnnual = false;
      } else {
        this.isSelAnnual = true;
      }
      this.page.currentPage = 1;
      this.getList(this.page);
    }
  },
  computed: {
    permissionList() {
      return {
        addBtn: !this.isSelAnnual,
        editBtn: !this.isSelAnnual,
        delBtn: !this.isSelAnnual,
      };
    },
  },
  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.params.yearAndMonth}.xlsx`);
      });
    },
    /**
     * 打印表格
     * @param isLandscape 是否横向打印
     */
    printTable(isLandscape) {
      this.wideTableColumns = this.$refs.crud.columnOption;
      this.printTitle = `科研房屋建筑物明细表${this.params.yearAndMonth}`;
      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 ? item.asset.jcz : '';
      item.yzje = item.asset ? item.asset.yzje : '';
      item.status = item.status ? item.asset.status : '';
      return item;
    });
    return data;
  },
});
</script>
