<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="110px"
      v-loading="formLoading"
    >
      <el-form-item label="套餐名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入套餐名称" />
      </el-form-item>
      <el-form-item v-if="!hidden" label="是否激活VIP" prop="activateVip">
        <el-switch v-model="formData.activateVip" />
      </el-form-item>
      <el-form-item v-if="!hidden" label="套餐原价(元)" prop="originalPrice">
        <el-input-number v-model="formData.originalPrice" :min="1" :step="100" />
      </el-form-item>
      <el-form-item v-if="!hidden" label="套餐价格(元)" prop="price">
        <el-input-number v-model="formData.price" :min="1" :step="100" />
      </el-form-item>
      <el-form-item v-if="!hidden" label="会员时效(天)" prop="day">
        <el-input-number v-model="formData.day" :min="0" :step="100" />
      </el-form-item>
      <el-form-item label="说明文本" prop="text">
        <el-input v-model="formData.text" placeholder="请输入说明文本" />
      </el-form-item>
      <el-form-item label="发布职位数" prop="publishJobCount">
        <el-input-number v-model="formData.publishJobCount" :min="0" :step="10" />
      </el-form-item>
      <!-- <el-form-item v-if="!hidden" label="搜索人才数" prop="searchCount">
        <el-input-number v-model="formData.searchCount" :min="0" :step="10" />
      </el-form-item> -->
      <el-form-item label="查看简历数" prop="lookCvCount">
        <el-input-number v-model="formData.lookCvCount" :min="0" :step="10" />
      </el-form-item>
      <el-form-item v-if="!hidden" label="开启人才地图" prop="personMap">
        <el-switch v-model="formData.personMap" />
      </el-form-item>
      <el-form-item v-if="!hidden" label="发布众聘职位" prop="hireJob">
        <el-switch v-model="formData.hireJob" />
      </el-form-item>
      <!-- <el-form-item v-if="!hidden" label="开启新任命" prop="newAppointment">
        <el-switch v-model="formData.newAppointment" />
      </el-form-item> -->
      <el-form-item v-if="!hidden" label="参加招聘会" prop="jobFair">
        <el-switch v-model="formData.jobFair" />
      </el-form-item>
      <!-- <el-form-item v-if="!hidden" label="排序" prop="sort">
        <el-input-number v-model="formData.sort" :min="0" :step="1" />
      </el-form-item> -->
      <!-- <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="0">正常</el-radio>
          <el-radio value="1">停用</el-radio>
          <el-radio value="2">隐藏</el-radio>
        </el-radio-group>
      </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 { EnterprisePackageApi } from '@/api/menduner/system/enterprisePackage'

/** 企业套餐 表单 */
defineOptions({ name: 'PackageForm' })

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

// 默认套餐不展示部分表单项
const hidden = ref(false)
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  name: undefined,
  activateVip: false,
  originalPrice: 0,
  price: 0,
  day: 0,
  defaultPackage: false,
  recommend: false,
  text: undefined,
  publishJobCount: 0,
  searchCount: 0,
  lookCvCount: 0,
  personMap: false,
  hireJob: false,
  newAppointment: false,
  jobFair: false,
  sort: 0,
  status: '0'
})

// 基础验证规则
const baseRules = {
  name: [{ required: true, message: '套餐名称不能为空', trigger: 'blur' }],
  activateVip: [{ required: true, message: '是否激活VIP不能为空', trigger: 'change' }],
  originalPrice: [{ required: true, message: '套餐原价不能为空', trigger: 'blur' }],
  price: [{ required: true, message: '套餐价格不能为空', trigger: 'blur' }],
  day: [{ required: true, message: '会员时效不能为空', trigger: 'blur' }],
  text: [{ required: true, message: '说明文本不能为空', trigger: 'blur' }],
  publishJobCount: [{ required: true, message: '发布职位数不能为空', trigger: 'blur' }],
  lookCvCount: [{ required: true, message: '查看简历数不能为空', trigger: 'blur' }],
  personMap: [{ required: true, message: '开启人才地图不能为空', trigger: 'change' }],
  hireJob: [{ required: true, message: '发布众聘职位不能为空', trigger: 'change' }],
  jobFair: [{ required: true, message: '参加招聘会不能为空', trigger: 'change' }],
  // sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'change' }]
}

// 动态计算表单验证规则
const formRules = computed(() => {
  if (hidden.value) {
    // 当hidden为true时，只保留name、publishJobCount、text的必填验证
    return {
      name: baseRules.name,
      publishJobCount: baseRules.publishJobCount,
      text: baseRules.text,
      lookCvCount: baseRules.lookCvCount
    }
  }
  // 当hidden为false时，返回所有验证规则
  return baseRules
})

const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  hidden.value = false
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      const data = await EnterprisePackageApi.getPackageDetail(id)
      hidden.value = data.defaultPackage
      // 将价格除以100进行展示
      data.originalPrice = data.originalPrice ? data.originalPrice / 100 : 0
      data.price = data.price ? data.price / 100 : 0
      formData.value = data
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()

  // 提交请求
  formLoading.value = true
  try {
    const submitData = { ...formData.value }
    // 将价格乘以100提交给接口
    submitData.originalPrice = submitData.originalPrice ? submitData.originalPrice * 100 : 0
    submitData.price = submitData.price ? submitData.price * 100 : 0

    if (formType.value === 'create') {
      await EnterprisePackageApi.createPackage(submitData)
      message.success(t('common.createSuccess'))
    } else {
      await EnterprisePackageApi.updatePackage(submitData)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name: undefined,
    activateVip: false,
    originalPrice: 0,
    price: 0,
    day: 0,
    defaultPackage: false,
    recommend: false,
    text: undefined,
    publishJobCount: 0,
    searchCount: 0,
    lookCvCount: 0,
    personMap: false,
    hireJob: false,
    newAppointment: false,
    jobFair: false,
    sort: 0,
    status: '0'
  }
  formRef.value?.resetFields()
}
</script>