<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>
        <div style="display: flex">
          <el-button type="primary" @click="handleReserve"
            >生成储备定额表</el-button
          >
          <div
            style="
              display: flex;
              align-items: center;
              margin-left: 15px;
              font-weight: bold;
            "
          >
            <div class="orange" style="margin-right: 5px"></div>
            未生成补货计划
            <div
              class="green"
              style="margin-left: 15px; margin-right: 5px"
            ></div>
            已生成补货计划
          </div>
        </div>
      </template>
    </Com-search>
    <Com-table
      ref="table"
      :tableFormat="tableFormat"
      :tableData="tableData"
      :loading="loading"
      @handleSelectionChange="handleSelectionChange"
    >
      <template #isSave="scope">
        <span class="green" v-if="scope.row.isSave == 1"></span>
        <span class="orange" v-if="scope.row.isSave == 0"></span>
      </template>
      <template #operation="scope">
        <el-button type="primary" @click="handleDetail(scope.row)"
          >查看编辑</el-button
        >
        <el-button type="primary" @click="handleReplenishment(scope.row)"
          >生成补货计划</el-button
        >
        <el-button type="danger" @click="handleDelete(scope.row)"
          >删除</el-button
        >
      </template>
    </Com-table>
    <pagination
      :total="pages.total"
      :page.sync="pages.pageNum"
      :limit.sync="pages.pageSize"
      @pagination="getPageData"
    />
    <edit ref="edit" :get-data="getPageData"></edit>
  </div>
</template>

<script>
import { tableFormat, searchObj } from "./utils/index.json";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  pageProject,
  recommendDpage,
  deleteById,
  saveStoreRation,
  saveReplenishment,
} from "@/api/home";
import edit from "./components/edit.vue";
export default {
  components: { edit, Treeselect },
  data() {
    return {
      loading: false,
      tableData: [],
      tableFormat: tableFormat,
      pages: {
        pageNo: 1,
        pageSize: 50,
        total: 0,
      },
      searchObj: searchObj,
      searchData: {
        mtrlNo: "",
        mtrlDes: "",
        deptName: "",
        orgNo: "",
      },
      selectList: [],
      orgNo: undefined,
      deptOptions: [],
    };
  },
  mounted() {
    this.getPageData();
    this.getDeptTree();
  },
  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;
      });
    },
    // 补货清单
    handleReplenishment(row) {
      this.$confirm("生成补货清单?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            id: row.id,
          };
          saveReplenishment(params).then((res) => {
            if (res && res.code == 200) {
              this.$message.success("操作成功");
              this.getPageData();
            } else {
              this.$message.warning(res.msg || "操作失败");
            }
          });
        })
        .catch(() => {});
    },
    // 储备定额表
    handleReserve() {
      this.$confirm("生成储备定额表?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {};
          saveStoreRation(params).then((res) => {
            if (res && res.code == 200) {
              this.$message.success("操作成功");
              this.getPageData();
            } else {
              this.$message.warning(res.msg || "操作失败");
            }
          });
        })
        .catch(() => {});
    },
    // 详情
    handleDetail(row) {
      let obj = {
        data: row,
      };
      this.$refs.edit.handleInit(obj);
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除数据, 是否继续?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            id: row.id,
          };
          deleteById(params).then((res) => {
            if (res && res.code == 200) {
              this.getPageData();
              this.$message.success("删除成功");
            } else {
              this.$message.warning("删除失败");
            }
          });
        })
        .catch(() => {});
    },
    handleSelectionChange(val) {
      this.selectList = val;
    },
    handleEdit() {
      let obj = {
        data: this.selectList,
      };
      this.$refs.edit.handleInit(obj);
    },
    // 导出
    handleExport() {
      this.searchData.orgNo = this.orgNo;
      let searchData = JSON.parse(JSON.stringify(this.searchData));
      if (searchData && searchData.time && searchData.time.length) {
        searchData.startTime = searchData.time[0];
        searchData.endTime = searchData.time[1];
      } else {
        searchData.startTime = null;
        searchData.endTime = null;
      }
      delete searchData.time;
      this.download(
        "adsHyxtMatMtrlForeD/exportMtrlFore",
        {
          ...searchData,
        },
        `${new Date().getTime()}.xlsx`
      );
    },
    // 搜索
    handleSearch(obj) {
      this.searchData = obj;
      this.pages.pageNo = 1;
      this.pages.total = 0;
      this.getPageData();
    },
    // 分页查询
    getPageData() {
      this.searchData.orgNo = this.orgNo;
      let searchData = JSON.parse(JSON.stringify(this.searchData));
      if (searchData && searchData.time && searchData.time.length) {
        searchData.startTime = searchData.time[0];
        searchData.endTime = searchData.time[1];
      } else {
        searchData.startTime = null;
        searchData.endTime = null;
      }
      delete searchData.time;
      let params = {
        ...this.pages,
        ...searchData,
      };
      this.loading = true;
      recommendDpage(params).then((res) => {
        if (res && res.code === 200) {
          this.tableData = res.data.records || [];
          this.pages.total = res.data.total;
          this.loading = false;
          this.selectList = [];
          //   if (this.$refs && this.$refs.tableWrapper) {
          //     this.$refs.tableWrapper.$refs.table.clearSelection();
          //   }
        } 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);
}
.orange {
  width: 20px;
  height: 20px;
  background: orange;
  border-radius: 50%;
  display: inline-block;
}
.green {
  width: 20px;
  height: 20px;
  background: green;
  border-radius: 50%;
  display: inline-block;
}
</style>
