<template>
  <el-dialog v-model="dialogVisible" :title="isCreate ? '新增快捷评语' : '编辑快捷评语'" width="600px" :close-on-click-modal="false">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="评语内容" prop="content">
        <el-input v-model="formData.content" type="textarea" rows="4" placeholder="请输入评语内容" />
      </el-form-item>
      <el-form-item label="评语类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择评语类型" class="!w-full">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.PRISON_REPORT_COMMENT_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="适用维度" prop="dimension">
        <el-input v-model="formData.dimension" placeholder="请输入适用维度，如：服刑表现、心理状态" />
      </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.PRISON_COMMON_STATUS)" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { ReportCommentApi, ReportComment } from '@/api/prison/evaluation'

defineOptions({ name: 'CommentForm' })

const emit = defineEmits(['success'])

const dialogVisible = ref(false)
const isCreate = ref(true)
const submitLoading = ref(false)
const formRef = ref()

const formData = reactive({
  id: undefined,
  content: '',
  type: undefined as number | undefined,
  dimension: '',
  status: 1
})

const rules = {
  content: [{ required: true, message: '评语内容不能为空', trigger: 'blur' }],
  type: [{ required: true, message: '评语类型不能为空', trigger: 'change' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'change' }]
}

const open = (type: string, id?: number) => {
  dialogVisible.value = true
  isCreate.value = type === 'create'
  resetForm()
  if (id) {
    loadData(id)
  }
}

const resetForm = () => {
  formData.id = undefined
  formData.content = ''
  formData.type = undefined
  formData.dimension = ''
  formData.status = 1
}

const loadData = async (id: number) => {
  const data = await ReportCommentApi.getComment(id)
  if (data) {
    formData.id = data.id
    formData.content = data.content
    formData.type = data.type
    formData.dimension = data.dimension || ''
    formData.status = data.status
  }
}

const handleSubmit = async () => {
  try {
    await formRef.value?.validate()
    submitLoading.value = true
    if (isCreate.value) {
      await ReportCommentApi.createComment(formData as ReportComment)
      message.success('创建成功')
    } else {
      await ReportCommentApi.updateComment(formData as ReportComment)
      message.success('更新成功')
    }
    dialogVisible.value = false
    emit('success')
  } finally {
    submitLoading.value = false
  }
}

defineExpose({ open })
</script>
