<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="12" class="mb20">
          <el-form-item label="学号" prop="stuNo">
            <el-input 
              v-model="form.stuNo" 
              placeholder="请输入学号" 
              clearable
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="入团时间" prop="enterTime">
            <el-date-picker
              v-model="form.enterTime"
              type="date"
              placeholder="请输入入团时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="团员编号" prop="serNo">
            <el-input 
              v-model="form.serNo" 
              placeholder="请输入团员编号" 
              clearable
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="团内职务" prop="position">
            <el-input 
              v-model="form.position" 
              placeholder="请输入团内职务" 
              clearable
              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="StuUnionLeagueFormDialog">
import { ref, reactive, nextTick } from 'vue'
import { useMessage } from '/@/hooks/message'
import { addObj, editObj, getDetail } from '/@/api/stuwork/stuunionleague'

const emit = defineEmits(['refresh'])

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

// 提交表单数据
const form = reactive({
  id: '',
  stuNo: '',
  enterTime: '',
  serNo: '',
  position: ''
})

// 定义校验规则
const dataRules = {
  stuNo: [
    { required: true, message: '请输入学号', trigger: 'blur' }
  ],
  enterTime: [
    { required: true, message: '请输入入团时间', trigger: 'change' }
  ],
  serNo: [
    { 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.stuNo = ''
    form.enterTime = ''
    form.serNo = ''
    form.position = ''
    
    // 编辑时填充数据
    if (type === 'edit' && row) {
      form.id = row.id
      form.stuNo = row.stuNo || ''
      form.enterTime = row.enterTime || ''
      form.serNo = row.serNo || ''
      form.position = row.position || ''
      
      // 如果需要获取详情
      if (row.id && (!row.stuNo || !row.enterTime)) {
        loading.value = true
        getDetail(row.id).then((res: any) => {
          if (res.data) {
            form.stuNo = res.data.stuNo || ''
            form.enterTime = res.data.enterTime || ''
            form.serNo = res.data.serNo || ''
            form.position = res.data.position || ''
          }
        }).catch((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 = {
          stuNo: form.stuNo,
          enterTime: form.enterTime,
          serNo: form.serNo,
          position: form.position
        }
        
        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>

