<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>
      <template #btn>
        <el-button type="primary" @click="handleExport">导出</el-button>
      </template>
    </Com-search>
    <Com-table
      :tableFormat="tableFormat"
      :tableData="tableData"
      :loading="loading"
    >
    </Com-table>
    <pagination
      :total="pages.total"
      :page.sync="pages.pageNo"
      :limit.sync="pages.pageSize"
      @pagination="getPageData"
    />
  </div>
</template>

<script>
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { findCabinetRecipientsItemCountPage } from "@/api/home";
import { tableFormat, searchObj } from "./utils/index.json";
export default {
  components: { Treeselect },
  data() {
    return {
      loading: false,
      tableData: [],
      tableFormat: tableFormat,
      pages: {
        pageNo: 1,
        pageSize: 50,
        total: 0,
      },
      searchObj: searchObj,
      searchData: {
        mtrlNo: "",
        mtrlDes: "",
        orgNo: "",
        createTimeStart: "",
        createTimeEnd: "",
        queryType: "",
      },
      orgNo: undefined,
      deptOptions: [],
    };
  },
  mounted() {
    this.getDeptTree();
    this.getPageData();
  },
  methods: {
    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;
      });
    },
    // 导出
    handleExport() {
      this.searchData.orgNo = this.orgNo;
      let searchData = JSON.parse(JSON.stringify(this.searchData));
      if (searchData.createTime && searchData.createTime.length) {
        searchData.createTimeStart = searchData.createTime[0];
        searchData.createTimeEnd = searchData.createTime[1];
      } else {
        searchData.createTimeStart = "";
        searchData.createTimeEnd = "";
      }
      delete searchData.createTime;
      this.download(
        "indexStatiss/exportCabinetRecipientsItemCount",
        {
          ...searchData,
        },
        `${new Date().getTime()}.xlsx`
      );
    },
    // 搜索
    handleSearch(obj) {
      this.searchData = obj;
      this.pages.pageNo = 1;
      this.getPageData();
    },
    getPageData() {
      this.searchData.orgNo = this.orgNo;
      let searchData = JSON.parse(JSON.stringify(this.searchData));
      if (searchData.createTime && searchData.createTime.length) {
        searchData.createTimeStart = searchData.createTime[0];
        searchData.createTimeEnd = searchData.createTime[1];
      } else {
        searchData.createTimeStart = "";
        searchData.createTimeEnd = "";
      }
      delete searchData.createTime;
      let params = {
        ...this.pages,
        ...searchData,
      };
      this.loading = true;
      findCabinetRecipientsItemCountPage(params).then((res) => {
        if (res && res.code === 200) {
          this.tableData = res.rows || [];
          this.pages.total = res.total;
          this.loading = false;
        } else {
          this.loading = false;
          this.$message.warning(res.msg || "加载失败");
        }
      });
    },
  },
};
</script>

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