<template>
  <el-dialog
    title="编辑评语"
    v-model="visible"
    :width="700"
    :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.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.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.schoolYear" disabled style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="学期">
            <el-input v-model="form.schoolTerm" disabled style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="评语" prop="comment">
            <el-input
              v-model="form.comment"
              type="textarea"
              :rows="6"
              placeholder="请输入评语"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="家长寄语" prop="parentalMsg">
            <el-input
              v-model="form.parentalMsg"
              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="QualityReportEditDialog">
import { ref, reactive, nextTick } from 'vue'
import { useMessage } from '/@/hooks/message'
import { updatePY } from '/@/api/ems/qualityReport'
import { getDicts } from '/@/api/admin/dict'

const emit = defineEmits(['refresh'])

// 定义变量内容
const dataFormRef = ref()
const visible = ref(false)
const loading = ref(false)
const schoolTermList = ref<any[]>([])

// 提交表单数据
const form = reactive({
  stuNo: '',
  realName: '',
  schoolYear: '',
  schoolTerm: '',
  comment: '',
  parentalMsg: ''
})

// 定义校验规则
const dataRules = {
  // comment 和 parentalMsg 非必填，不需要验证规则
}

// 格式化学期
const formatSchoolTerm = (value: string | number) => {
  if (value === null || value === undefined || value === '') {
    return '-'
  }
  const dictItem = schoolTermList.value.find(item => item.value == value)
  return dictItem ? dictItem.label : value
}

// 打开弹窗
const openDialog = async (row: any) => {
  visible.value = true
  
  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields()
    form.stuNo = row.stuNo || ''
    form.realName = row.realName || ''
    form.schoolYear = row.schoolYear || ''
    form.schoolTerm = row.schoolTerm || ''
    form.comment = row.comment || ''
    form.parentalMsg = row.parentalMsg || ''
  })
}

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

// 获取学期字典
const getSchoolTermDict = async () => {
  try {
    const res = await getDicts('school_term')
    if (res.data && Array.isArray(res.data)) {
      schoolTermList.value = res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      }))
    } else {
      schoolTermList.value = []
    }
  } catch (err) {
    console.error('获取学期字典失败', err)
    schoolTermList.value = []
  }
}

// 初始化
getSchoolTermDict()

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

<style scoped lang="scss">
</style>

