<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
          @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.bqmc"
            placeholder="请输入经验名称"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="handleFilter"> 查询 </el-button>
          <el-button type="info" @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabBox">
      <el-table
        :data="tableData"
        border
        @selection-change="handleSelectionChange"
        row-key="id"
        :tree-props="{ children: 'itemList', hasChildren: true }"
        style="width: 100%"
        :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="150px"
          label="经验编号"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="bqmc"
          show-overflow-tooltip
          align="center"
          width="350px"
          label="经验名称"
        >
        </el-table-column>
        <el-table-column sortable 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 prop="mxId" show-overflow-tooltip label="计算模型" align="center" width="200px">
                </el-table-column> -->
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          width="200px"
        >
          <template #default="{ row }">
            <!-- <el-button size="small" @click="setInfo(row.id)">详情</el-button> -->
            <el-button @click="update(row.id)" size="small">修改</el-button>
            <el-button @click="delDictItem(row.id)" 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.pageCurrent"
          :page-sizes="[10, 20, 50]"
          :page-size="form.pageSize"
          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">{{ diaTitle }}</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"
        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"
            :readonly="disable"
            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 {
  getTbJcglBq,
  addTbJcglBq,
  putTbJcglBq,
  infoTbJcglBq,
  delTbJcglBq,
  deleteTbJcglBq
} from "@/api/basicsmanage/experience";
import { getTbJcglXfll } from "@/api/xfll";
import { ref, reactive, onMounted, getCurrentInstance, onUnmounted } from "vue";
const { proxy } = getCurrentInstance();
const xfllActive = ref(false);
const ids = ref([]); //批量注销的ID
const activeName = ref("mj");
//巡防力量数据
const xfllDtata = ref([]);
//巡防力量查询参数
const xfllQuery = ref({
  current: 1,
  size: 20,
  fl: "01"
});
const xfllTotal = ref(0);
const tableData = ref([]); //表单数据
const total = ref(0); //总数据
const dialogFormVisible = ref(false); //是否显示新增弹窗
const elform = ref({}); //表单对象
const loadingTable = ref(true);
const chackadd = ref(true); //切换新增修改
const disable = ref(false);
const searchBox = ref(null); // 搜索盒子
const diaTitle = ref("新增经验管理");
const isDisabled = ref(false); //是否警用保存按钮
const loading = ref(false); //按钮是否显示加载
const keyCount = ref(0); //tabel组件刷新值
//赛选条件参数
const form = ref({
  pageCurrent: 1,
  pageSize: 20,
  bqmc: "",
  bqlx: "EXP"
});
//新增表单参数
const addForm = ref({
  id: "",
  bqbh: "",
  bqmc: "",
  bqlx: "EXP"
});

function addLoad() {
  xfllActive.value = false;
  addForm.value = {
    bqbh: "JY" + repairZero(total.value + 1),
    bqmc: "",
    bqlx: "EXP"
  };
  isDisabled.value = false;
  disable.value = false;
  chackadd.value = true;
  diaTitle.value = "新增经验管理";
  dialogFormVisible.value = true;
}
//表单验证
const rules = reactive({
  bqmc: [
    {
      required: true,
      message: "请输入经验名称"
    }
  ]
  // mxId: [
  //   {
  //     required: true
  //   }
  // ]
});
const tableHeight = ref(); // 表格高度
//批量数据
const handleSelectionChange = (val) => {
  ids.value = [];
  if (val) {
    val.forEach((item) => {
      ids.value.push(item.id);
    });
  }
};
//批量注销数据
const batchDelete = () => {
  proxy
    .$confirm("确定要注销", "警告", {
      type: "warning"
    })
    .then(() => {
      deleteTbJcglBq(ids.value).then((res) => {
        proxy.$message({
          type: "success",
          message: "注销成功"
        });
        getListData();
      });
    })
    .catch(() => {
      proxy.$message.info("已取消");
    });
};
onMounted(() => {
  tabHeightFn();
  getListData();
  window.onresize = function () {
    tabHeightFn();
  };
  proxy.mittBus.on("mittFn", (data) => {
    keyCount.value = data;
  });
});
onUnmounted(() => {
  proxy.mittBus.off("mittFn");
});
//补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 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 handleSizeChange(e) {
  form.value.pageSize = e;
  form.value.pageCurrent = 1;
  getListData();
}
function handleClick() {
  if (activeName.value == "mj") {
    xfllQuery.value.fl = "01";
    getXfll();
  } else {
    xfllQuery.value.fl = "02";
    getXfll();
  }
}
//分页查询
function handleCurrentChange(e) {
  form.value.pageCurrent = e;
  getListData();
}
//查看详情
function setInfo(id) {
  infoTbJcglBq(id).then((res) => {
    disable.value = true;
    diaTitle.value = res.bqmc + "经验详情";
    addForm.value = res;
    dialogFormVisible.value = true;
    isDisabled.value = true;
  });
}
//修改
function update(id) {
  infoTbJcglBq(id).then((res) => {
    disable.value = false;
    isDisabled.value = false;
    chackadd.value = false;
    diaTitle.value = res.bqmc + "经验修改";
    addForm.value = res;
    dialogFormVisible.value = true;
  });
  if (e.xfllSl == 0) {
    xfllActive.value = false;
  } else {
    xfllActive.value = true;
    xfllQuery.value.bqId = id;
    getXfll();
  }
}
function info(row) {
  infoTbJcglBq(row.id).then((res) => {
    disable.value = false;
    isDisabled.value = false;
    chackadd.value = false;
    diaTitle.value = res.bqmc + "经验修改";
    addForm.value = res;
    dialogFormVisible.value = true;
  });
  if (row.xfllSl == 0) {
    xfllActive.value = false;
  } else {
    xfllActive.value = true;
    xfllQuery.value.bqId = row.id;
    getXfll();
  }
}
//新增提交
function submit() {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
  }, 1.5e3);
  elform.value.validate((valid) => {
    if (valid) {
      let data = addForm.value;
      if (!chackadd.value) {
        putTbJcglBq(data).then((res) => {
          proxy.$message({
            type: "success",
            message: "修改成功"
          });
          dialogFormVisible.value = false;
          getListData();
        });
      } else {
        addTbJcglBq(data).then(() => {
          proxy.$message({
            type: "success",
            message: "新增成功"
          });
          dialogFormVisible.value = false;
          getListData();
        });
      }
    }
  });
}
//删除
function delDictItem(id) {
  // 删除数据用
  // delTbJcglBq(id).then((res) => {
  proxy
    .$confirm("确定要注销", "警告", {
      type: "warning"
    })
    .then(() => {
      deleteTbJcglBq([id]).then(() => {
        proxy.$message({
          type: "success",
          message: "注销成功"
        });
        getListData();
      });
    })
    .catch(() => {
      proxy.$message.info("已取消");
    });
}
//更改查询条数xfll
function handleSizeChangexf(e) {
  activeName.size = e;
  xfllQuery.value.current = 1;
  getXfll();
}
//分页查询xfll
function handleCurrentChangexf(e) {
  xfllQuery.value.current = e;
  getListData();
  getXfll();
}
//点击查询
const handleFilter = () => {
  form.value.pageCurrent = 1;
  getListData();
};
//获取数据
const getListData = () => {
  loadingTable.value = true;
  const params = form.value;
  getTbJcglBq(params).then((res) => {
    tableData.value = res.records;
    total.value = res.total;
    loadingTable.value = false;
  }).catch(()=>{
    loadingTable.value = false;
  });
};
//重置查询条件
const reset = () => {
  form.value = {
    pageCurrent: 1,
    pageSize: 20,
    bqmc: "",
    bqlx: "EXP"
  };
  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>
