<template>
  <Dialog title="佣金比例配置" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="170px"
      v-loading="formLoading"
    >
      <el-form-item label="猎头佣金比例" prop="headhuntRate">
        <el-input-number v-model="formData.headhuntRate" :min="0" :precision="2" />
      </el-form-item>
      <el-form-item label="推荐人佣金比例" prop="recommendRate">
        <el-input-number v-model="formData.recommendRate" :min="0" :precision="2" />
      </el-form-item>
      <el-form-item label="投递人佣金比例" prop="cvRate">
        <el-input-number v-model="formData.cvRate" :min="0" :precision="2" />
      </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 * as CommissionRatioApi from '@/api/menduner/system/hire/commissionRatio/index'

defineOptions({ name: 'JobRadioForm' })
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  id: null,
  headhuntRate: 0,
  recommendRate: 0,
  cvRate: 0
})

var checkHeadhuntRate = (rule, value, callback) => {
  if (!value) return callback(new Error('猎头佣金比例不能空'))
  if (value >= 0 && value <= 100) {
    if ((value + formData.value.cvRate + formData.value.recommendRate) !== 100) return callback(new Error('三者加起来需等于100%'))
    callback()
  }
  else callback(new Error('请输入0-100之间的数值'))
}

var checkRecommendRate = (rule, value, callback) => {
  if (!value) return callback(new Error('推荐人佣金比例不能空'))
  if (value >= 0 && value <= 100) {
    if ((value + formData.value.headhuntRate + formData.value.cvRate) !== 100) return callback(new Error('三者加起来需等于100%'))
    callback()
  }
  else callback(new Error('请输入0-100之间的数值'))
}

var checkCvRate = (rule, value, callback) => {
  if (!value) return callback(new Error('投递人佣金比例不能空'))
  if (value >= 0 && value <= 100) {
    if ((value + formData.value.headhuntRate + formData.value.recommendRate) !== 100) return callback(new Error('三者加起来需等于100%'))
    callback()
  }
  else callback(new Error('请输入0-100之间的数值'))
}
const formRules = reactive({
  headhuntRate: [{ validator: checkHeadhuntRate, trigger: 'change', required: true }],
  recommendRate: [{ validator: checkRecommendRate, trigger: 'change', required: true }],
  cvRate: [{ validator: checkCvRate, trigger: 'change', required: true }]
})
const formRef = ref() // 表单 Ref


/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  resetForm()
  // 修改时，设置数据
  formLoading.value = true
  try {
    formData.value = await CommissionRatioApi.getCommissionRatio()
  } finally {
    formLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

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

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: null,
    headhuntRate: 0,
    recommendRate: 0,
    cvRate: 0
  }
  formRef.value?.resetFields()
}
</script>