<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="700px">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" v-loading="formLoading">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="维度名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入维度名称，如：消费行为分析" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维度编码" prop="code">
            <el-input v-model="formData.code" placeholder="请输入维度编码" :disabled="formType === 'update'" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维度类型" prop="dimensionType">
            <el-select v-model="formData.dimensionType" placeholder="请选择维度类型" class="!w-100%">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.PRISON_EVALUATION_DIMENSION_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="评估方式" prop="evaluationMethod">
            <el-select v-model="formData.evaluationMethod" placeholder="请选择评估方式" class="!w-100%">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.PRISON_EVALUATION_METHOD)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="输出格式" prop="outputFormat">
            <el-select v-model="formData.outputFormat" placeholder="请选择输出格式" class="!w-100%">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.PRISON_EVALUATION_OUTPUT_FORMAT)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="编辑器类型" prop="editorType">
            <el-select v-model="formData.editorType" placeholder="请选择编辑器类型" class="!w-100%">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.PRISON_EVALUATION_EDITOR_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="formData.sort" :min="0" controls-position="right" class="!w-100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="formData.status">
              <el-radio
                v-for="dict in getIntDictOptions(DICT_TYPE.PRISON_COMMON_STATUS)"
                :key="dict.value"
                :value="dict.value"
              >{{ dict.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否AI生成" prop="aiEnabled">
            <el-switch v-model="formData.aiEnabled" :active-value="1" :inactive-value="0" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否必填" prop="isRequired">
            <el-switch v-model="formData.isRequired" :active-value="1" :inactive-value="0" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据源绑定" prop="dataSources">
            <el-select v-model="formData.dataSources" multiple placeholder="请选择数据源" class="!w-100%">
              <el-option label="罪犯档案" value="prisoner" />
              <el-option label="消费记录" value="consumption" />
              <el-option label="计分考核" value="score" />
              <el-option label="问卷测评" value="questionnaire" />
              <el-option label="风险评估" value="risk" />
              <el-option label="违规记录" value="violation" />
              <el-option label="奖励记录" value="reward" />
              <el-option label="会见记录" value="visit" />
              <el-option label="劳动数据" value="labor" />
              <el-option label="家庭帮教" value="family" />
              <el-option label="心理测评" value="psychology" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="维度描述" prop="description">
            <el-input v-model="formData.description" type="textarea" :rows="2" placeholder="请输入维度描述" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="AI提示词" prop="aiPrompt">
            <el-input v-model="formData.aiPrompt" type="textarea" :rows="4" placeholder="请输入AI提示词，用于指导AI生成该维度的内容" />
          </el-form-item>
        </el-col>
      </el-row>
    </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 { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { DimensionApi, DimensionVO } from '@/api/prison/evaluation-report'

defineOptions({ name: 'EvaluationDimensionForm' })

const { t } = useI18n()
const message = useMessage()

const dialogVisible = ref(false)
const dialogTitle = ref('')
const formLoading = ref(false)
const formType = ref('')
const formData = ref<DimensionVO>({
  id: undefined,
  templateId: 0,
  name: '',
  code: '',
  description: '',
  dimensionType: undefined,
  aiPrompt: '',
  dataSources: [],  // 改为空数组
  questions: [],  // 问题列表也改为空数组
  outputFormat: 1,
  aiEnabled: 0,
  editorType: 1,
  sort: 0,
  isRequired: 1,
  status: 1
})
const formRules = reactive({
  name: [{ required: true, message: '维度名称不能为空', trigger: 'blur' }],
  dimensionType: [{ required: true, message: '维度类型不能为空', trigger: 'change' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'change' }]
})
const formRef = ref()

const open = async (type: string, templateId: number, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.templateId = templateId
  if (id) {
    formLoading.value = true
    try {
      const existingDimension = await DimensionApi.getDimension(id)
      formData.value = { ...existingDimension, templateId }
    } finally {
      formLoading.value = false
    }
  }
}

defineExpose({ open })

const emit = defineEmits(['success'])

const submitForm = async () => {
  await formRef.value.validate()
  formLoading.value = true
  try {
    // 创建提交数据的副本，确保数组字段被正确传递
    const submitData = {
      ...formData.value,
      dataSources: formData.value.dataSources || [],
      questions: formData.value.questions || []
    }
    if (formType.value === 'create') {
      await DimensionApi.createDimension(submitData)
      message.success(t('common.createSuccess'))
    } else {
      await DimensionApi.updateDimension(submitData)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const resetForm = () => {
  formData.value = {
    id: undefined,
    templateId: formData.value.templateId,
    name: '',
    code: '',
    description: '',
    dimensionType: undefined,
    aiPrompt: '',
    dataSources: [],  // 改为空数组
    questions: [],  // 问题列表也改为空数组
    outputFormat: 1,
    aiEnabled: 0,
    editorType: 1,
    sort: 0,
    isRequired: 1,
    status: 1
  }
  formRef.value?.resetFields()
}
</script>
