<template>
  <el-dialog
    :title="titleValue"
    width="1200px"
    :model-value="modelValue"
    append-to-body
    @close="closed"
  >
    <div>
      <el-form :model="listQuery" class="mosty-from-wrap" :inline="true">
        <el-form-item label="姓名">
          <el-input
            placeholder="请输入姓名"
            v-model="listQuery.ryXm"
            clearable
          ></el-input>
        </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
        class="tabBox"
        :class="props.Single ? 'tabBoxRadio' : ''"
        style="margin-top: 0px"
      >
        <el-table
          v-loading="loading"
          ref="multipleUserRef"
          @selection-change="handleSelectionChange"
          :data="tableData"
          border
          :row-key="keyid"
          style="width: 100%"
          height="450"
        >
          <el-table-column
            type="selection"
            width="55"
            :reserve-selection="true"
          />
          <el-table-column prop="fjZp" align="center" label="照片" />
          <el-table-column prop="ryXm" align="center" label="姓名" />
          <el-table-column label="性别">
            <template #default="scope">
              <span style="margin-left: 10px">{{
                getLabelByValue(scope.row.ryXb, D_BZ_XB)
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="rySfzh" align="center" label="身份证号" />
          <el-table-column prop="hjdXz" align="center" label="户籍地详址" />
          <el-table-column prop="xzdXz" align="center" label="现居住地详址" />
          <el-table-column prop="ryLxdh" align="center" label="联系电话" />
          <el-table-column label="人员民族">
            <template #default="scope">
              <span style="margin-left: 10px">{{
                getLabelByValue(scope.row.ryMz, D_BZ_MZ)
              }}</span>
            </template>
          </el-table-column>

          <el-table-column label="重点人处置状态">
            <template #default="scope">
              <span style="margin-left: 10px">{{
                getLabelByValue(scope.row.zdrCzzt, D_GS_ZDR_CZZT)
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="人员级别">
            <template #default="scope">
              <span style="margin-left: 10px">{{
                getLabelByValue(scope.row.zdrRyjb, D_GS_ZDR_RYJB)
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="所属警种">
            <template #default="scope">
              <span style="margin-left: 10px">{{
                getLabelByValue(scope.row.zdrSsjz, D_GS_BK_SSJZ)
              }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.current"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closed">取消</el-button>
        <el-button type="primary" @click="onComfirm">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineProps, ref, onMounted, getCurrentInstance } from "vue";
import { qcckGet } from "@/api/qcckApi.js";
const { proxy } = getCurrentInstance();
const {
  D_GS_BK_DX,
  D_GS_BK_SSJZ,
  D_GS_SSYJ,
  D_GS_BQ_DJ,
  D_BZ_XB,
  D_GS_BK_LX,
  D_BZ_MZ,
  D_GS_ZDR_CZZT,
  D_GS_ZDR_RYJB
} = proxy.$dict(
  "D_GS_BK_DX",
  "D_GS_BK_SSJZ",
  "D_GS_SSYJ",
  "D_GS_BQ_DJ",
  "D_BZ_XB",
  "D_GS_BK_LX",
  "D_BZ_MZ",
  "D_GS_ZDR_CZZT",
  "D_GS_ZDR_RYJB"
); //获取字典数据
const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  dic: {
    type: Object,
    default: () => {}
  },
  bqDl: {
    type: String,
    default: "01"
  },
  bqLx: {
    type: String,
    default: "01"
  },
  titleValue: {
    type: String,
    default: "选择大类"
  },
  LeaderType: {
    type: String,
    default: ""
  },
  //是否单选
  Single: {
    type: Boolean,
    default: true
  },
  roleIds: {
    type: Array,
    default: []
  }
});
const total = ref(0);
const listQuery = ref({
  current: 1,
  size: 20
});
const loading = ref(false);
const tableData = ref([]);
const emits = defineEmits(["update:modelValue", "chooseDate"]);
onMounted(() => {
  handleFilter();
});
const closed = () => {
  emits("update:modelValue", false);
};
const reset = () => {
  listQuery.value = { current: 1, size: 20 };
  getDataList();
};

const keyid = (row) => {
  return row.id;
};
// 为用户分配角色
const onComfirm = () => {
  const userList = multipleSelectionUser.value;
  let list = [];
  let listId = [];
  userList.forEach((val) => {
    if (listId.indexOf(val.id) == -1) {
      list.push(val);
      listId.push(val.id);
    }
  });
  emits("chooseDate", list);
  closed();
};
/**
 * pageSize 改变触发
 */
const handleSizeChange = (currentSize) => {
  listQuery.value.size = currentSize;
  getDataList();
};
const getLabelByValue = (value, Array) => {
  console.log(Array, "D_BZ_XB");
  const item = Array.find((item) => item.value === value);
  return item ? item.label : "未知";
};
/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  listQuery.value.current = currentPage;
  getDataList();
};

// 查询标签组合列表
const getDataList = () => {
  const data = listQuery.value;
  loading.value = true;
  qcckGet(data, "/mosty-gsxt/tbGsxtZdry/selectPage").then((res) => {
    tableData.value = res?.records;
    total.value = Number(res.total);
    loading.value = false;
    multipleUser();
  });
};

//列表回显
function multipleUser() {
  tableData.value.forEach((item) => {
    if (props.roleIds.some((id) => id == item.id)) {
      multipleUserRef.value.toggleRowSelection(item, true);
    }
  });
}

const handleFilter = () => {
  listQuery.value.current = 1;
  getDataList();
};

const multipleUserRef = ref(null);
const multipleSelectionUser = ref([]);
const handleSelectionChange = (val) => {
  if (props.Single) {
    if (val.length > 1) {
      let del_row = val.shift();
      multipleUserRef.value.toggleRowSelection(del_row, false);
    }
    multipleSelectionUser.value = val;
  } else {
    multipleSelectionUser.value = val;
  }
};
</script>

<style lang="scss" scoped>
@import "@/assets/css/layout.scss";
@import "@/assets/css/element-plus.scss";
</style>
<style>
.tabBoxRadio .el-checkbox__inner {
  border-radius: 50% !important;
}
.tabBoxRadio .el-table__header-wrapper .el-checkbox {
  display: none;
}
</style>
