<template>
  <el-dialog 
    :title="form.id ? '编辑' : '新增'" 
    v-model="visible" 
    :width="800" 
    :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="标题" prop="title">
            <el-input
              v-model="form.title"
              placeholder="请输入标题"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="附件上传">
            <upload-file
              v-model="form.fileUrl"
              :limit="5"
              :fileSize="10"
              type="simple" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="内容" prop="content">
            <Editor
              v-model:getHtml="form.content"
              :height="'400'"
              placeholder="请输入内容" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="备注">
            <el-input
              v-model="form.remarks"
              type="textarea"
              :rows="4"
              placeholder="请输入备注"
              :maxlength="250"
              show-word-limit
              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="ClassThemeRecordFormDialog">
import { ref, reactive, nextTick, computed } from 'vue'
import { useMessage } from '/@/hooks/message'
import { addRecord, editRecord, getRecordDetail } from '/@/api/stuwork/classtheme'
import Editor from '/@/components/Editor/index.vue'
import UploadFile from '/@/components/Upload/index.vue'

const props = defineProps({
  currentRow: {
    type: Object,
    default: null
  }
})

const emit = defineEmits(['refresh'])

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

// 提交表单数据
const form = reactive({
  id: '',
  title: '',
  fileUrl: '',
  content: '',
  remarks: ''
})

// 定义校验规则
const dataRules = {
  title: [
    { required: true, message: '请输入标题', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '请输入内容', trigger: 'blur' }
  ]
}

// 打开弹窗
const openDialog = async (type: string = 'add', row?: any) => {
  visible.value = true
  operType.value = type
  
  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields()
    form.id = ''
    form.title = ''
    form.fileUrl = ''
    form.content = ''
    form.remarks = ''
    
    // 编辑时填充数据
    if (type === 'edit' && row) {
      form.id = row.id
      form.title = row.title || ''
      form.fileUrl = row.fileUrl || ''
      form.content = row.content || ''
      form.remarks = row.remarks || ''
      
      // 如果需要获取详情
      if (row.id && !row.content) {
        loading.value = true
        getRecordDetail(row.id).then((res: any) => {
          if (res.data) {
            form.content = res.data.content || ''
            form.fileUrl = res.data.fileUrl || ''
            form.remarks = res.data.remarks || ''
          }
        }).finally(() => {
          loading.value = false
        })
      }
    }
  })
}

// 提交表单
const onSubmit = async () => {
  if (!dataFormRef.value) return
  
  await dataFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    
    if (!props.currentRow) {
      useMessage().error('缺少必要信息')
      return
    }
    
    loading.value = true
    try {
      const submitData = {
        title: form.title,
        fileUrl: form.fileUrl || '',
        content: form.content,
        remarks: form.remarks || '',
        schoolYear: props.currentRow.schoolYear,
        schoolTerm: props.currentRow.schoolTerm,
        deptCode: props.currentRow.deptCode,
        classNo: props.currentRow.classNo
      }
      
      if (operType.value === 'add') {
        await addRecord(submitData)
        useMessage().success('新增成功')
      } else {
        await editRecord({
          id: form.id,
          ...submitData
        })
        useMessage().success('编辑成功')
      }
      visible.value = false
      emit('refresh')
    } catch (err: any) {
      useMessage().error(err.msg || (operType.value === 'add' ? '新增失败' : '编辑失败'))
    } finally {
      loading.value = false
    }
  })
}

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

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

