<template>
  <div>
    <div style="margin-top: 6px;">
      <el-form :inline="true">
        <el-form-item label="研发项目名称">
          <project-select v-model="params.xmId" @change="handleProjectChange"></project-select>
        </el-form-item>
        <el-form-item label="研发项目编号">
          {{ selProject.xmbh }}
        </el-form-item>
      </el-form>
    </div>
    <avue-crud
      ref="crud"
      v-bind="bindVal"
      v-on="onEvent"
      v-model="form"
      :page.sync="page"
      :before-open="handleBeforeOpen"
    >
      <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>
        <print-table-btn @click="printTable" />
      </template>

      <template slot="zcbm" slot-scope="scope">
        <span>{{ scope.row.zcmc }}</span>
      </template>

      <template slot="zcbmForm">
        <asset-select
          v-model="form.zcbm"
          :params="{ yearAndMonth: projectStartMonth, zclb: '软件,专利权,非专利技术,其他' }"
          @change="handleAssetChange"
        ></asset-select>
      </template>
      
    </avue-crud>

    <upload-excel-dialog ref="uploadExcelDialog" :showTempBtn="false" templateKey="预算-无形资产摊销费用" templateName="预算-无形资产摊销费用-导入模板" :uploadAfter="uploadAfter"/>
  </div>
</template>

<script>
import {exportBloByPost} from "@/api/common";
import UploadExcelDialog from "@/components/upload-excel-dialog";
import projectSelect from "@/components/project-select";
import {getToken} from "@/util/auth";
import {downloadXls} from "@/util/util";
import moment from "moment";
import Decimal from "decimal.js";
import assetSelect from "@/components/asset-select";



export default window.$crudCommon({
  props: {
    projectId: [String, Number],
    projectData: Object,
    projectStartMonth: [String, Number]
  },
  components: {
    UploadExcelDialog,
    projectSelect,
    assetSelect
  },
  data() {
    return {
      params: {
        xmId: ''
      },
      selProject: {},

      currZcObj: {},
    };
  },
  created() {
    this.params.xmId = this.projectId;
    this.option.height = window.innerHeight - 340;
  },
  watch: {
    projectId(newVal, oldVal) {
      this.params.xmId = newVal;
      if (oldVal) {
        this.selProject = this.projectData;
      }
    },
    "params.xmId"() {
      this.page.currentPage = 1;
      this.getList(this.page);
    },
  },
  computed: {
    pageTitle() {
      return `${this.selProject.xmmc}无形资产摊销费用`;
    }
  },
  methods: {
    validCustom() {
      const { sysm, ysysm } = this.currZcObj;
      const { xmkssj, xmjssj } = this.selProject
      let sm = sysm - ysysm;
      if (this.form.zzsc > sm) {
        this.$message.warning(`设备剩余寿命为${sm}月，在项目的工作时长不能大于设备寿命!`);
        return false;
      }

      if (xmkssj && xmjssj) {
        let monthDiff = moment(xmjssj).diff(moment(xmkssj), 'months', true).toFixed(0);
        if (this.form.zzsc > monthDiff) {
          this.$message.warning(`项目周期为${monthDiff}月，在项目的工作时长不能大于项目周期!`);
          return false;
        }
      }
      return true;
    },
    addBefore(loading, callback) {
      if (!this.validCustom()) {
        loading();
        return;
      }
      callback && callback();
    },
    updateBefore(loading, callback) {
      if (!this.validCustom()) {
        loading();
        return false;
      }
      callback && callback();
    },
    handleBeforeOpen(done) {
      if (!this.projectStartMonth) {
        this.$message.warning("请选择项目!");
        return;
      }
      this.currZcObj = {};

      done();
    },
    getList() {
      if (!this.params.xmId) {
        return;
      }
      this.loadData();
    },
    getFormData() {
      return { assetId: this.currZcObj.id, ...this.form, xmId: this.params.xmId,  }
    },
    handleProjectChange(data) {
      this.selProject = data;
      this.$emit("change", data);
    },
    handleAssetChange(data) {
      this.currZcObj = data;
    },
    handleImport() {
      if (!this.params.xmId) {
        this.$message.warning("请选择项目!");
        return;
      }
      let excelParams = { xmId: this.params.xmId };
      this.$refs.uploadExcelDialog.open('/api/kd-scientific/ys-wxzctxfy/import', excelParams);
    },
    uploadAfter() {
      this.page.currentPage = 1;
      this.getList(this.page);
    },
    handleExport() {
      exportBloByPost(`/api/kd-scientific/ys-wxzctxfy/export?${this.website.tokenHeader}=${getToken()}`, this.params).then(res => {
        downloadXls(res.data, `${this.pageTitle}.xlsx`);
      });
    },
    /**
     * 打印表格
     * @param isLandscape 是否横向打印
     */
    printTable(isLandscape) {
      this.$emit("printClick", {
        column: this.printOption.column,
        data: this.data,
        pageTitle: this.pageTitle,
        zoom: 95
      });
    },
  },
}, {
  // 模块路径
  name: 'projectManage/wxzctxCost',
  res: ({ data }) => {
    data.records = data.records.map(item => {
      item.zcmc = item.zcEntity ? item.zcEntity.zcmc : '';
      item.zcbm = item.zcEntity ? item.zcEntity.zcbm : '';
      item.ytxe = Number(item.zcEntity ? item.zcEntity.yzje : 0).toFixed(2);

      let zzscD = new Decimal(item.zzsc || 0); // 工作时长
      let yzjeD = new Decimal(item.ytxe || 0); // 摊销额
      item.yshj = zzscD.mul(yzjeD).toFixed(2);
      return item;
    });
    return data;
  },
});
</script>
