<template>
  <div>
    <el-dialog
      v-if="visible"
      :model-value="visible"
      :title="type === 'add' ? '新增' : '修改'"
      :before-close="fnClose"
    >
      <el-form
        ref="formRef"
        :model="data.form"
        :rules="rules"
        label-width="150px"
      >
        <el-form-item label="姓名" prop="NAME">
          <el-input
            v-model="data.form.NAME"
            maxlength="255"
            placeholder="这里输入姓名"
          />
        </el-form-item>
        <el-form-item label="岗位/操作项目" prop="POST">
          <el-input
            v-model="data.form.POST"
            maxlength="255"
            placeholder="这里输入岗位/操作项目"
          />
        </el-form-item>
        <el-form-item label="电话" prop="PHONE">
          <el-input
            v-model="data.form.PHONE"
            maxlength="255"
            placeholder="这里输入电话"
          />
        </el-form-item>
        <el-form-item label="证书号" prop="CARD_ID">
          <el-input
            v-model="data.form.CARD_ID"
            maxlength="255"
            placeholder="这里输入证书号"
          />
        </el-form-item>
        <el-form-item label="证书有效期限" prop="EFFECTIVE_DATE">
          <el-date-picker
            v-model="data.form.EFFECTIVE_DATE"
            value-format="YYYY-MM-DD"
            format="YYYY-MM-DD"
            type="date"
            clearable
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="复训日期" prop="RETRAINING">
          <el-date-picker
            v-model="data.form.RETRAINING"
            value-format="YYYY-MM-DD"
            format="YYYY-MM-DD"
            type="date"
            clearable
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="fnClose">取消</el-button>
        <el-button type="primary" @click="fnSubmit"> 确定 </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import { useVModels } from "@vueuse/core";
import {
  getArchivesPostman,
  addArchivesPostman,
  editArchivesPostman,
} from "@/request/archives.js";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
  },
  year: {
    type: String,
    required: true,
  },
  type: {
    type: String,
    required: true,
  },
  ARCHIVES_POSTMAN_ID: {
    type: String,
    required: false,
    default: "",
  },
});
const { visible, year, type, ARCHIVES_POSTMAN_ID } = useVModels(props);

const emits = defineEmits(["update:visible", "getData"]);

const data = reactive({
  form: {
    ARCHIVES_POSTMAN_ID: undefined,
    NAME: undefined,
    POST: undefined,
    PHONE: undefined,
    CARD_ID: undefined,
    EFFECTIVE_DATE: undefined,
    RETRAINING: undefined,
    YEAR: year.value,
  },
  isoccupation: [
    { value: "0", label: "专职" },
    { value: "1", label: "兼职" },
  ],
});

const rules = {
  NAME: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
  POST: [{ required: true, message: "岗位/操作项目不能为空", trigger: "blur" }],
  PHONE: [
    { required: true, message: "电话不能为空", trigger: "blur" },
    { 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: "请输入正确的手机号码",
    },
  ],
  CARD_ID: [{ required: true, message: "证书号不能为空", trigger: "blur" }],
  EFFECTIVE_DATE: [
    { required: true, message: "请选择证书有效期限", trigger: "blur" },
  ],
  RETRAINING: [{ required: true, message: "请选择复训日期", trigger: "blur" }],
};

const formRef = ref(null);

const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    const params = Object.assign({}, data.form);
    let respData;
    if (type.value === "add") {
      respData = await addArchivesPostman(params);
    } else if (type.value === "edit") {
      respData = await editArchivesPostman(params);
    }
    if (respData && respData.result === "success") {
      emits("getData");
      fnClose();
    }
  },
  { atBegin: true }
);

const fnClose = () => {
  emits("update:visible", false);
};

const fnGetData = async () => {
  if (
    ARCHIVES_POSTMAN_ID &&
    ARCHIVES_POSTMAN_ID.value &&
    ARCHIVES_POSTMAN_ID.value.length > 0
  ) {
    const respData = await getArchivesPostman({
      ARCHIVES_POSTMAN_ID: ARCHIVES_POSTMAN_ID.value,
    });
    if (respData && respData.pd) {
      data.form = respData.pd;
    }
  }
};
await fnGetData();
</script>

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