<template>
  <div>
    <!-- 搜索 -->
    <div ref="searchBox" class="mt10">
      <Search :searchArr="searchConfiger" @submit="onSearch">
        <el-button type="primary" size="small" @click="handleAdd('add', null)"
          >添加规则</el-button
        >
        <el-button type="danger" size="small" @click="handleRow()"
          >批量删除</el-button
        >
      </Search>
    </div>
    <!-- 按钮组 -->
    <div class="content">
      <!-- 表格 -->
      <div class="margTop">
        <MyTable
          :tableData="pageData.tableData"
          :tableColumn="pageData.tableColumn"
          :tableHeight="pageData.tableHeight"
          :key="pageData.keyCount"
          :tableConfiger="pageData.tableConfiger"
          :controlsWidth="pageData.controlsWidth"
          @chooseData="handleChooseData"
        >
          <template #jqdjdm="{ row }">
            <DictTag
              :options="D_BZ_JQDJ"
              :tag="false"
              :value="row.jqdjdm"
            ></DictTag>
          </template>
          <template #jqzldm="{ row }">
            {{ getJqName(row.jqzldm) }}
          </template>
          <!-- 操作 -->
          <template #controls="{ row }">
            <el-link type="primary" size="small" @click="handleGljq(row)"
              >关联警情</el-link
            >
            <el-link type="primary" size="small" @click="handleAdd('edit', row)"
              >编辑</el-link
            >
            <el-link
              type="primary"
              size="small"
              @click="handleAdd('detail', row)"
              >详情</el-link
            >
            <el-link type="danger" size="small" @click="handleRow(row.id)"
              >删除</el-link
            >
          </template>
        </MyTable>
        <Pages
          @changeNo="changeNo"
          @changeSize="changeSize"
          :tableHeight="pageData.tableHeight"
          :pageConfiger="{
            ...pageData.pageConfiger,
            total: pageData.total
          }"
        ></Pages>
      </div>
    </div>
  </div>
  <AddForm ref="addForm" @getList="getList" :dict="{ D_BZ_JQDJ, jqTree }" />
  <GljqLod v-model="gljqShow" :dict="{ D_GS_BQ_DJ, JQLB }" :zdsjLod="zdsjLod" />
</template>
<script setup>
import MyTable from "@/components/aboutTable/MyTable.vue";
import Pages from "@/components/aboutTable/Pages.vue";
import Search from "@/components/aboutTable/Search.vue";
import { qcckGet, qcckPost, qcckDelete } from "@/api/qcckApi.js";
import { reactive, ref, onMounted, getCurrentInstance } from "vue";
import GljqLod from "./gljqLod.vue";
import AddForm from "./addForm.vue";
const { proxy } = getCurrentInstance();
const { D_BZ_JQDJ, D_GS_BQ_DJ, JQLB } = proxy.$dict(
  "D_BZ_JQDJ",
  "D_GS_BQ_DJ",
  "JQLB"
);
const searchBox = ref(); //搜索框
const searchConfiger = ref([
  {
    label: "内容关键字",
    prop: "jqgjz",
    placeholder: "请输入警情内容关键字",
    showType: "input"
  },
  {
    label: "监测时间",
    prop: "startTime",
    placeholder: "请选择监测时间",
    showType: "datetimerange"
  },
  {
    label: "警情等级",
    prop: "jqdjdm",
    placeholder: "请选择警情等级",
    showType: "select",
    options: D_BZ_JQDJ
  }
]);
const queryFrom = ref({});
const pageData = reactive({
  tableData: [], //表格数据
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "checkbox", //选择类型
    loading: false
  },
  total: 0,
  pageConfiger: {
    pageSize: 20,
    pageCurrent: 1
  }, //分页
  controlsWidth: 250, //操作栏宽度
  tableColumn: [
    { label: "规则描述", prop: "gzms", showOverflowTooltip: true },
    { label: "内容关键字", prop: "jqgjz", showOverflowTooltip: true },
    {
      label: "警情等级",
      prop: "jqdjdm",
      showOverflowTooltip: true,
      showSolt: true
    },
    {
      label: "警情类型",
      prop: "jqzldm",
      showOverflowTooltip: true,
      showSolt: true
    },
    { label: "录入人姓名", prop: "lrrXm", showOverflowTooltip: true },
    { label: "录入人身份证号", prop: "lrrSfzh", showOverflowTooltip: true },
    { label: "所属部门", prop: "ssbm" }
  ]
});

onMounted(() => {
  getJqTree();
  getList();
  tabHeightFn();
});

// 搜索
const onSearch = (val) => {
  queryFrom.value = {
    ...val,
    kssj: val.startTime ? val.startTime[0] : "",
    jssj: val.startTime ? val.startTime[1] : ""
  };
  pageData.pageConfiger.pageCurrent = 1;
  getList();
};

const changeNo = (val) => {
  pageData.pageConfiger.pageCurrent = val;
  getList();
};
const changeSize = (val) => {
  pageData.pageConfiger.pageSize = val;
  getList();
};

// 获取列表
const getList = () => {
  const promes = {
    ...pageData.pageConfiger,
    ...queryFrom.value
  };
  qcckGet(promes, "/mosty-gsxt/jqjczg/getPageList")
    .then((res) => {
      pageData.tableData = res.records || [];
      pageData.total = res.total;
      pageData.tableConfiger.loading = false;
    })
    .catch(() => {
      pageData.tableConfiger.loading = false;
    });
};
// 获取警情树

const jqTree = ref([]);
const getJqTree = () => {
  qcckGet({}, "/mosty-gsxt/lzJcjPjdb/getDictTree").then((res) => {
    jqTree.value = res;
  });
};

// 根据值获取警情名称
const getJqName = (value) => {
  if (!value || !jqTree.value || jqTree.value.length === 0) {
    return value || "";
  }

  const findName = (tree, targetValue) => {
    for (const node of tree) {
      if (node.dm === targetValue) {
        return node.zdmc;
      }
      if (node.itemList && node.itemList.length > 0) {
        const result = findName(node.itemList, targetValue);
        if (result) {
          return result;
        }
      }
    }
    return null;
  };

  const name = findName(jqTree.value, value);
  return name || value;
};
// 删除
const ids = ref([]);
const handleChooseData = (val) => {
  ids.value = val.map((item) => item.id);
};
const handleRow = (id) => {
  const promes = {
    ids: id ? [id] : ids.value
  };
  proxy.$confirm("确定要删除？", "警告", { type: "warning" }).then(() => {
    qcckDelete(promes, "/mosty-gsxt/jqjczg/deleteEntity").then(() => {
      proxy.$message({ type: "success", message: "删除成功" });
      getList();
    });
  });
};
// 值班操作
const addForm = ref(null);
const handleAdd = (type, row) => {
  addForm.value.init(type, row);
};
// 关联警情
const gljqShow = ref(false);
const zdsjLod = ref();
const handleGljq = (row) => {
  gljqShow.value = true;
  zdsjLod.value = row;
};

// 表格高度计算
const tabHeightFn = () => {
  pageData.tableHeight =
    window.innerHeight - searchBox.value.offsetHeight - 220;
  window.onresize = function () {
    tabHeightFn();
  };
};
</script>

<style lang="scss" scoped>
.el-loading-mask {
  background: rgba(0, 0, 0, 0.5) !important;
}
</style>
