<template>
  <layout-card>
    <el-form ref="formRef" :rules="rules" :model="form" label-width="160px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="人员类型" prop="PTYPE">
            <el-select v-model="form.PTYPE" @change="changeName">
              <el-option
                v-for="item in typeList"
                :key="item.ID"
                :label="item.NAME"
                :value="item.NAME"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.PTYPE === '相关方人员'">
          <el-form-item label="单位名称" prop="UNITS_ID">
            <el-select v-model="form.UNITS_ID" placeholder="请选择">
              <el-option
                v-for="item in unitsNameList"
                :key="item.UNITS_ID"
                :label="item.UNITS_NAME"
                :value="item.UNITS_ID"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.PTYPE !== '相关方人员'">
          <el-form-item label="单位名称">
            <el-input :model-value="CORP_NAME" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="作业类别" prop="JOB_TYPE">
            <el-select v-model="form.JOB_TYPE">
              <el-option
                v-for="item in personnelManagementJobTypeList"
                :key="item.DICTIONARIES_ID"
                :label="item.NAME"
                :value="item.DICTIONARIES_ID"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="特种作业证书编号" prop="CERTIFICATE_NUM">
            <el-input v-model="form.CERTIFICATE_NUM" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="姓名" prop="NAME">
            <el-input v-model="form.NAME" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="SEX">
            <el-select v-model="form.SEX">
              <el-option
                v-for="item in sexList"
                :key="item.ID"
                :label="item.NAME"
                :value="item.NAME"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="复审时间" prop="REVIEWTIME">
            <el-date-picker
              v-model="form.REVIEWTIME"
              value-format="YYYY-MM-DD"
              format="YYYY-MM-DD"
              type="date"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码" prop="PHONENUM">
            <el-input v-model="form.PHONENUM" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号码" prop="IDENTITYCARD">
            <el-input v-model="form.IDENTITYCARD" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发证机关" prop="LICENSING">
            <el-input v-model="form.LICENSING" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="特种作业证书有效期" prop="VALID_TIME">
            <el-date-picker
              v-model="form.VALID_TIME"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD"
              format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="mt-10 tc">
      <el-button type="primary" @click="fnSubmit">提交</el-button>
    </div>
  </layout-card>
</template>

<script setup>
import { layoutFnGetPersonnelManagementJobType } from "@/assets/js/data_dictionary.js";
import { getRelatedPartiesManagementList } from "@/request/stakeholder_management.js";
import useListData from "@/assets/js/useListData.js";
import { ref } from "vue";
import {
  getPersonnelManagementView,
  getVerifyDeduplicationCertificateNum,
  setPersonnelManagementAdd,
  setPersonnelManagementEdit,
} from "@/request/special_operations.js";
import { useRoute, useRouter } from "vue-router";
import { useUserStore } from "@/pinia/user.js";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import { ElMessage } from "element-plus";

const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const { PERSONNELMANAGEMENT_ID } = route.query;
const CORP_NAME = userStore.getUserInfo.CORP_NAME;
const CORPINFO_ID = userStore.getUserInfo.CORPINFO_ID;
const typeList = [
  { ID: "0", NAME: "公司员工" },
  { ID: "1", NAME: "相关方人员" },
];
const sexList = [
  { ID: "0", NAME: "男" },
  { ID: "1", NAME: "女" },
];
const personnelManagementJobTypeList =
  await layoutFnGetPersonnelManagementJobType();
const { list: unitsNameList } = useListData(getRelatedPartiesManagementList, {
  otherParams: {
    showCount: 99999,
  },
});
const fnHasCertificate = async (rule, value, callback) => {
  if (value) {
    const resData = await getVerifyDeduplicationCertificateNum({
      CERTIFICATE_NUM: value,
      PERSONNELMANAGEMENT_ID,
    });
    if (resData.msg) callback(new Error(resData.msg));
    else callback();
  } else {
    callback();
  }
};
const rules = {
  UNITS_ID: [
    { required: true, message: "单位名称不能为空", trigger: "change" },
  ],
  PTYPE: [{ required: true, message: "人员类型不能为空", trigger: "change" }],
  JOB_TYPE: [
    { required: true, message: "作业类别不能为空", trigger: "change" },
  ],
  NAME: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
  SEX: [{ required: true, message: "性别不能为空", trigger: "change" }],
  CERTIFICATE_NUM: [
    { required: true, message: "特种作业证书编号不能为空", trigger: "blur" },
    { validator: fnHasCertificate, trigger: "blur" },
  ],
  PHONENUM: [
    { min: 11, max: 11, message: "请输入11位手机号码", trigger: "blur" },
    {
      pattern:
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
      message: "请输入正确的手机号码",
    },
  ],
  IDENTITYCARD: [
    {
      pattern:
        /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(([0-2][1-9])|10|20|30|31)\d{3}(\d|X|x)$/,
      message: "请输入正确的身份证号",
    },
  ],
};
const form = ref({
  PTYPE: "",
  UNITS_ID: "",
  JOB_TYPE: "",
  CERTIFICATE_NUM: "",
  NAME: "",
  SEX: "",
  REVIEWTIME: "",
  PHONENUM: "",
  IDENTITYCARD: "",
  LICENSING: "",
  VALID_TIME: [],
});
const formRef = ref(null);
const fnGetData = async () => {
  if (!PERSONNELMANAGEMENT_ID) return;
  const resData = await getPersonnelManagementView({
    PERSONNELMANAGEMENT_ID,
  });
  form.value = resData.pd;
  form.value.VALID_TIME = [resData.pd.VALID_STIME, resData.pd.VALID_ETIME];
};
fnGetData();
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    const params = {
      ...form.value,
      UNITS_ID:
        form.value.PTYPE === "公司员工" ? CORPINFO_ID : form.value.UNITS_ID,
      VALID_STIME: form.value.VALID_TIME[0],
      VALID_ETIME: form.value.VALID_TIME[1],
    };
    !PERSONNELMANAGEMENT_ID
      ? await setPersonnelManagementAdd(params)
      : await setPersonnelManagementEdit(params);
    ElMessage.success("提交成功");
    router.back();
  },
  { atBegin: true }
);
</script>

<style scoped lang="scss"></style>
