<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >
      <el-form-item label="指标名称" prop="name">
        <el-input v-model="formData.name" class="!w-240px" clearable placeholder="请输入指标名称" />
      </el-form-item>
      <el-form-item label="指标值" prop="value">
        <el-input v-model="formData.value" class="!w-240px" clearable placeholder="请输入指标值" />
      </el-form-item>
      <el-form-item label="指标类型" prop="type">
        <el-select
          v-model="formData.type"
          class="!w-240px"
          clearable
          placeholder="请选择指标类型"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_METRIC_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="指标值类型" prop="valueType">
        <el-select
          v-model="formData.valueType"
          class="!w-240px"
          clearable
          placeholder="请选择指标值类型"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_METRIC_VALUE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" class="!w-240px" clearable placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup>
defineOptions({ name: 'SaveForm'})
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { getEventMetricDetails, saveEventMetric, updateEventMetric } from '@/api/menduner/reward/eventIndicators'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false)
const formLoading = ref(false)
const dialogTitle = ref('')
const formType = ref('')
const formData = ref({
    name: '',
    value: '',
    type: 0,
    remark: '',
    status: 0,
    valueType: 0
  })

const formRules = reactive({
  name: [{ required: true, message: '指标名称不能为空', trigger: 'blur' }],
  value: [{ required: true, message: '指标值不能为空', trigger: 'blur' }],
  type: [{ required: true, message: '指标分类不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
  valueType: [{ required: true, message: '指标值类型不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type, id) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()

  if (id) {
    formLoading.value = true
    try {
      formData.value = await getEventMetricDetails(id)
      formData.value.status = Number(formData.value.status)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const emit = defineEmits(['success'])

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name: '',
    value: '',
    type: 0,
    remark: '',
    status: 0,
    valueType: 0
  }
  formRef.value?.resetFields()
}

// 提交
const submitForm = async () => {
  await formRef.value.validate()
  formLoading.value = true
  try {
    if (formType.value === 'create') {
      await saveEventMetric(formData.value)
      message.success(t('common.createSuccess'))
    } else {
      await updateEventMetric(formData.value)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>