<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 class="mb-10">
      <el-button type="primary" @click="handleAdd">新增常用物料</el-button>
      <!-- <el-button type="primary" :disabled="!selectRow" @click="handleEdit"
        >修改常用物料</el-button
      > -->
      <el-button
        type="danger"
        :disabled="!(selectList && selectList.length)"
        @click="handleDelete"
        >删除</el-button
      >
      <el-button type="primary" @click="handleExport">导出</el-button>
    </div>
    <Com-table
      ref="table"
      :tableFormat="tableFormat"
      :tableData="tableData"
      @handleSelectionChange="handleSelectionChange"
    >
      <template #operation="scope">
        <el-button type="primary" @click="handleEdit(scope.row)"
          >修改</el-button
        >
      </template>
    </Com-table>
    <pagination
      :total="pages.total"
      :page.sync="pages.pageNo"
      :limit.sync="pages.pageSize"
      @pagination="materialDpage"
    />
    <materials ref="materials" :get-data="materialDpage"></materials>
  </div>
</template>

<script>
import { tableFormat, searchObj } from "./utils/index.json";
import {
  queryEquipmentTypeList,
  materialDpage,
  materialPageDeleteById,
} from "@/api/home";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Materials from "./components/materials.vue";
export default {
  components: { Treeselect, Materials },
  data() {
    return {
      searchObj: searchObj,
      pages: {
        pageNo: 1,
        pageSize: 50,
        total: 0,
      },
      orgNo: undefined,
      deptOptions: [],
      tableData: [],
      tableFormat: tableFormat,
      searchData: {
        orgNo: undefined,
        mtrlNo: "",
        mtrlDes: "",
        equipmentType: "",
      },
      selectRow: null,
      selectList: null,
    };
  },
  mounted() {
    this.queryEquipmentTypeList();
    this.getDeptTree();
    this.materialDpage();
  },
  methods: {
    handleExport() {
      this.download(
        "adsMatCzEquipmentMaterialD/exportEquipmentMaterial",
        {
          ...this.searchData,
        },
        `${new Date().getTime()}.xlsx`
      );
    },
    handleDelete() {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let arr = [];
          this.selectList.map((el) => {
            arr.push({
              id: el.id,
            });
          });
          materialPageDeleteById(arr).then((res) => {
            if (res && res.code == 200) {
              this.$message.success("操作成功！");
              this.selectList = null;
              if (this.$refs && this.$refs.table) {
                this.$refs.table.$refs.table.clearSelection();
              }
              this.materialDpage();
            } else {
              this.$message.success("操作失败！");
            }
          });
        })
        .catch(() => {});
    },
    handleSelectionChange(val) {
      this.selectList = val;
    },
    handleCurrentChange(val) {
      this.selectRow = val;
    },
    handleEdit(row) {
      let obj = {
        data: [row],
        type: "edit",
      };
      this.$refs.materials.handleInit(obj);
    },
    handleAdd() {
      let obj = {
        data: [],
        type: "add",
      };
      this.$refs.materials.handleInit(obj);
    },
    materialDpage() {
      this.searchData.orgNo = this.orgNo;
      let params = {
        ...this.pages,
        ...this.searchData,
      };
      materialDpage(params).then((res) => {
        if (res && res.code === 200) {
          this.tableData = res.data.records || [];
          this.pages.total = res.data.total;
          // if (this.$refs && this.$refs.table) {
          //   this.$refs.table.$refs.table.setCurrentRow();
          // }
        } else {
          this.$message.warning(res.msg || "加载失败");
        }
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    unitNamenormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.orgNo,
        label: node.label,
      };
    },
    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.materialDpage();
    },
  },
};
</script>

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