<template>
  <div>
    <div class="titleBox">
      <div class="title">巡防装备分类管理</div>
      <div class="btnBox">
        <el-button type="primary" @click="addLoad">
          <el-icon style="vertical-align: middle">
            <CirclePlus />
          </el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>
        <el-button typeof="danger">
          <el-icon style="vertical-align: middle">
            <Delete />
          </el-icon>
          <span style="vertical-align: middle">批量注销</span>
        </el-button>
      </div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="一级分类">
          <el-select
            @change="changeTree"
            v-model="listQuery.pcode"
            class="m-2"
            placeholder="请选择"
          >
            <el-option
              v-for="item in zbTree"
              :key="item.scode"
              :label="item.sname"
              :value="item.scode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="listQuery.scode" class="m-2" placeholder="请选择">
            <el-option
              v-for="item in ejTree"
              :key="item.scode"
              :label="item.sname"
              :value="item.scode"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleFilter"> 查询 </el-button>
          <el-button @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabBox">
      <el-table
        :data="tableData"
        border
        ref="dataTreeList"
        row-key="id"
        :tree-props="{ children: 'itemList', hasChildren: true }"
        :height="tableHeight"
        :key="keyCount"
      >
        <el-table-column fixed align="center" type="selection" width="55" />
        <el-table-column type="index" align="center" width="60px" label="序号">
        </el-table-column>
        <el-table-column
          sortable
          prop="pname"
          show-overflow-tooltip
          align="center"
          label="一级分类"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="sname"
          show-overflow-tooltip
          align="center"
          label="二级分类"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="count"
          show-overflow-tooltip
          align="center"
          label="数量"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          width="200px"
        >
          <template #default="{ row }">
            <el-button @click="update(row)" size="small">修改</el-button>
            <el-popconfirm
              confirm-button-text="是"
              cancel-button-text="否"
              icon-color="red"
              title="确定要注销?"
              @confirm="delDictItem(row.id)"
            >
              <template #reference>
                <el-button type="danger" size="small">注销</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.pageNo"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <div v-if="dialog" class="dialog">
      <div class="head_box">
        <span class="title">{{ diaTitle }}</span>
        <div>
          <el-button
            type="primary"
            :loading="btnLoading"
            size="small"
            @click="submit"
            >保存</el-button
          >
          <el-button size="small" @click="dialog = false">关闭</el-button>
        </div>
      </div>
      <el-form
        ref="elform"
        :model="form"
        :rules="rules"
        :inline="true"
        label-position="top"
      >
        <!-- <el-form-item prop="pcode" label="装备分类编号">
          <el-input
            v-model="form.pcode"
            placeholder="请输入装备分类编号"
            clearable
            style="width: 100%"
          />
        </el-form-item> -->
        <el-form-item prop="pname" label="一级分类名称">
          <el-input
            v-model="form.pname"
            placeholder="请输入一级分类名称"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item prop="sname" label="二级分类名称">
          <el-input
            v-model="form.sname"
            placeholder="请输入二级分类名称"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="备注" style="width: 100%">
          <el-input
            v-model="form.bz"
            placeholder="请输入关键字"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  getTree,
  addTpjcglzbfl,
  getZbfllist,
  upZbfl,
  delZbfl,
  infoZbfl
} from "@/api/basicsmanage/watchmanZbgl";
import { ref, reactive, onMounted, getCurrentInstance,onUnmounted } from "vue";
const listQuery = ref({
  pageNo: 1,
  pageSize: 20,
  pcode: "",
  scode: ""
});
const { proxy } = getCurrentInstance();
const rules = ref({
  // pcode: [{ required: true, message: "请输入分类编码", trigger: "change" }],
  pname: [{ required: true, message: "请输入一级分类名称", trigger: "change" }],
  sname: [{ required: true, message: "请输入二级分类名称", trigger: "change" }]
});
const zbTree = ref([]);
const keyCount = ref(0); //tabel组件刷新值
const ejTree = ref([]);
const diaTitle = ref();
//获取装备树
function getZbtree() {
  getTree().then((res) => {
    zbTree.value = res;
  });
}
onMounted(() => {
  getZbtree();
  getListData();
  tabHeightFn();
  window.onresize = function () {
    tabHeightFn();
  };
  proxy.mittBus.on("mittFn", (data) => {
    keyCount.value = data;
  });
});
onUnmounted(() => {
  proxy.mittBus.off("mittFn");
});
const btnLoading = ref(false);
const searchBox = ref(null); // 搜索盒子
const tableHeight = ref(); // 表格高度
const tableData = ref([]); //表单数据
const chackadd = ref(true); //切换新增修改
const dialog = ref(false); //是否显示新增弹窗
const total = ref(0); //总数据
const elform = ref(null);
const form = ref({
  sname: "",
  pname: "",
  bz: ""
});
const handleFilter = () => {
  listQuery.value.pageNo = 1;
  getListData();
};
function changeTree(code) {
  listQuery.value.scode = "";
  let arr = zbTree.value;
  arr.forEach((v) => {
    if (v.scode == listQuery.value.pcode) {
      ejTree.value = v.children;
    }
  });
}
//获取数据
const getListData = () => {
  const params = listQuery.value;
  getZbfllist(params).then((res) => {
    tableData.value = res.records;
    total.value = res.total;
  })
};
const reset = () => {
  listQuery.value = {
    pageNo: 1,
    pageSize: 20,
    pcode: "",
    scode: ""
  };
  getListData();
};
//重置表单
function formReset() {
  form.value = {};
}
function addLoad() {
  formReset();
  chackadd.value = false;
  diaTitle.value = "新增装备分类";
  dialog.value = true;
}
const addDict = (row) => {
  isEdit.value = false;
  dialogForm.value = {};
  dialog.value = true;
};
function update(row) {
  form.value.sname = row.sname;
  form.value.pname = row.pname;
  form.value.id = row.id;
  dialog.value = true;
  chackadd.value = true;
  diaTitle.value = "装备分类修改";
  // infoZbfl(id).then((res) => {
  //   dialog.value = true;
  //   chackadd.value = true;
  //   diaTitle.value = "装备分类修改";
  //   form.value = res;
  // });
}
//提交
function submit() {
  elform.value.validate((valid) => {
    if (valid) {
      btnLoading.value = true;
      setTimeout(() => {
        btnLoading.value = false;
      }, 1500);
      let data = {};
      const keys = Object.keys(form.value);
      keys.map((item) => {
        if (form.value[item] != null) {
          data[item] = form.value[item];
        }
      });
      if (chackadd.value) {
        upZbfl(data).then(() => {
          proxy.$message({
            type: "success",
            message: "修改成功"
          });
          dialog.value = false;
          getListData();
        });
      } else {
        addTpjcglzbfl(data).then(() => {
          proxy.$message({
            type: "success",
            message: "新增成功"
          });
          dialog.value = false;
          getListData();
        });
      }
    }
  });
}
//删除
function delDictItem(id) {
  const ids = [id];
  delZbfl(ids).then(() => {
    proxy.$message({
      type: "success",
      message: "删除成功"
    });
    getListData();
  });
}
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  getListData();
};

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageNo = currentPage;
  getListData();
};
// 表格高度计算
const tabHeightFn = () => {
 tableHeight.value = window.innerHeight - searchBox.value.offsetHeight - 240;
};
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
</style>
