<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 style="margin-right: 15px" type="primary" @click="handleEdit"
          >新增</el-button
        >
      </template>
    </Com-search>
    <Com-table
      :tableFormat="tableFormat"
      :tableData="tableData"
      :loading="loading"
    >
      <template #autogenerationSwitch="scope">
        <span>{{ scope.row.autogenerationSwitch == 1 ? "关闭" : "开启" }}</span>
      </template>
      <template #operate="scope">
        <el-button type="primary" @click="handleModify(scope.row)"
          >编辑</el-button
        >
        <el-button type="danger" @click="handleDelete(scope.row)"
          >删除</el-button
        >
      </template>
    </Com-table>
    <edit ref="Edit" :get-data="getPageData"></edit>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import { deptTreeSelect } from "@/api/system/user";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  adsCzxtReplenishmentPeriodDpage,
  adsCzxtReplenishmentPeriodDdeleted,
} from "@/api/home";
import { searchObj, tableFormat } from "./utils/index.json";
import Edit from "./components/edit.vue";
export default {
  components: { Treeselect, Edit },
  data() {
    return {
      tableFormat: tableFormat,
      tableData: [],
      orgNo: undefined,
      pages: {
        pageNo: 1,
        pageSize: 50,
        total: 0,
      },
      loading: false,
      searchObj: searchObj,
      searchData: {
        orgNo: "",
      },
      deptOptions: [],
    };
  },
  mounted() {
    this.getDeptTree();
    this.getPageData();
  },
  methods: {
    handleModify(row) {
      let obj = {
        type: "edit",
        data: row,
      };
      this.$refs.Edit.handleClick(obj);
    },
    handleEdit() {
      let obj = {
        type: "add",
      };
      this.$refs.Edit.handleClick(obj);
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除数据, 是否继续?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            id: row.id,
          };
          adsCzxtReplenishmentPeriodDdeleted(params).then((res) => {
            if (res && res.code == 200) {
              this.getPageData();
              this.$message.success("删除成功");
            } else {
              this.$message.warning("删除失败");
            }
          });
        })
        .catch(() => {});
    },
    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;
      });
    },
    handleSearch(obj) {
      this.searchData = obj;
      this.pages.pageNo = 1;
      this.getPageData();
    },
    getPageData() {
      this.searchData.orgNo = this.orgNo;
      let params = {
        ...this.pages,
        ...this.searchData,
      };
      this.loading = true;
      adsCzxtReplenishmentPeriodDpage(params).then((res) => {
        if (res && res.code === 200) {
          this.tableData = res.data.records || [];
          this.pages.total = res.data.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>
