<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="企业id" prop="enterpriseId">
        <el-input v-model="formData.enterpriseId" placeholder="请输入企业id" />
      </el-form-item>
      <el-form-item label="发布用户id" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入发布用户id" />
      </el-form-item>
      <el-form-item label="工作地区" prop="areaId">
        <el-select v-model="formData.areaId" placeholder="请选择工作地区">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="职位名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入职位名称" />
      </el-form-item>
      <el-form-item label="职位类型id" prop="positionId">
        <el-select v-model="formData.positionId" placeholder="请选择职位类型id">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="招聘类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择招聘类型">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_JOB_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工作经验" prop="expType">
        <el-select v-model="formData.expType" placeholder="请选择工作经验">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="学历要求" prop="eduType">
        <el-select v-model="formData.eduType" placeholder="请选择学历要求">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="薪酬from" prop="payFrom">
        <el-input v-model="formData.payFrom" placeholder="请输入薪酬from" />
      </el-form-item>
      <el-form-item label="薪酬to" prop="payTo">
        <el-input v-model="formData.payTo" placeholder="请输入薪酬to" />
      </el-form-item>
      <el-form-item label="职位标签" prop="tagList">
        <el-input v-model="formData.tagList" placeholder="请输入职位标签" />
      </el-form-item>
      <el-form-item label="职位内容" prop="content">
        <Editor v-model="formData.content" height="150px" />
      </el-form-item>
      <el-form-item label="职位要求" prop="requirement">
        <el-input v-model="formData.requirement" placeholder="请输入职位要求" />
      </el-form-item>
      <el-form-item label="工作地址" prop="address">
        <el-input v-model="formData.address" placeholder="请输入工作地址" />
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input v-model="formData.longitude" placeholder="请输入经度" />
      </el-form-item>
      <el-form-item label="维度" prop="latitude">
        <el-input v-model="formData.latitude" placeholder="请输入维度" />
      </el-form-item>
      <el-form-item label="职位状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择职位状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { JobAdvertisedApi, JobAdvertisedVO } from '@/api/menduner/system/job'

/** 门墩儿-招聘职位 表单 */
defineOptions({ name: 'JobAdvertisedForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  enterpriseId: undefined,
  userId: undefined,
  areaId: undefined,
  name: undefined,
  positionId: undefined,
  type: undefined,
  expType: undefined,
  eduType: undefined,
  payFrom: undefined,
  payTo: undefined,
  tagList: undefined,
  content: undefined,
  requirement: undefined,
  address: undefined,
  longitude: undefined,
  latitude: undefined,
  status: undefined
})
const formRules = reactive({
  enterpriseId: [{ required: true, message: '企业id不能为空', trigger: 'blur' }],
  userId: [{ required: true, message: '发布用户id不能为空', trigger: 'blur' }],
  areaId: [{ required: true, message: '工作地区不能为空', trigger: 'change' }],
  name: [{ required: true, message: '职位名称不能为空', trigger: 'blur' }],
  positionId: [{ required: true, message: '职位类型id不能为空', trigger: 'change' }],
  type: [{ required: true, message: '招聘类型不能为空', trigger: 'change' }],
  expType: [{ required: true, message: '工作经验不能为空', trigger: 'change' }],
  eduType: [{ required: true, message: '学历要求不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await JobAdvertisedApi.getJobAdvertised(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as JobAdvertisedVO
    if (formType.value === 'create') {
      await JobAdvertisedApi.createJobAdvertised(data)
      message.success(t('common.createSuccess'))
    } else {
      await JobAdvertisedApi.updateJobAdvertised(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    enterpriseId: undefined,
    userId: undefined,
    areaId: undefined,
    name: undefined,
    positionId: undefined,
    type: undefined,
    expType: undefined,
    eduType: undefined,
    payFrom: undefined,
    payTo: undefined,
    tagList: undefined,
    content: undefined,
    requirement: undefined,
    address: undefined,
    longitude: undefined,
    latitude: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}
</script>