<template>
  <div class="app-container">
    <Com-search :search="searchObj" @handleSearch="handleSearch">
      <template #selectTree>
        <treeselect
          v-model="orgNo"
          :options="deptOptions"
          :show-count="true"
          placeholder="请选择所属单位"
          :normalizer="unitNamenormalizer"
        />
      </template>
    </Com-search>
    <div style="margin-bottom: 10px">
      <el-button type="primary" @click="handleExport">导出</el-button>
    </div>
    <Com-table :tableFormat="tableFormat" :tableData="tableData">
      <template #workNumber="scope">
        <el-link
          type="primary"
          :underline="false"
          @click="handleDetail(scope.row, 'workNumber')"
          >{{ scope.row.workNumber }}</el-link
        >
      </template>
      <template #qxgd="scope">
        <el-link
          type="primary"
          :underline="false"
          @click="handleDetail(scope.row, 'qxgd')"
          >{{ scope.row.qxgd }}</el-link
        >
      </template>
    </Com-table>
    <pagination
      :total="pages.total"
      :page.sync="pages.pageNo"
      :limit.sync="pages.pageSize"
      @pagination="findMassPage"
    />
    <detail ref="Detail"></detail>
  </div>
</template>

<script>
import { faultPage, queryEquipmentTypeList } from "@/api/home";
import { tableFormat, searchObj } from "./utils/index.json";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Detail from "./components/detail.vue";
export default {
  components: { Treeselect, Detail },
  data() {
    return {
      searchObj: searchObj,
      tableFormat: tableFormat,
      tableData: [],
      pages: {
        pageNo: 1,
        pageSize: 50,
        total: 0,
      },
      orgNo: undefined,
      searchData: {
        time: "",
        mtrlNo: "",
        mtrlDes: "",
        orgNo: "",
        startTime: "",
        endTime: "",
      },
      deptOptions: [],
      selectTions: [],
    };
  },
  created() {
    this.searchData.time = [
      this.currentTime(),
      this.getLastDayOfNaturalMonth(),
    ];
    let temp = this.searchObj.find((o) => o.value === "time");
    temp.default = [this.currentTime(), this.getLastDayOfNaturalMonth()];
  },
  mounted() {
    this.queryEquipmentTypeList();
    this.getDeptTree();
    this.findMassPage();
  },
  methods: {
    // 导出
    handleExport() {
      this.searchData.orgNo = this.orgNo;
      let searchData = JSON.parse(JSON.stringify(this.searchData));
      if (searchData.time && searchData.time.length) {
        searchData.startTime = searchData.time[0];
        searchData.endTime = searchData.time[1];
      } else {
        searchData.startTime = "";
        searchData.endTime = "";
      }
      delete searchData.time;
      let params = {
        ...searchData,
      };
      this.download(
        "adsMatRationRecommendD/exportFault",
        {
          ...params,
        },
        `${new Date().getTime()}.xlsx`
      );
    },
    handleDetail(row, type) {
      let obj = {
        data: row,
        type: type,
        time: this.searchData.time,
      };
      this.$refs.Detail.handleInit(obj);
    },
    //获取当月第一天
    currentTime() {
      const currentDate = new Date();
      const year = currentDate.getFullYear(); //获取当前年
      const month = String(currentDate.getMonth() + 1).padStart(2, "0"); //获取当前月
      const firstDay = "01"; //日
      return `${year}-${month}-${firstDay}`;
    },
    //获取当月最后一天
    getLastDayOfNaturalMonth() {
      //下个月的1号减去1天
      let date = new Date();
      let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
      let lastDayFormatted = lastDay.toISOString().slice(0, 10);
      return lastDayFormatted;
    },
    unitNamenormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.orgNo,
        label: node.label,
      };
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    queryEquipmentTypeList() {
      queryEquipmentTypeList().then((res) => {
        if (res && res.code == 200) {
          this.selectTions = res.data || [];
          let temp = this.searchObj.find((o) => o.value === "equipmentType");
          temp.options = res.data || [];
        }
      });
    },
    handleSearch(obj) {
      this.searchData = obj;
      this.pages.pageNo = 1;
      this.findMassPage();
    },
    findMassPage() {
      this.searchData.orgNo = this.orgNo;
      let searchData = JSON.parse(JSON.stringify(this.searchData));
      if (searchData.time && searchData.time.length) {
        searchData.startTime = searchData.time[0];
        searchData.endTime = searchData.time[1];
      } else {
        searchData.startTime = "";
        searchData.endTime = "";
      }
      delete searchData.time;
      let params = {
        ...this.pages,
        ...searchData,
      };
      faultPage(params).then((res) => {
        if (res && res.code === 200) {
          this.tableData = res.data.records || [];
          this.pages.total = res.data.total;
        } else {
          this.$message.warning(res.msg || "加载失败");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  position: absolute;
  width: 100%;
  height: calc(100% - 60px - 60px - 36px);
}
</style>
