<template>
  <div class="app-container">
    <Com-search :search="searchObj" @handleSearch="handleSearch">
      <template #btn>
        <el-button type="primary" style="margin-left: 8px" @click="handleExport"
          >导出</el-button
        >
      </template>
    </Com-search>
    <Com-table
      :tableFormat="tableFormat"
      ref="table"
      :tableData="tableData"
    ></Com-table>
    <pagination
      :total="pages.total"
      :page.sync="pages.pageNum"
      :limit.sync="pages.pageSize"
      @pagination="findMtrlPage"
    />
  </div>
</template>

<script>
import { findMtrlPage, queryEquipmentTypeList } from "@/api/home";
import { tableFormat, searchObj } from "./utils/index.json";
export default {
  data() {
    return {
      searchObj: searchObj,
      tableFormat: tableFormat,
      tableData: [],
      pages: {
        pageNo: 1,
        pageSize: 50,
        total: 0,
      },
      searchData: {
        eqpNo: "",
        eqpNm: "",
      },
    };
  },
  mounted() {
    this.queryEquipmentTypeList();
    this.findMtrlPage();
  },
  methods: {
    handleExport() {
      /* let searchData = JSON.parse(JSON.stringify(this.searchData)); */
      this.download(
        "indexStatiss/exportMatEqp",
        JSON.stringify(this.searchData),
        `${new Date().getTime()}.xlsx`,
        {
          headers: { "Content-Type": "application/json" },
          transformRequest: [],
        }
      );
    },
    queryEquipmentTypeList() {
      queryEquipmentTypeList().then((res) => {
        if (res && res.code == 200) {
          let temp = this.searchObj.find((o) => o.value === "eqpType");
          temp.options = res.data || [];
        }
      });
    },
    handleSearch(obj) {
      this.searchData = obj;
      this.pages.pageNo = 1;
      this.findMtrlPage();
    },
    findMtrlPage() {
      let params = {
        ...this.pages,
        ...this.searchData,
      };
      findMtrlPage(params).then((res) => {
        if (res && res.code === 200) {
          this.tableData = res.rows || [];
          this.pages.total = res.total;
        } else {
          this.$message.warning(res.msg || "加载失败");
        }
        if (this.$refs && this.$refs.table) {
          this.$refs.table.$refs.table.doLayout();
        }
      });
    },
  },
};
</script>

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