<template>
  <div>
    <div class="titleBox">
      <page-title title="保安待培训人员管理" />
    </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 #ssbags="{ row }">
          <DictTag :value="row.ssbags" :options="D_BAXX_DWLX" />
        </template>

        <template #pxsc="{ row }">
          <span>{{ row.pxsc }}小时</span>
        </template>
        <!-- 操作 -->
        <template #controls="{ row }">
          <el-link type="danger" @click="handleDelete([row.id])">删除</el-link>
          <el-link type="warning" @click="addEdit('view', row)">详情</el-link>
        </template>
      </MyTable>
      <Pages @changeNo="changeNo" @changeSize="changeSize" :tableHeight="pageData.tableHeight" :pageConfiger="{
        ...pageData.pageConfiger,
        total: pageData.total
      }"></Pages>
    </div>

    <view-info-dialog ref="trainerRef" v-model="isVisible" />
  </div>
</template>

<script setup>
import { onMounted, reactive, ref, getCurrentInstance } 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 viewInfoDialog from "./components/viewInfoDialog.vue";

const { proxy } = getCurrentInstance();
const { D_BAXX_DWLX } = proxy.$dict("D_BAXX_DWLX")
const trainerRef = ref(null);
const queryFrom = ref({});
const isVisible = ref(false);
const searchBox = ref(null);
const searchConfiger = ref([
  {
    label: "姓名",
    prop: "xm",
    placeholder: "请输入人员姓名",
    showType: "input"
  },
  {
    label: "联系电话",
    prop: "lxdh",
    placeholder: "请输入联系电话",
    showType: "input"
  },
  {
    label: "所属保安公司",
    prop: "ssbags",
    placeholder: "请选择所属保安公司",
    showType: "select",
    options: D_BAXX_DWLX
  },
  {
    label: "提交日期",
    prop: "tjrq",
    placeholder: "请选择提交日期",
    showType: "date"
  }
]);

const pageData = reactive({
  tableData: [],
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "null",
    loading: false
  },
  total: 0,
  pageConfiger: {
    sfcjpx: 0, // 是否参加培训
    pageSize: 10,
    pageCurrent: 1
  },
  controlsWidth: 180,
  tableColumn: [
    { label: "姓名", prop: "xm" },
    { label: "身份证号", prop: "sfzh" },
    { label: "联系方式", prop: "lxdh" },
    { label: "所属保安公司", prop: "ssbags" },
    { label: "线上培训时长", prop: "pxsc" },
    { label: "提交日期", prop: "" },
  ]
});

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

const addEdit = (type, row) => {
  trainerRef.value.open(row, type);
};

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

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

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

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

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

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

<style scoped lang="scss">

</style>
