<template>
  <el-dialog
    title="详情"
    :visible.sync="open"
    width="90%"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <Com-search
      style="margin-bottom: 10px"
      v-if="searchObj"
      :search="searchObj"
      @handleSearch="handleSearch"
    >
      <template #selectTree>
        <treeselect
          v-model="orgNo"
          :options="deptOptions"
          :show-count="true"
          placeholder="请选择所属单位"
          :normalizer="unitNamenormalizer"
        />
      </template>
      <template #btn>
        <el-button type="primary" style="margin-left: 8px" @click="handleExport"
          >导出</el-button
        >
      </template>
    </Com-search>
    <div class="tableHeight" v-if="reload">
      <Com-table :tableFormat="tableFormat" :tableData="tableData"></Com-table>
    </div>
    <pagination
      v-if="type === '全年物料使用情况'"
      :total="pages.total"
      :page.sync="pages.pageNum"
      :limit.sync="pages.pageSize"
      @pagination="getPage"
    />
    <pagination
      v-else-if="type === '本月度周转率'"
      :total="pages.total"
      :page.sync="pages.pageNo"
      :limit.sync="pages.pageSize"
      @pagination="queryOrgStockRevolveInfoPage"
    />
    <pagination
      v-else-if="type == '各单位设备信息'"
      :total="pages.total"
      :page.sync="pages.pageNum"
      :limit.sync="pages.pageSize"
      @pagination="findMtrlPage"
    />
    <pagination
      v-else-if="type !== '所有各单位月度周转率'"
      :total="pages.total"
      :page.sync="pages.pageNum"
      :limit.sync="pages.pageSize"
      @pagination="getPage2"
    />
    <span slot="footer">
      <el-button @click="open = false">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import { deptTreeSelect } from "@/api/system/user";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  materialOrgUseInfoPage,
  massOrgEqpInfoPage,
  queryOrgStockRevolveCount,
  queryOrgStockRevolveInfoPage,
  queryEquipmentTypeList,
  findMtrlPage,
} from "@/api/home";
export default {
  components: { Treeselect },
  data() {
    return {
      deptOptions: [],
      orgNo: undefined,
      open: false,
      tableFormat: [],
      tableData: [],
      type: "",
      defaultSearchData: {},
      pages: {
        pageNo: 1,
        pageNum: 1,
        pageSize: 50,
        total: 0,
      },
      searchObj: [],
      searchData: {},
      reload: true,
    };
  },
  methods: {
    handleExport() {
      console.log(this.searchData, "this.searchData");
      this.download(
        "adsMatOrgStockRevolveInfoD/exportOrgStockRevolveInfo",
        {
          ...this.searchData,
        },
        `${new Date().getTime()}.xlsx`
      );
    },
    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;
      });
    },
    resetForm(obj) {
      for (const key in obj) {
        if (Object.hasOwnProperty.call(obj, key)) {
          obj[key] = null;
        }
      }
      return obj;
    },
    handleInit({ defaultSearchData, tableFormat, type, searchCollect }) {
      this.tableFormat = [];
      this.tableData = [];
      this.type = "";
      this.defaultSearchData = {};
      this.searchObj = [];
      this.searchData = this.resetForm(this.searchData);
      this.reload = false;
      this.$nextTick(() => {
        this.reload = true;
      });
      this.tableFormat = tableFormat;
      this.type = type;
      this.defaultSearchData = defaultSearchData;
      this.pages.pageNo = 1;
      this.pages.pageNum = 1;
      this.pages.total = 0;
      this.searchObj = searchCollect || null;
      this.getDeptTree();
      this.open = true;
      if (type === "全年物料使用情况") {
        this.getPage();
      } else if (type === "所有各单位月度周转率") {
        this.queryOrgStockRevolveCount();
      } else if (type === "本月度周转率") {
        this.orgNo = this.defaultSearchData.orgNo;
        let temp = this.searchObj.find((o) => o.value === "extractMonths");
        temp.default = this.defaultSearchData.extractMonths;
        this.searchData.extractMonths = this.defaultSearchData.extractMonths;
        this.queryOrgStockRevolveInfoPage();
      } else if (type === "各单位设备信息") {
        this.findMtrlPage();
        this.queryEquipmentTypeList();
      } else {
        this.getPage2();
      }
    },
    queryEquipmentTypeList() {
      queryEquipmentTypeList().then((res) => {
        if (res && res.code == 200) {
          let temp = this.searchObj.find((o) => o.value === "eqpType");
          temp.options = res.data || [];
        }
      });
    },
    findMtrlPage() {
      let params = {
        ...this.pages,
        ...this.searchData,
        ...this.defaultSearchData,
      };
      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();
        }
      });
    },
    queryOrgStockRevolveInfoPage() {
      this.searchData.orgNo = this.orgNo;
      let obj = {
        ...this.pages,
        ...this.defaultSearchData,
        ...this.searchData,
      };
      queryOrgStockRevolveInfoPage(obj).then((res) => {
        if (res && res.code == 200) {
          this.tableData = res.data.records || [];
          this.pages.total = res.data.total;
        }
      });
    },
    queryOrgStockRevolveCount() {
      let obj = {
        ...this.defaultSearchData,
      };
      queryOrgStockRevolveCount(obj).then((res) => {
        if (res && res.code == 200) {
          this.tableData = res.data || [];
        }
      });
    },
    handleSearch(obj) {
      this.searchData = obj;
      this.total = 0;
      this.pages.pageNum = 1;
      this.pages.pageNo = 1;
      if (this.type === "本月度周转率") {
        this.queryOrgStockRevolveInfoPage();
      } else if (this.type === "各单位设备信息") {
        this.findMtrlPage();
      } else {
        this.getPage();
      }
    },
    getPage2() {
      let obj = {
        ...this.pages,
        ...this.defaultSearchData,
      };
      massOrgEqpInfoPage(obj).then((res) => {
        if (res && res.code == 200) {
          this.tableData = res.rows || [];
          this.pages.total = res.total;
        }
      });
    },
    getPage() {
      let obj = {
        ...this.pages,
        // ...this.searchData,
        ...this.defaultSearchData,
      };
      materialOrgUseInfoPage(obj).then((res) => {
        if (res && res.code == 200) {
          this.tableData = res.rows || [];
          this.pages.total = res.total;
        }
      });
    },
    handleClose() {
      this.open = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.tableHeight {
  height: 500px;
}
::v-deep {
  .el-dialog__footer {
    text-align: center;
  }
}
</style>
