<template>
  <el-dialog :modelValue="show" :title="title" width="25%" :before-close="handleClose">
    <el-form ref="formRef" :model="form" :rules="rules" hide-required-asterisk label-position="top">
      <el-form-item v-if="type === 'SkillCertificate'" label="" prop="FSkillCertificate">
        <el-input v-model="form.FSkillCertificate" size="large" :placeholder="placeholder" />
      </el-form-item>
      <el-form-item v-else label="" prop="FCredentials">
        <el-input v-model="form.FCredentials" size="large" :placeholder="placeholder" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button size="large" style="width: 100px" @click="handleClose">取消</el-button>
      <el-button size="large" type="primary" style="width: 100px" @click="onConfirm"
        >完成</el-button
      >
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { updateResume } from '@/services/api/recruitmentManagement'

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  type: {
    type: String,
    default: 'FSkillCertificate'
  },
  title: {
    type: String,
    default: '添加证书'
  },
  placeholder: {
    type: String,
    default: '请输入证书'
  },
  data: {
    type: String,
    default: ''
  }
})

const emits = defineEmits(['update:show', 'init'])

const form = ref({
  FSkillCertificate: '',
  FCredentials: ''
})

const validateName = (rule: any, value: string, callback: any) => {
  if (value.includes(',')) {
    callback(new Error('证书名称不能包含逗号'))
  } else {
    callback()
  }
}

const rules = ref({
  FSkillCertificate: [
    { required: true, message: '技能证书名称不能为空', trigger: 'blur' },
    { validator: validateName, trigger: 'blur' }
  ],
  FCredentials: [
    { required: true, message: '资料证书名称不能为空', trigger: 'blur' },
    { validator: validateName, trigger: 'blur' }
  ]
})

const formRef = ref()

const onConfirm = async () => {
  await formRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      let params
      if (props.type === 'SkillCertificate') {
        params = {
          NeedUpDateFields: ['FSkillCertificate'],
          IsDeleteEntry: 'false',
          Model: {
            FID: 0,
            FSkillCertificate:
              props.data !== ' '
                ? props.data + ',' + form.value.FSkillCertificate
                : form.value.FSkillCertificate //技能证书
          }
        }
      } else {
        params = {
          NeedUpDateFields: ['FCredentials'],
          IsDeleteEntry: 'false',
          Model: {
            FID: 0,
            FCredentials:
              props.data !== ' '
                ? props.data + ',' + form.value.FCredentials
                : form.value.FCredentials
          }
        }
      }
      const res: any = await updateResume({ Parmeter: params })
      if (res.code === 200) {
        ElNotification.success({
          title: '添加证书',
          message: '添加成功'
        })
        handleClose()
        emits('init')
      }
    }
  })
}

const handleClose = () => {
  formRef.value?.resetFields()
  emits('update:show', false)
}

defineExpose({
  form
})
</script>

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