<template>
  <div>
    <!-- 搜索 -->
    <div ref="searchBox" class="mt10">
      <Search :searchArr="searchConfiger" @submit="onSearch">
        <el-button type="primary" size="small" @click="addEdit('add', '')">
          <el-icon style="vertical-align: middle">
            <CirclePlus />
          </el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>
      </Search>
    </div>

    <!-- 表格 -->
    <div class="margTop" style="padding: 0">
      <WarnDataTable
        :data="pageData.tableData"
        :columns="pageData.tableColumn"
        :tableHeight="pageData.tableHeight"
        :loading="pageData.tableConfiger.loading"
        @selection-change="chooseData"
      >
        <template #bqList="{ row }">
          <ul>
            <li
              :style="{ background: Bqys(item.bqYs) }"
              class="one_text_detail marks mb4"
              :key="index"
              v-for="(item, index) in row.bqList"
            >
              {{ item.bqMc }}({{ item.bqFz || 0 }} 分)
            </li>
          </ul>
        </template>
        <template #ryXb="{ row }">
          <DictTag :tag="false" :value="row.ryXb" :options="D_BZ_XB" />
        </template>
        <template #ryJg="{ row }">
          <DictTag :tag="false" :value="row.ryJg" :options="D_BZ_XZQHDM" />
        </template>
        <template #ryMz="{ row }">
          <DictTag :tag="false" :value="row.ryMz" :options="D_BZ_MZ" />
        </template>
        <template #hjdQh="{ row }">
          <DictTag :tag="false" :value="row.hjdQh" :options="D_BZ_XZQHDM" />
        </template>
        <template #zdrRyjb="{ row }">
          <DictTag :tag="false" :value="row.zdrRyjb" :options="D_GS_ZDR_RYJB" />
        </template>
        <template #zdrBkZt="{ row }">
          <DictTag :tag="false" :value="row.zdrBkZt" :options="D_BZ_RCBKZT" />
        </template>
        <template #zdrCzzt="{ row }">
          <DictTag :tag="false" :value="row.zdrCzzt" :options="D_GS_ZDR_CZZT" />
        </template>
        <template #zdrZt="{ row }">
          <DictTag :tag="false" :value="row.zdrZt" :options="D_GS_ZDQT_ZT" />
        </template>
        <template #zdrSsjz="{ row }">
          <DictTag :tag="false" :value="row.zdrSsjz" :options="D_GS_BK_SSJZ" />
        </template>
        <template #zdrSjjz="{ row }">
          <DictTag :tag="false" :value="row.zdrSjjz" :options="D_GS_BK_SSJZ" />
        </template>

        <template #xtSjzt="{ row }">
          <div>
            {{ row.xtSjzt == 0 ? "注销" : row.xtSjzt == 1 ? "正常" : "封存" }}
          </div>
        </template>

        <!-- 操作 -->
        <template #controls="{ row }">
          <!-- <el-link size="small" type="success" @click="handleremove(row.id)">移至关注库</el-link> -->
          <el-link
            size="small"
            type="success"
            @click="handleMoveToFocus(row.id)"
            >移入关注库</el-link
          >
          <el-link size="small" type="success" @click="handleremove(row.id)"
            >移入基础库</el-link
          >
          <el-link
            size="small"
            type="success"
            v-if="row.zdrZt == '01' || row.zdrZt == '03'"
            @click="handleSend(row.id)"
            >送审</el-link
          >
          <el-link
            size="small"
            type="primary"
            v-if="row.zdrZt == '01' || row.zdrZt == '03'"
            @click="addEdit('edit', row)"
            >编辑</el-link
          >
          <el-link size="small" type="primary" @click="addEdit('detail', row)"
            >详情</el-link
          >
          <el-link size="small" type="danger" @click="deleteRow(row.id)"
            >删除</el-link
          >
        </template>
      </WarnDataTable>
      <Pages
        @changeNo="changeNo"
        @changeSize="changeSize"
        :tableHeight="pageData.tableHeight"
        :pageConfiger="{
          ...pageData.pageConfiger,
          total: pageData.total
        }"
      ></Pages>
    </div>
    <!-- 详情 -->
    <AddForm
      ref="addFormDiloag"
      @updateDate="getList"
      :dic="{
        D_GS_ZDR_RYJB,
        D_BZ_XB,
        D_BZ_MZ,
        D_BZ_XZQHDM,
        D_ZDRGK_GKZT,
        D_GS_ZDR_CZZT,
        D_GS_BQ_ZL,
        D_GS_BQ_LB,
        D_GS_BQ_LX,
        D_GS_ZDR_YJDJ,
        D_GS_BK_SSJZ
      }"
    />
    <!-- 选择用户 -->
    <ChooseUser
      v-model="chooseUserVisible"
      @choosedUsers="handleUserSelected"
      :roleIds="roleIds"
    />
    <!-- 转线索 -->
    <ZxsForm
      v-if="showzxs"
      ref="zxsDilof"
      @change="getList"
      :dic="{
        D_BZ_SF,
        D_BZ_XB,
        D_GS_XS_LY,
        D_BZ_SSZT,
        D_GS_XS_LX,
        D_GS_XS_QTLX
      }"
    ></ZxsForm>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import ChooseUser from "@/components/ChooseList/ChooseUser/index.vue";
import ZxsForm from "./components/zxsForm.vue";
import PageTitle from "@/components/aboutTable/PageTitle.vue";
import WarnDataTable from "@/views/backOfficeSystem/ces/components/WarnDataTable.vue";
import Pages from "@/components/aboutTable/Pages.vue";
import Search from "@/components/aboutTable/Search.vue";
import AddForm from "./components/addForm.vue";
import { qcckGet, qcckPost, qcckDelete } from "@/api/qcckApi.js";
import { reactive, ref, onMounted, getCurrentInstance, nextTick } from "vue";
import { useRouter, useRoute } from "vue-router";
import { getItem } from "@/utils/storage.js";
const router = useRouter();
const route = useRoute();
const { proxy } = getCurrentInstance();
const {
  D_GS_ZDQT_ZT,
  D_GS_ZDR_RYJB,
  D_BZ_XB,
  D_BZ_MZ,
  D_BZ_RCBKZT,
  D_BZ_XZQHDM,
  D_ZDRGK_GKZT,
  D_GS_ZDR_CZZT,
  D_GS_BQ_ZL,
  D_GS_BQ_LB,
  D_GS_BQ_LX,
  D_GS_ZDR_YJDJ,
  D_GS_BK_SSJZ,
  D_GS_BK_SQLX,
  D_BZ_SF,
  D_GS_XS_LY,
  D_BZ_SSZT,
  D_GS_XS_LX,
  D_GS_XS_QTLX
} = proxy.$dict(
  "D_GS_ZDQT_ZT",
  "D_BZ_RCBKZT",
  "D_GS_ZDR_RYJB",
  "D_BZ_XB",
  "D_BZ_MZ",
  "D_BZ_XZQHDM",
  "D_ZDRGK_GKZT",
  "D_GS_ZDR_CZZT",
  "D_GS_BQ_ZL",
  "D_GS_BQ_LB",
  "D_GS_BQ_LX",
  "D_GS_ZDR_YJDJ",
  "D_GS_BK_SSJZ",
  "D_GS_BK_SQLX",
  "D_BZ_SF",
  "D_GS_XS_LY",
  "D_BZ_SSZT",
  "D_GS_XS_LX",
  "D_GS_XS_QTLX"
);
const obj = ref({});
const showzxs = ref(false);
const zxsDilof = ref();
const show = ref(false);
const addFormDiloag = ref();
const searchBox = ref(); //搜索框
const chooseUserVisible = ref(false); //审批流程
const ids = ref([]);
const choosList = ref([]);
const visible = ref(false);
const visiblefp = ref(false);
const searchConfiger = ref([
  {
    label: "姓名",
    prop: "ryXm",
    placeholder: "请输入姓名",
    showType: "input"
  },
  {
    label: "身份证",
    prop: "rySfzh",
    placeholder: "请输入身份证",
    showType: "input"
  },
  {
    label: "户籍地",
    prop: "hjdXz",
    placeholder: "请输入户籍地",
    showType: "input"
  },
  {
    label: "人员级别",
    prop: "zdrRyjb",
    placeholder: "请输入人员级别",
    showType: "select",
    options: D_GS_ZDR_RYJB
  },
  {
    label: "所属警种",
    prop: "zdrSsjz",
    placeholder: "请选择所属警种",
    showType: "select",
    options: D_GS_BK_SSJZ
  },
  {
    label: "涉及警种",
    prop: "zdrSjjz",
    placeholder: "请选择涉及警种",
    showType: "select",
    options: D_GS_BK_SSJZ
  },
]);
const queryFrom = ref({});
const pageData = reactive({
  tableData: [],
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "checkBox",
    loading: false
  },
  total: 0,
  pageConfiger: {
    pageSize: 20,
    pageCurrent: 1
  },
  tableColumn: [
    { label: "姓名", prop: "ryXm", width: 100 },
    { label: "性别", prop: "ryXb", slotName: "ryXb", width: 80 },
    { label: "身份证", prop: "rySfzh", width: 170 },
    { label: "民族", prop: "ryMz", slotName: "ryMz", width: 80 },
    { label: "户籍派出所", prop: "hjdPcsmc" },
    {
      label: "标签",
      prop: "bqList",
      slotName: "bqList",
      showOverflowTooltip: true
    },
    { label: "协管单位", prop: "gxSsbmmc" },
    {
      label: "管控状态",
      prop: "zdrBkZt",
      showOverflowTooltip: true,
      slotName: "zdrBkZt",
      width: 100
    },
    { label: "审核状态", prop: "zdrZt", slotName: "zdrZt", width: 100 },
    { label: "入库时间", prop: "zdrRkkssj" },
    { label: "所属警种", prop: "zdrSsjz",slotName: "zdrSsjz", },
    { label: "涉及警种", prop: "zdrSjjz",slotName: "zdrSjjz", },
    {
      label: "操作",
      prop: "controls",
      slotName: "controls",
      align: "center",
      width: 350
    }
  ]
});
const isShiQzDelet = ref(false);
onMounted(() => {
  tabHeightFn();
  const isShiQz =
    getItem("roleList").find((item) => item.roleCode == "JS_777777") !=
    undefined;
  if (isShiQz) isShiQzDelet.value = true;
  if (route.query.id) {
    addEdit("x", {
      id: route.query.id
    });
  } else {
    getList();
  }
});

// 搜索
const onSearch = (val) => {
  queryFrom.value = { ...val };
  pageData.pageConfiger.pageCurrent = 1;
  getList();
};

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

// 获取列表
const getList = () => {
  pageData.tableConfiger.loading = true;
  // 人员类型D_ZDRY_RYLX(01 重点 02 普通〉
  //  rylx: '01',
  let data = { ...pageData.pageConfiger, ...queryFrom.value, rylx: "01" };
  qcckGet(data, "/mosty-gsxt/tbGsxtZdry/selectPage")
    .then((res) => {
      pageData.tableData = res.records || [];
      pageData.total = res.total;
      pageData.tableConfiger.loading = false;
    })
    .catch(() => {
      pageData.tableConfiger.loading = false;
    });
};

//送审
const handleSend = (id) => {
  proxy.$confirm("确定要送审？", "警告", { type: "warning" }).then(() => {
    qcckPost({ id }, "/mosty-gsxt/tbGsxtZdry/subSh").then(() => {
      proxy.$message({ type: "success", message: "送审成功" });
      getList();
    });
  });
};
// 移除
const handleremove = (id) => {
  proxy
    .$confirm("确定要移除此重点人员？", "警告", { type: "warning" })
    .then(() => {
      qcckPost({ id, rylx: "02" }, "/mosty-gsxt/tbGsxtZdry/update").then(() => {
        proxy.$message({ type: "success", message: "移除成功" });
        getList();
      });
    });
};
const handleMoveToFocus = (id) => {
  proxy.$confirm("确定要移至关注库？", "警告", { type: "warning" }).then(() => {
    qcckPost({ id, rylx: "03" }, "/mosty-gsxt/tbGsxtZdry/rylxyd").then(() => {
      proxy.$message({ type: "success", message: "移除成功" });
      getList();
    });
  });
};

const chooseData = (data) => {
  ids.value = Array.isArray(data) ? data.map((item) => item.id) : [];
  choosList.value = Array.isArray(data) ? data : [];
};
// 选择申请数据数据
const handleApplication = () => {
  if (ids.value.length === 0)
    return ElMessage.error("请先选择需要布控的重点人");
  qcckPost({ ids: ids.value }, "/mosty-gsxt/tbGsxtZdry/addBksq")
    .then(() => {
      ElMessage.success("申请成功");
      visible.value = false;
      getList();
    })
    .catch(() => {
      ElMessage.error("布控申请失败");
    });
};

const handleUserSelected = (val) => {
  obj.value.fpmc = val[0].userName;
  obj.value.fpid = val[0].id;
};

// 处理分配
const handlefp = () => {
  if (ids.value.length === 0)
    return ElMessage.error("请先选择需要布控的重点人");
  qcckPost(
    { ids: ids.value, uid: obj.value.fpid },
    "/mosty-gsxt/tbGsxtZdry/addGkmj"
  )
    .then(() => {
      ElMessage.success("分配成功");
      visible.value = false;
      visiblefp.value = false;
      getList();
    })
    .catch(() => {
      ElMessage.error("分配失败");
    });
};

// 移交管控
const handleMove = () => {
  if (ids.value.length === 0)
    return ElMessage.error("请先选择需要移交管控的重点群体");
  proxy.$confirm("是否确定移交？", "警告", { type: "warning" }).then(() => {
    qcckPost({ ids: ids.value }, "/mosty-gsxt/tbGsxtZdry/addSfyj")
      .then(() => {
        ElMessage.success("移交管控成功");
        getList();
      })
      .catch(() => {
        ElMessage.error("移交管控失败");
      });
  });
};

// 转线索
const handleZxs = () => {
  if (ids.value.length === 0)
    return ElMessage.error("请先选择需要转线索的重点群体");
  showzxs.value = true;
  nextTick(() => {
    zxsDilof.value.init(choosList.value);
  });
};

//删除操作
const deleteRow = (id) => {
  proxy.$confirm("确定要删除", "警告", { type: "warning" }).then(() => {
    qcckDelete({}, "/mosty-gsxt/tbGsxtZdry/" + id).then((res) => {
      ElMessage.success("删除成功");
      getList();
    });
  });
};

//新增编辑
const addEdit = (type, row) => {
  show.value = true;
  nextTick(() => {
    addFormDiloag.value.init(type, row);
  });
};

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

// 标签颜色
const Bqys = (ys) => {
  switch (ys) {
    case "01":
      return "#fd4343";
    case "02":
      return "#c26e09";
    case "03":
      return "#ffd208ff";
    case "04":
      return "#01abee";
    default:
      break;
  }
};
</script>

<style lang="scss" scoped>
.marks {
  padding: 0 4px;
  white-space: nowrap;
  background: #73acf1;
  border-radius: 4px;
  color: #fff;
}
</style>
<style>
.el-loading-mask {
  background: rgba(0, 0, 0, 0.5) !important;
}
</style>
