<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="PHONE">
          <el-input
            v-model="data.form.PHONE"
            maxlength="255"
            placeholder="这里输入电话"
          />
        </el-form-item>
        <el-form-item label="从事本专业工作年限" prop="WORK_YEAR">
          <el-input-number
            :min="0"
            v-model.number="data.form.WORK_YEAR"
            maxlength="11"
            placeholder="这里输入从事本专业工作年限"
          />
        </el-form-item>
        <el-form-item label="专/兼职" prop="OCCUPATION">
          <el-select v-model="data.form.OCCUPATION" title="请选择专/兼职">
            <el-option
              v-for="item in data.isoccupation"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </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="ASSESSMENT_DEPT">
          <el-input
            v-model="data.form.ASSESSMENT_DEPT"
            maxlength="255"
            placeholder="这里输入考核部门"
          />
        </el-form-item>
        <el-form-item label="考核日期" prop="ASSESSMENT_DATE">
          <el-date-picker
            v-model="data.form.ASSESSMENT_DATE"
            value-format="YYYY-MM-DD"
            format="YYYY-MM-DD"
            type="date"
            clearable
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="考核结果" prop="ASSESSMENT_RESULT">
          <el-input
            v-model="data.form.ASSESSMENT_RESULT"
            maxlength="255"
            placeholder="这里输入考核结果"
          />
        </el-form-item>
        <el-form-item label="备注" prop="DESCR">
          <el-input
            v-model="data.form.DESCR"
            maxlength="255"
            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 {
  getArchivesTeacher,
  addArchivesTeacher,
  editArchivesTeacher,
} from "@/request/archives.js";

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

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

const data = reactive({
  form: {
    ARCHIVESTEACHER_ID: undefined,
    NAME: undefined,
    PHONE: undefined,
    WORK_YEAR: undefined,
    OCCUPATION: undefined,
    CARD_ID: undefined,
    ASSESSMENT_DEPT: undefined,
    ASSESSMENT_DATE: undefined,
    DESCR: undefined,
    YEAR: year.value,
  },
  isoccupation: [
    { value: "0", label: "专职" },
    { value: "1", label: "兼职" },
  ],
});

const rules = {
  NAME: [{ 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: "请输入正确的手机号码",
    },
  ],
  WORK_YEAR: [
    {
      required: true,
      message: "从事本专业工作年限不能为空",
      trigger: "change",
    },
    { type: "number", message: "从事本专业工作年限必须为数字" },
  ],
  OCCUPATION: [{ required: true, message: "专/兼职不能为空", trigger: "blur" }],
  CARD_ID: [{ required: true, message: "证书编号不能为空", trigger: "blur" }],
  ASSESSMENT_DEPT: [
    { required: true, message: "考核部门不能为空", trigger: "blur" },
  ],
  ASSESSMENT_DATE: [
    { required: true, message: "请选择考核日期", trigger: "blur" },
  ],
  ASSESSMENT_RESULT: [
    { 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 addArchivesTeacher(params);
    } else if (type.value === "edit") {
      respData = await editArchivesTeacher(params);
    }
    if (respData && respData.result === "success") {
      emits("getData");
      fnClose();
    }
  },
  { atBegin: true }
);

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

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

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