<template>
  <el-dialog 
    title="干预结果" 
    v-model="visible" 
    :width="600" 
    :close-on-click-modal="false" 
    draggable>
    <el-form 
      ref="dataFormRef" 
      :model="form" 
      :rules="dataRules" 
      label-width="120px" 
      v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item label="学生姓名">
            <el-input v-model="form.realName" disabled style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="学号">
            <el-input v-model="form.stuNo" disabled style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="班级">
            <el-input v-model="form.classNo" disabled style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="干预结果" prop="result">
            <el-input 
              v-model="form.result" 
              type="textarea" 
              :rows="6" 
              placeholder="请输入干预结果"
              style="width: 100%" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="StuCareResultDialog">
import { ref, reactive, nextTick } from 'vue'
import { useMessage } from '/@/hooks/message'
import { updateResult, getDetail } from '/@/api/stuwork/stucare'

const emit = defineEmits(['refresh'])

// 定义变量内容
const dataFormRef = ref()
const visible = ref(false)
const loading = ref(false)

// 提交表单数据
const form = reactive({
  id: '',
  realName: '',
  stuNo: '',
  classNo: '',
  result: ''
})

// 定义校验规则
const dataRules = {
  result: [
    { required: true, message: '请输入干预结果', trigger: 'blur' }
  ]
}

// 打开弹窗
const openDialog = async (row: any) => {
  visible.value = true
  
  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields()
    form.id = ''
    form.realName = ''
    form.stuNo = ''
    form.classNo = ''
    form.result = ''
    
    if (row) {
      form.id = row.id
      form.realName = row.realName || ''
      form.stuNo = row.stuNo || ''
      form.classNo = row.classNo || ''
      form.result = row.result || ''
      
      // 如果没有干预结果，尝试获取详情
      if (row.id && !row.result) {
        loading.value = true
        getDetail(row.id).then((res: any) => {
          if (res.data) {
            form.result = res.data.result || ''
          }
        }).catch((err) => {
          console.error('获取详情失败', err)
        }).finally(() => {
          loading.value = false
        })
      }
    }
  })
}

// 提交表单
const onSubmit = async () => {
  if (!dataFormRef.value) return
  
  await dataFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    
    loading.value = true
    try {
      await updateResult({
        id: form.id,
        result: form.result
      })
      useMessage().success('保存成功')
      visible.value = false
      emit('refresh')
    } catch (err: any) {
      useMessage().error(err.msg || '保存失败')
    } finally {
      loading.value = false
    }
  })
}

// 暴露方法
defineExpose({
  openDialog
})
</script>

<style scoped lang="scss">
.mb20 {
  margin-bottom: 20px;
}
</style>

