<template>
  <div>
    <div class="titleBox">
      <PageTitle title="保安员年检送培">
        <el-button type="primary" @click="onBatchAudit">新增</el-button>
      </PageTitle>
    </div>
    <!-- 搜索 -->
    <div ref="searchBox">
      <Search :searchArr="searchConfiger" @submit="onSearch" :key="pageData.keyCount" />
    </div>
    <!-- 表格 -->
    <div class="tabBox">
      <div style="margin-bottom: 12px;">
        <el-button type="primary" @click="onBatchAudit">批量审核</el-button>
      </div>
      <MyTable :tableData="pageData.tableData" :tableColumn="pageData.tableColumn" :tableHeight="pageData.tableHeight"
        :key="pageData.keyCount" :tableConfiger="pageData.tableConfiger" :controlsWidth="pageData.controlsWidth"
        @chooseData="chooseData">
        <template #auditStatus="{ row }">
          <el-tag v-if="String(row.auditStatus || '').includes('驳回')" type="danger">{{ row.auditStatus }}</el-tag>
          <el-tag v-else-if="String(row.auditStatus || '').includes('待')" type="warning">{{ row.auditStatus }}</el-tag>
          <el-tag v-else-if="String(row.auditStatus || '').includes('通过')" type="success">{{ row.auditStatus }}</el-tag>
          <span v-else>{{ row.auditStatus }}</span>
        </template>
        <template #sendStatus="{ row }">
          <el-tag v-if="String(row.sendStatus || '').includes('未')" type="info">{{ row.sendStatus }}</el-tag>
          <el-tag v-else-if="String(row.sendStatus || '').includes('已')" type="success">{{ row.sendStatus }}</el-tag>
          <span v-else>{{ row.sendStatus }}</span>
        </template>
        <!-- 操作 -->
        <template #controls="{ row }">
          <el-link type="primary" link @click="addEdit('RefSteap', row)">审核流程</el-link>
          <el-link type="primary" link @click="addEdit('detail', row)">详情</el-link>
        </template>
      </MyTable>
      <Pages @changeNo="changeNo" @changeSize="changeSize" :tableHeight="pageData.tableHeight" :pageConfiger="{
        ...pageData.pageConfiger,
        total: pageData.total
      }"></Pages>
    </div>
    <!-- 详情 -->
    <DetailForm ref="detailDiloag" :dict="{D_BZ_SF}"/>
    <!-- 审核流程 -->
    <Steps ref="RefSteap" ></Steps>
  </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 DetailForm from "./components/detailForm.vue";
import Steps from "./components/steps.vue";
import {getItem} from '@/utils/storage.js'
import { cyryPage,njryPage} from "@/api/pxzx.js";
import { reactive, ref, onMounted, getCurrentInstance, nextTick } from "vue";
import { ElMessage } from "element-plus";
const { proxy } = getCurrentInstance();
const { D_BZ_SF ,D_BAXX_DWLX} = proxy.$dict("D_BZ_SF","D_BAXX_DWLX");
const detailDiloag = ref();
const RefSteap = ref();
const shForm = ref();
const searchBox = ref(); //搜索框
const selectedRows = ref([]);
const searchConfiger = ref([
  {
    label: "姓名",
    prop: "xm",
    showType: "input"
  },
  {
    label: "身份证号码",
    prop: "sfzh",
    showType: "input"
  },
  {
    label: "电话",
    prop: "lxdh",
    showType: "input"
  },
]);
const idEntityCard=getItem('idEntityCard')
const queryFrom = ref({});
const pageData = reactive({
  tableData: [
    {
        companyName: "保安公司名称",
        reviewYear: "年审年份",
        xm: "姓名",
        sfzh: "身份证号码",
        phone: "电话",
        education: "文化程度",
        certificateNo: "证书编号",
        serviceIndustry: "服务行业",
        entryDate: "入职时间",
        workCardNo: "上岗证号码",
        auditStatus: "审核状态",
        auditTime: "审核时间",
        sendStatus: "送培状态"
    }
  ],
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "checkBox",
    loading: false
  },
  total: 0,
  pageConfiger: {
    pageSize: 20,
    pageCurrent: 1
  },
  controlsWidth: 180,
  tableColumn: [
    { label: "保安公司名称", prop: "companyName",showOverflowTooltip: true },
    { label: "培训公司", prop: "pxgs", showOverflowTooltip: true },
    { label: "年审年份", prop: "reviewYear"},
    { label: "姓名", prop: "xm" },
    { label: "身份证号码", prop: "sfzh",showOverflowTooltip: true },
    { label: "电话", prop: "phone" },
    { label: "文化程度", prop: "education" },
    { label: "证书编号", prop: "certificateNo", showOverflowTooltip: true },
    { label: "服务行业", prop: "serviceIndustry" },
    { label: "入职时间", prop: "entryDate" },
    { label: "上岗证号码", prop: "workCardNo", showOverflowTooltip: true },
    { label: "送培状态", prop: "sendStatus", showSolt: true }
  ]
});
onMounted(() => {
//   getInfo();
  tabHeightFn();
});
// 搜索
const onSearch = (val) => {
  queryFrom.value = { ...val };
  pageData.pageConfiger.pageCurrent = 1;
  getList();
};
const gsId=ref('')
const changeNo = (val) => {
  pageData.pageConfiger.pageNum = val;
  getList();
};
const changeSize = (val) => {
  pageData.pageConfiger.pageSize = val;
  getList();
};
const getInfo = () => {
  let promes = { sfzh: idEntityCard }
  cyryPage(promes).then(res => {
    const data = res.records[0] || {};
    if (data) {
    gsId.value=data.code
   }
    getList()
  }).finally(() => {
    pageData.tableConfiger.loading = false;
  })
}
// 获取列表
const getList = () => {
  pageData.tableConfiger.loading = true;
  const promes = {
    ...pageData.pageConfiger,
    ...queryFrom.value,
    dwbm: gsId.value
  }
  njryPage(promes).then((res) => {
    pageData.tableData = (res.records || []).map((item) => {
      return {
        ...item,
        companyName: item.companyName || item.dwmc || item.gsmc || item.dwmcName || item.bagsmc,
        reviewYear: item.reviewYear || item.njnd || item.njYear || item.nf || item.year,
        phone: item.phone || item.lxdh || item.dh || item.sjhm || item.xssbrdh,
        education: item.education || item.whcd || item.whcdText,
        certificateNo: item.certificateNo || item.zsbh || item.zsbhm || item.zjbh,
        serviceIndustry: item.serviceIndustry || item.fwhy || item.fwhyText || item.hy,
        entryDate: item.entryDate || item.rzsj || item.rzrq,
        workCardNo: item.workCardNo || item.sgzh || item.sgzNumber,
        auditStatus: item.auditStatus || item.shztText || item.shzt,
        auditTime: item.auditTime || item.shsj || item.njsj,
        sendStatus: item.sendStatus || item.spztText || item.spzt || (item.RefSteap ? "已送培" : "未送培")
      };
    });
    pageData.total = res.total;
  }).catch((err) => {
    console.log(err);

  }).finally(() => {
    pageData.tableConfiger.loading = false;
  });
};

const chooseData = (rows) => {
  selectedRows.value = Array.isArray(rows) ? rows : [];
};

const onBatchAudit = () => {
  if (!selectedRows.value.length) {
    ElMessage.warning("请选择需要审核的数据");
    return;
  }
  ElMessage.success("已选择 " + selectedRows.value.length + " 条");
};


// 详情
const addEdit = (type, row) => {
  nextTick(() => {
    if(type == 'RefSteap'){
      RefSteap.value.init(row);
    }else if(type == 'shForm'){
      shForm.value.init(row);
    }
    else{
      detailDiloag.value.init(type, row);
    }
  })
};


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

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