<template>
  <div>
    <div class="titleBox">
       <!-- 头部 -->
      <PageTitle title="从业人员管理">
        <el-button type="primary" @click="addEdit('add', '')">
          <el-icon style="vertical-align: middle"> <CirclePlus /> </el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>
        <el-button @click="delDictItem(ids)" :disabled="ids.length == 0" typeof="danger">
          <el-icon style="vertical-align: middle"><Delete /> </el-icon>
          <span style="vertical-align: middle">批量删除</span>
        </el-button>
      </PageTitle>
    </div>
    <!-- 搜索 -->
    <div ref="searchBox">
      <Search :searchArr="searchConfiger" @submit="onSearch" />
    </div>
    <!-- 表格 -->
    <div class="tabBox">
      <MyTable
        :tableData="pageData.tableData"
        :tableColumn="pageData.tableColumn"
        :tableHeight="pageData.tableHeight"
        :key="pageData.keyCount"
        :tableConfiger="pageData.tableConfiger"
        :controlsWidth="pageData.controlsWidth"
        @chooseData="chooseData"
      >
        <template #xb="{ row }">
          <dict-tag :options="D_BZ_XB" :value="row.xb" :tag="false" />
        </template>
        <!-- 操作 -->
        <template #controls="{ row }">
          <el-button size="small" @click="addEdit('edit', row.id)">修改</el-button>
          <el-button size="small" @click="addEdit('detail', row.id)">详情</el-button>
          <el-button size="small" @click="delDictItem([row.id])">删除</el-button>
        </template>
      </MyTable>
      <Pages
        @changeNo="changeNo"
        @changeSize="changeSize"
        :tableHeight="pageData.tableHeight"
        :pageConfiger="{
          ...pageData.pageConfiger,
          total: pageData.total
        }"
      ></Pages>

    </div>
    <!-- 编辑详情 -->
    <EditAddForm ref="addEditDialog" :dic="{D_BZ_XB}" @updateDate="getData"/>
  </div>
</template>

<script setup>
import PageTitle from "@/components/aboutTable/PageTitle.vue";
import MyTable from "@/components/aboutTable/MyTable.vue";
import Pages from "@/components/aboutTable/Pages.vue";
import Search from "@/components/aboutTable/Search.vue";
import EditAddForm from './editAddForm.vue'
import { qcckGet, qcckPost ,qcckDelete} from "@/api/qcckApi.js";
import { reactive, ref ,onMounted,getCurrentInstance } from 'vue';
const { proxy } = getCurrentInstance();
const { D_BZ_XB } = proxy.$dict("D_BZ_XB");
const addEditDialog = ref();
const ids = ref([]);//多选
const searchBox = ref() //搜索框
const searchConfiger = reactive([
  {
    showType: "input",
    prop: "xm",
    placeholder: "请输入姓名",
    label: "姓名"
  },{
    showType: "input",
    prop: "sfzh",
    placeholder: "请输入身份证号",
    label: "身份证号",
  },
  {
    showType: "input",
    prop: "jg",
    placeholder: "请输入籍贯",
    label: "籍贯",
  },
])

const queryFrom = ref({})
const pageData = reactive({
  tableData: [], //表格数据
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType:'checkBox',
    loading:false
  },
  total: 0,
  pageConfiger: {
    pageSize: 10,
    pageNum: 1
  }, //分页
  controlsWidth: 210, //操作栏宽度
  tableColumn: [
    {
      label: "姓名",
      prop: "xm"
    },
    {
      label: "身份证号",
      prop: "sfzh"
    },
    {
      label: "手机号",
      prop: "lxdh"
    },
    {
      label: "性别",
      prop: "xb",
      showSolt :true,
    },
    {
      label: "籍贯",
      prop: "jg",
    },
     {
      label: "最后修改时间",
      prop: "xtZhgxsj",
    },
  ]
});
onMounted(() => {
  getData() //获取数据
  tabHeightFn();
  proxy.mittBus.on("mittFn", (data) => {
    pageData.keyCount = data;
  });
});
// 搜索
const onSearch = (val)=>{
  for (let key in val) {
    if(!val[key]) delete val[key];
    if(val.cz) delete val.cz
  }
  queryFrom.value = {...val}
  pageData.pageConfiger.pageNum = 1
  getData() //获取数据
}
// 获取数据
const getData = ()=>{
  let pramas = {
    pageSize:pageData.pageConfiger.pageSize,
    pageCurrent:pageData.pageConfiger.pageNum,
    ...queryFrom.value
  }
  pageData.tableConfiger.loading = true
  qcckGet(pramas,'/mosty-jcgl/tbJcglXfCyry').then(res=>{
    pageData.tableData = res.records || []
    pageData.tableConfiger.loading = false
    pageData.total = res.total
  }).catch(()=> { pageData.tableConfiger.loading = false })
}
// 新增
const addEdit =  (type,id)=>{
  addEditDialog.value.init(type,id)
}

// 多选
const chooseData = (val)=>{
  if(!val) return false;
  ids.value = val.map(v=>{ return v.id })
  
}
// 批量删除 
function delDictItem(row) {
  proxy.$confirm("确定要删除", "警告", {type: "warning"}).then(() => {
    qcckPost(row,'/mosty-jcgl/tbJcglXfCyry/bacth').then(() => {
      proxy.$message({type: "success", message: "删除成功" });
      pageData.pageConfiger.pageNum = 1
      getData();
    });
    }).catch(() => {
      proxy.$message.info("已取消");
  });
}

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

<style>
.el-loading-mask{
  background: rgba(0,0,0,0.3);
}
</style>
