<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item
              label="用户名/姓名"
              prop="KEYWORDS"
              label-width="100px"
            >
              <el-input
                v-model="searchForm.KEYWORDS"
                placeholder="请输入关键字"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="人员类型" prop="PERSONNEL_TYPE">
              <el-select v-model="searchForm.PERSONNEL_TYPE" clearable>
                <el-option
                  v-for="item in data.personnelTypeList"
                  :key="item.DICTIONARIES_ID"
                  :label="item.NAME"
                  :value="item.DICTIONARIES_ID"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPagination">
                重置
              </el-button>
              <!--              <el-button native-type="reset" @click="fnWindow"> BI </el-button>-->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        v-model:pagination="pagination"
        :data="list"
        row-key="USER_ID"
        @get-data="fnGetDataTransfer"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="NAME" label="从业人员" />
        <el-table-column
          prop="PERSONNEL_TYPE_NAME"
          label="人员类型"
          width="150"
        />
        <el-table-column prop="USER_ID_CARD" label="身份证号" />
        <el-table-column prop="PHONE" label="手机号" />
        <el-table-column label="申请类别">
          <template #default="{ row }">
            <span v-if="row.APPLY_TYPE === '1'">入职申请</span>
            <span v-else-if="row.APPLY_TYPE === '2'">企业入职</span>
            <span v-else-if="row.APPLY_TYPE === '3'">离职申请</span>
            <span v-else-if="row.APPLY_TYPE === '4'">企业解聘</span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column label="审核状态">
          <template #default="{ row }">
            <div v-if="row.APPLY_TYPE === '1' || row.APPLY_TYPE === '3'">
              <span v-if="row.APPLY_STATUS === '0'">待审核</span>
              <span v-else-if="row.APPLY_STATUS === '-1'">审核未通过</span>
              <span v-else>审核通过</span>
            </div>
            <div v-else>
              <span v-if="row.APPLY_STATUS === '0'">待确认</span>
              <span v-else-if="row.APPLY_STATUS === '-1'">确认未认可</span>
              <span v-else>确认完成</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/enterprise_management/user_practitioner/details',
                  query: {
                    USER_ID: row.USER_ID,
                  },
                })
              "
            >
              查看
            </el-button>
            <el-button
              v-if="buttonJurisdiction.edit && row.APPLY_STATUS !== '0'"
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/enterprise_management/user_practitioner/edit',
                  query: {
                    USER_ID: row.USER_ID,
                  },
                })
              "
            >
              编辑
            </el-button>
            <el-button
              v-if="
                buttonJurisdiction.del &&
                row.APPLY_STATUS === '0' &&
                (row.APPLY_TYPE === '1' || row.APPLY_TYPE === '3')
              "
              type="danger"
              text
              link
              @click="reviewItem(row)"
            >
              审核
            </el-button>
            <el-button
              v-if="buttonJurisdiction.del && row.APPLY_STATUS === '1'"
              type="danger"
              text
              link
              @click="fnDelete(row)"
            >
              解聘
            </el-button>
          </template>
        </el-table-column>

        <template #button>
          <el-button
            v-if="buttonJurisdiction.add"
            type="primary"
            @click="
              router.push({
                path: '/enterprise_management/user_practitioner/add',
              })
            "
          >
            添加
          </el-button>
          <!--          <el-button
            v-if="buttonJurisdiction.del"
            type="danger"
            @click="fnBatchDelete"
          >
            批量删除
          </el-button>-->
        </template>
      </layout-table>
    </layout-card>

    <review
      v-model:visible="data.reviewDialog.visible"
      :form="data.reviewDialog.form"
      @get-data="fnGetDataTransfer"
    />
  </div>
</template>

<script setup>
import { nextTick, reactive, ref } from "vue";
import { serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import { ElMessageBox, ElMessage } from "element-plus";
import Review from "./review.vue";
import { debounce } from "throttle-debounce";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import { useRouter } from "vue-router";
import {
  getPractitionerList,
  setPractitionerDismissal,
} from "@/request/user_practitioner.js";
import { layoutFnGetPersonnelTypeTraffic } from "@/assets/js/data_dictionary.js";

const router = useRouter();
const buttonJurisdiction = await useButtonJurisdiction("user");
const tableRef = ref(null);
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getPractitionerList, {
    otherParams: {
      // APPLY_TYPE: "1-3",
    },
  });
const data = reactive({
  personnelTypeList: [],
  reviewDialog: {
    visible: false,
    form: {
      USER_ID: "",
      // 审核结果
      REVIEW_RESULT: "",
      // 审核意见
      REVIEW_COMMENTS: "",
      DEPARTMENT_ID: "",
      POST_ID: "",
      SHIFTDUTYONE: "",
      SHIFTDUTYTWO: "",
    },
    id: "",
  },
  visible: false,
  info: {},
});
const fnGetDataTransfer = () => {
  fnGetData({});
};

const reviewItem = async (row) => {
  data.reviewDialog.visible = true;
  await nextTick();
  data.reviewDialog.form = row;
};

/* const fnWindow = async () => {
  window.open(
    "http://60.10.47.50:9001/#/middlePage?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd2QiOiJMZmtmcUAxMjM0NTYiLCJleHAiOjE3MTI4OTc1NjcsInVzZXIiOiJsZnlqemhkcCIsInRhcmdldCI6Imh0dHA6Ly8xOTIuMTY4LjAuNjk6ODA4Mi8jL21pZGRsZVBhZ2UifQ.TUl_q6hcWaXMAVlVqOVQup77MjQWcxc-g8SNrhQRbzc"
  );
}; */
// 页面下拉选项字典获取
const fnGetLevels = async () => {
  const { value: personnelTypeList } = await layoutFnGetPersonnelTypeTraffic({
    DICTIONARIES_ID: "0b62f92b0b624aab8e89a77304a64d5e",
    BIANMA: "TRAFFIC_EMPLOYMENT_DRIVE",
  });
  data.personnelTypeList = personnelTypeList;
};
fnGetLevels();
const fnDelete = debounce(
  1000,
  async (row) => {
    await ElMessageBox.confirm(`确定要解聘【${row.NAME}】吗？`, {
      type: "warning",
    });
    await setPractitionerDismissal({ ...row });
    ElMessage.success("人员解聘完成");
    fnGetDataTransfer();
  },
  { atBegin: true }
);
</script>

<style scoped></style>
