<template>
  <el-dialog 
    :title="form.id ? '编辑活动' : '新增活动'" 
    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="活动主题" prop="activityTheme">
            <el-input 
              v-model="form.activityTheme" 
              placeholder="请输入活动主题" 
              clearable
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="活动说明" prop="remarks">
            <el-input 
              v-model="form.remarks" 
              type="textarea"
              :rows="4"
              placeholder="请输入活动说明" 
              clearable
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="活动兼报数" prop="maxSub">
            <el-input-number
              v-model="form.maxSub"
              :min="0"
              :max="100"
              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="ActivityInfoFormDialog">
import { ref, reactive, nextTick } from 'vue'
import { useMessage } from '/@/hooks/message'
import { addObj, editObj, getDetail } from '/@/api/stuwork/activityinfo'

const emit = defineEmits(['refresh'])

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

// 提交表单数据
const form = reactive({
  id: '',
  activityTheme: '',
  remarks: '',
  maxSub: undefined as number | undefined
})

// 定义校验规则
const dataRules = {
  activityTheme: [
    { required: true, message: '请输入活动主题', trigger: 'blur' }
  ],
  remarks: [
    { required: true, message: '请输入活动说明', trigger: 'blur' }
  ]
}

// 打开弹窗
const openDialog = async (type: string = 'add', row?: any) => {
  operType.value = type
  visible.value = true
  
  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields()
    form.id = ''
    form.activityTheme = ''
    form.remarks = ''
    form.maxSub = undefined
    
    // 编辑时填充数据
    if (type === 'edit' && row) {
      form.id = row.id
      form.activityTheme = row.activityTheme || ''
      form.remarks = row.remarks || ''
      form.maxSub = row.maxSub !== undefined && row.maxSub !== null ? row.maxSub : undefined
      
      // 如果需要获取详情
      if (row.id && (!row.activityTheme || !row.remarks)) {
        loading.value = true
        getDetail(row.id).then((res: any) => {
          if (res.data) {
            form.activityTheme = res.data.activityTheme || ''
            form.remarks = res.data.remarks || ''
            form.maxSub = res.data.maxSub !== undefined && res.data.maxSub !== null ? res.data.maxSub : undefined
          }
        }).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) {
      loading.value = true
      try {
        const submitData = {
          activityTheme: form.activityTheme,
          remarks: form.remarks,
          maxSub: form.maxSub !== undefined && form.maxSub !== null ? form.maxSub : undefined
        }
        
        if (operType.value === 'edit' && form.id) {
          await editObj({ ...submitData, id: form.id })
          useMessage().success('编辑成功')
        } else {
          await addObj(submitData)
          useMessage().success('新增成功')
        }
        
        visible.value = false
        emit('refresh')
      } catch (err: any) {
        useMessage().error(err.msg || (operType.value === 'edit' ? '编辑失败' : '新增失败'))
      } finally {
        loading.value = false
      }
    }
  })
}

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

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

