<template>
  <el-card header="佣金比例配置" style="width: 100%;" shadow="never">
    <template #header>
      <CardTitle title="佣金比例配置" />
    </template>
    <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-item>
        <el-button v-hasPermi="['menduner:system:hire-commission-ratio:save']"  @click="submitForm" type="primary">保 存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
defineOptions({ name: 'CommissionRatio'})
import * as CommissionRatioApi from '@/api/menduner/system/hire/commissionRatio/index'

const formData = ref({
  id: null,
  headhuntRate: 0,
  recommendRate: 0,
  cvRate: 0
})

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

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 formRef = ref()
const formLoading = ref(false)
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 resetForm = () => {
  formData.value = {
    id: null,
    headhuntRate: 0,
    recommendRate: 0,
    cvRate: 0
  }
}

// 获取佣金比例数据
const getRatioData = async () => {
  const data = await CommissionRatioApi.getCommissionRatio()
  if (!data || !Object.keys(data).length) return
  formData.value = data
}
getRatioData()

// 编辑
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    await CommissionRatioApi.saveCommissionRatio(formData.value)
    message.success(t('common.updateSuccess'))
    await getRatioData()
  } finally {
    formLoading.value = false
  }
}
</script>
