<template>
  <div>
    <div class="titleBox">
      <page-title title="申请人员" />
      <el-button type="primary" @click="addEdit('add')">新增</el-button>
    </div>
    <!-- 搜索 -->
    <div ref="searchBox">
      <Search :searchArr="searchConfiger" @submit="onSearch"></Search>
    </div>
    <!-- 表格 -->
    <div class="tabBox">
      <MyTable :tableData="pageData.tableData" :tableColumn="pageData.tableColumn" :tableHeight="pageData.tableHeight"
        :key="pageData.keyCount" :tableConfiger="pageData.tableConfiger" :controlsWidth="pageData.controlsWidth">
        <template #pxgs="{ row }">
          {{ row?.ryzpzm && row?.ryzpfm && row?.tjbg && row?.wfzzmjl ? `是` : `否` }}
        </template>
        <template #sfpx="{ row }">
          {{ row?.sfpx === '01' ? `否` : `是` }}
        </template>
        <template #gw="{ row }">
          <DictTag :value="row.gw" :options="D_BAXX_GWLX" />
        </template>
        <!-- 操作 -->
        <template #controls="{ row }">
          <el-link type="success" @click="addEdit('upload', row)">上传资料</el-link>
          <el-link type="danger" @click="handleDelete([row.id])">删除</el-link>
          <el-link type="warning" @click="addEdit('view', row)">详情</el-link>
          <el-link type="primary" @click="addEdit('select', row)">提交培训公司</el-link>
        </template>
      </MyTable>
      <Pages @changeNo="changeNo" @changeSize="changeSize" :tableHeight="pageData.tableHeight" :pageConfiger="{
        ...pageData.pageConfiger,
        total: pageData.total
      }"></Pages>
    </div>


    <add-trainer-dialog v-model="isVisible" ref="trainerRef" @refresh="getList" />
    <select-ttaning-dialog ref="selectTtaningRef" v-model="dialogVisible"  @refresh="getList" />
  </div>
</template>

<script setup>
import { getCurrentInstance, onMounted, reactive, ref } from "vue";
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 PageTitle from "@/components/aboutTable/PageTitle.vue";
import AddTrainerDialog from "./components/addTrainerDialog.vue";
import selectTtaningDialog from "./components/selectTtaningDialog.vue";

const { proxy } = getCurrentInstance();
const { D_BAXX_GWLX } = proxy.$dict("D_BAXX_GWLX")
const trainerRef = ref(null);
const selectTtaningRef = ref(null);
const queryFrom = ref({});
const isVisible = ref(false);
const dialogVisible = ref(false);
const searchBox = ref(null);
const searchConfiger = ref([
  {
    label: "人员姓名",
    prop: "xm",
    placeholder: "请输入人员姓名",
    showType: "input"
  },
  {
    label: "证件号码",
    prop: "zjhm",
    placeholder: "请输入证件号码",
    showType: "input"
  },
  {
    label: "联系电话",
    prop: "lxdh",
    placeholder: "请输入联系电话",
    showType: "input"
  },
]);

const pageData = reactive({
  tableData: [{}],
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "null",
    loading: false
  },
  total: 0,
  pageConfiger: {
    pageSize: 10,
    pageCurrent: 1
  },
  controlsWidth: 180,
  tableColumn: [
    { label: "姓名", prop: "xm" },
    { label: "证件号码", prop: "zjhm" },
    { label: "联系方式", prop: "lxdh" },
    { label: "岗位", prop: "gw", showSolt: true },
    { label: "申请时间", prop: "xtCjsj" },
    { label: "是否上传资料", prop: "pxgs", showSolt: true },
    { label: "是否线上培训", prop: "sfpx", showSolt: true },
  ]
});

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

const addEdit = (type, row) => {
  if (type === 'select') {
    selectTtaningRef.value.open(row, type);
  } else {
    trainerRef.value.open(row, type);
  }
};

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

const getList = async () => {
  try {
    pageData.tableConfiger.loading = true;
    const res = await qcckPost({
      ...pageData.pageConfiger,
      ...queryFrom.value
    }, `/mosty-base/baxx/basq/page`)

    if(res) {
      pageData.tableData = res.records || [];
      pageData.total = res.total;
    }
  } finally {
    pageData.tableConfiger.loading = false;
  }
}

const handleDelete = async (ids) => {
  await proxy.$modal.confirm("是否确认删除该培训人员？")

  try {
    await qcckPost({ idList: ids }, `/mosty-base/baxx/basq/remove`)
    proxy.$modal.msgSuccess("删除成功");
    await getList();
  } catch (error) {
    proxy.$modal.msgError("删除失败");
  }
}

onMounted(() => {
  tabHeightFn();
  getList();
});
</script>

<style scoped lang="scss">

</style>
