<template>
  <div>
    <div class="titleBox">
      <div class="title">专业技能管理</div>
      <div class="btnBox">
        <el-button type="primary" @click="add">
          <el-icon style="vertical-align: middle">
            <CirclePlus />
          </el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>

        <el-button
          @click="batchDelete"
          :disabled="ids.length == 0"
          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="form" :inline="true">
        <el-form-item label="技能名称">
          <el-input
            v-model="form.textName"
            placeholder="请输入技能名称"
            clearable
            style="width: 100%"
          />
        </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
        row-key="id"
        :tree-props="{ children: 'itemList', hasChildren: true }"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        :height="tableHeight"
        :key="keyCount"
        v-loading="loadingTable"
        element-loading-background="rgba(0,0,0,0.3)"
        element-loading-text="数据加载中。。"
      >
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column
          label="序号"
          type="index"
          align="center"
          sortable
          width="80"
        />
        <el-table-column
          sortable
          prop="bqbh"
          show-overflow-tooltip
          align="center"
          width="250px"
          label="技能编号"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="bqmc"
          show-overflow-tooltip
          align="center"
          width="350px"
          label="技能名称"
        >
        </el-table-column>
        <el-table-column label="技能人数" align="center">
          <template #default="{ row }">
            <span class="textBtn" @click="info(row)">{{ row.xfllSl }}</span>
          </template>
        </el-table-column>
        <el-table-column width="150" label="数据状态" align="center">
          <template #default="{ row }">
            <el-tag size="small" type="success" v-if="row.xtSjzt != 0"
              >正常</el-tag
            >
            <el-tag size="small" type="danger" v-else>注销</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          width="200px"
        >
          <template #default="{ row }">
            <el-button @click="update(row, 'detail')" size="small"
              >详情</el-button
            >
            <el-button
              @click="update(row, 'edit')"
              size="small"
              v-if="row.xtSjzt == 1"
              >修改</el-button
            >
            <el-button
              @click="delDictItem(row)"
              v-if="row.xtSjzt == 1"
              type="danger"
              size="small"
              >注销</el-button
            >
          </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="form.current"
          :page-sizes="[10, 20, 50]"
          :page-size="form.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <div v-if="dialogFormVisible" class="dialog">
      <div class="head_box">
        <span class="title">{{ title }}</span>
        <div>
          <el-button
            type="primary"
            size="small"
            @click="submit"
            v-if="!isDisabled"
            :loading="loading"
            >保存</el-button
          >
          <el-button size="small" @click="dialogFormVisible = false"
            >关闭</el-button
          >
        </div>
      </div>
      <el-form
        ref="elform"
        :rules="rules"
        :model="addForm"
        :inline="true"
        :disabled="isDisabled"
        label-position="top"
      >
        <el-form-item label="技能编号" prop="bqbh">
          <el-input
            readonly
            v-model="addForm.bqbh"
            placeholder="请输入技能编号"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="技能名称" prop="bqmc">
          <el-input
            v-model="addForm.bqmc"
            placeholder="请输入技能名称"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <div style="width: 100%" v-if="xfllActive">
          <el-tabs
            style="width: 100%"
            v-model="activeName"
            type="card"
            class="demo-tabs"
            @tab-click="handleClick"
          >
            <el-tab-pane label="民警" name="mj"></el-tab-pane>
            <el-tab-pane label="辅警" name="fj"></el-tab-pane>
          </el-tabs>
          <el-table
            :data="xfllDtata"
            border
            row-key="id"
            :tree-props="{ children: 'itemList', hasChildren: true }"
            style="width: 100%"
            height="400px"
            :key="keyCount"
          >
            <el-table-column
              label="序号"
              type="index"
              align="center"
              sortable
              width="80"
            />
            <el-table-column
              sortable
              prop="xm"
              show-overflow-tooltip
              align="center"
              :label="activeName == 'mj' ? '民警姓名' : '辅警姓名'"
            >
            </el-table-column>
            <el-table-column
              sortable
              prop="sfzh"
              show-overflow-tooltip
              align="center"
              label="身份证号码"
            >
            </el-table-column>
            <el-table-column
              sortable
              prop="ssbm"
              label="所属部门"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              prop="jh"
              label="警号"
              align="center"
            ></el-table-column>
          </el-table>
          <el-pagination
            class="pagination"
            @size-change="handleSizeChangexf"
            @current-change="handleCurrentChangexf"
            :current-page="xfllQuery.current"
            :page-sizes="[10, 20, 50]"
            :page-size="xfllQuery.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="xfllTotal"
          >
          </el-pagination>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import PEO from "@/assets/images/layout/body-bg.png";
import { ref, reactive, onMounted, onUnmounted, getCurrentInstance } from "vue";
import { ElMessage } from "element-plus";
import {
  getTbJcglBq,
  getTbJcglXfll,
  addTbJcglBq,
  deleteTbJcglBq,
  getTbJcglBqInfo,
  updateTbJcglBqInfo
} from "@/api/xfll";
import { delTbJcglBq } from "@/api/basicsmanage/experience";
const xfllActive = ref(false);
const activeName = ref("mj");
const { proxy } = getCurrentInstance();
const searchBox = ref(null); // 搜索盒子
const loadingTable = ref(true);
const tableHeight = ref(); // 表格高度
const ids = ref([]); //批量注销的ID
const title = ref("新增专业技能");
//巡防力量数据
const xfllDtata = ref([]);
//巡防力量查询参数
const xfllQuery = ref({
  current: 1,
  size: 20,
  fl: "01"
});
const xfllTotal = ref(0);
//查询参数
const form = ref({
  current: 1,
  size: 20,
  textName: ""
});
//新增表单参数
const addForm = ref({
  id: "",
  bqbh: "",
  bqmc: ""
});
//表单验证
const rules = reactive({
  bqmc: [
    {
      required: true,
      message: "请输入技能名称"
    }
  ]
});
const tableData = ref([]); //表单数据
const total = ref(0); //总数据
const dialogFormVisible = ref(false); //是否打开新增弹窗
const elform = ref({}); //表当对象
const isDisabled = ref(false); //是否警用保存按钮
const loading = ref(false); //是否显示按钮加载
const checkAdd = ref(false);
const keyCount = ref(0); //tabel组件刷新值
onMounted(() => {
  getListData();
  tabHeightFn();
  window.onresize = function () {
    tabHeightFn();
  };
  proxy.mittBus.on("mittFn", (data) => {
    keyCount.value = data;
  });
});
onUnmounted(() => {
  proxy.mittBus.off("mittFn");
});
function handleClick() {
  if (activeName.value == "mj") {
    xfllQuery.value.fl = "01";
    getXfll();
  } else {
    xfllQuery.value.fl = "02";
    getXfll();
  }
}
//获取巡防力量数据
function getXfll() {
  let data = {
    fl: xfllQuery.value.fl,
    pageSize: xfllQuery.value.size,
    pageCurrent: xfllQuery.value.current,
    bqId: xfllQuery.value.bqId || ""
  };
  getTbJcglXfll(data).then((res) => {
    xfllDtata.value = res.records;
    xfllTotal.value = res.total;
  });
}
//查看详情
function setInfo(e) {
  title.value = "专业技能技能详情";
  getTbJcglBqInfo([e.id]).then((res) => {
    addForm.value = res;
    dialogFormVisible.value = true;
    isDisabled.value = true;
  });
}
//批量注销数据
const batchDelete = () => {
  proxy
    .$confirm("确定要注销", "警告", {
      type: "warning"
    })
    .then(() => {
      deleteTbJcglBq(ids.value).then((res) => {
        ElMessage({
          message: "注销成功",
          type: "success"
        });
        getListData();
      });
    })
    .catch(() => {
      proxy.$message.info("已取消");
    });
};
//补0函数
function repairZero(num) {
  let data = num;
  if (1 <= num < 10) {
    data = "000" + num;
  } else if (10 <= num < 100) {
    data = "00" + num;
  } else if (100 <= num < 1000) {
    data = "0" + num;
  }
  return data;
}
function add() {
  xfllActive.value = false;
  isDisabled.value = false;
  checkAdd.value = true;
  addForm.value = {
    bqbh: "JN" + repairZero(total.value + 1),
    bqmc: "",
    bqlx: "SKL"
  };
  title.value = "新增专业技能";
  dialogFormVisible.value = true;
}
//批量数据
const handleSelectionChange = (val) => {
  ids.value = [];
  if (val) {
    val.forEach((item) => {
      ids.value.push(item.id);
    });
  }
};
//新增提交
function submit() {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
  }, 1.5e3);
  elform.value.validate((valid) => {
    if (valid) {
      let data = addForm.value;
      if (!checkAdd.value) {
        updateTbJcglBqInfo(data).then((res) => {
          ElMessage({
            message: "修改成功",
            type: "success"
          });
          getListData();
        });
      } else {
        addTbJcglBq(data).then((res) => {
          ElMessage({
            message: "添加成功",
            type: "success"
          });
          getListData();
        });
      }
      dialogFormVisible.value = false;
    }
  });
}
//点击查询
const handleFilter = () => {
  form.value.current = 1;
  getListData();
};
//获取数据
const getListData = () => {
  loadingTable.value = true;
  let data = {
    pageSize: form.value.size,
    pageCurrent: form.value.current,
    bqmc: form.value.textName,
    bqlx: "SKL"
  };
  getTbJcglBq(data)
    .then((res) => {
      tableData.value = res.records;
      total.value = res.total;
      loadingTable.value = false;
    })
    .catch(() => {
      loadingTable.value = false;
    });
};
//更改查询条数
function handleSizeChange(e) {
  form.value.size = e;
  form.value.current = 1;
  getListData();
}
//分页查询
function handleCurrentChange(e) {
  xfllQuery.value.current = e;
}
//更改查询条数xfll
function handleSizeChangexf(e) {
  activeName.size = e;
  xfllQuery.value.current = 1;
  getXfll();
}
//分页查询xfll
function handleCurrentChangexf(e) {
  xfllQuery.value.current = e;
  getListData();
  getXfll();
}
//修改技能
function update(e, type) {
  isDisabled.value = type == "edit" ? false : true;
  getTbJcglBqInfo(e.id).then((res) => {
    addForm.value = res;
    activeName.value = "mj";
    xfllQuery.value.fl = "01";
    title.value = type == "edit" ? "专业技能修改" : "专业技能详情";
    checkAdd.value = false;
    dialogFormVisible.value = true;
  });
  if (e.xfllSl == 0) {
    xfllActive.value = false;
  } else {
    xfllActive.value = true;
    xfllQuery.value.bqId = e.id;
    getXfll();
  }
}
function info(row) {
  getTbJcglBqInfo(row.id).then((res) => {
    addForm.value = res;
    activeName.value = "mj";
    xfllQuery.value.fl = "01";

    title.value = "专业技能详情";
    checkAdd.value = false;
    isDisabled.value = true;
    dialogFormVisible.value = true;
  });
  if (row.xfllSl == 0) {
    xfllActive.value = false;
  } else {
    xfllActive.value = true;
    xfllQuery.value.bqId = row.id;
    getXfll();
  }
}
//注销技能
function delDictItem(e) {
  // 注销数据用
  // delTbJcglBq(e.id).then((res) => {
  proxy
    .$confirm("确定要注销", "警告", {
      type: "warning"
    })
    .then(() => {
      deleteTbJcglBq([e.id]).then((res) => {
        ElMessage({
          message: "注销成功",
          type: "success"
        });
        getListData();
      });
    })
    .catch(() => {
      proxy.$message.info("已取消");
    });
}
//点击重置
const reset = () => {
  form.value = {
    current: 1,
    size: 20,
    textName: ""
  };
  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";
::v-deep .el-tabs--card > .el-tabs__header {
  border-bottom: 1px solid #03438b;
}
::v-deep .el-tabs--card > .el-tabs__header .el-tabs__item.is-active {
  border-bottom-color: #03438b;
}
::v-deep .el-tabs--card > .el-tabs__header .el-tabs__nav {
  border: 1px solid #03438b;
  border-bottom: none;
  border-radius: 4px 4px 0 0;
  box-sizing: border-box;
}
::v-deep .el-tabs--card > .el-tabs__header .el-tabs__item {
  border-left: 1px solid #234c9e;
}
.textBtn {
  cursor: pointer;
}
.textBtn:hover {
  color: #2a5dc2;
}
</style>
