<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :width="600" :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="100px" v-loading="loading">
      <el-form-item label="楼号" prop="buildingNo">
        <el-input v-model="form.buildingNo" placeholder="请输入楼号" />
      </el-form-item>
      <el-form-item label="层数" prop="layers">
        <el-input-number 
          v-model="form.layers" 
          :min="1" 
          :max="20" 
          placeholder="请输入层数"
          style="width: 100%" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入电话" />
      </el-form-item>
    </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="DormBuildingFormDialog">
import { ref, reactive, nextTick } from 'vue'
import { useMessage } from '/@/hooks/message'
import { addObj, putObj, getObj } from '/@/api/stuwork/dormbuilding'

const emit = defineEmits(['refresh'])

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

// 提交表单数据
const form = reactive({
  id: '',
  buildingNo: '',
  layers: undefined as number | undefined,
  phone: ''
})

// 定义校验规则
const dataRules = {
  buildingNo: [
    { required: true, message: '请输入楼号', trigger: 'blur' }
  ],
  layers: [
    { required: true, message: '请输入层数', trigger: 'blur' },
    { type: 'number', min: 1, max: 20, message: '层数必须在1-20之间', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入电话', trigger: 'blur' }
  ]
}

// 打开弹窗
const openDialog = (type: string = 'add', row?: any) => {
  visible.value = true
  operType.value = type
  
  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields()
    form.id = ''
    form.buildingNo = ''
    form.layers = undefined
    form.phone = ''
    
    // 编辑时填充数据
    if (type === 'edit' && row) {
      form.id = row.id
      form.buildingNo = row.buildingNo || ''
      form.layers = row.layers
      form.phone = row.phone || ''
    }
  })
}

// 提交表单
const onSubmit = async () => {
  if (!dataFormRef.value) return
  
  await dataFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    
    loading.value = true
    try {
      if (operType.value === 'add') {
        await addObj({
          buildingNo: form.buildingNo,
          layers: form.layers,
          phone: form.phone
        })
        useMessage().success('新增成功')
      } else {
        await putObj({
          id: form.id,
          buildingNo: form.buildingNo,
          layers: form.layers,
          phone: form.phone
        })
        useMessage().success('编辑成功')
      }
      visible.value = false
      emit('refresh')
    } catch (err: any) {
      useMessage().error(err.msg || (operType.value === 'add' ? '新增失败' : '编辑失败'))
    } finally {
      loading.value = false
    }
  })
}

// 暴露方法给父组件
defineExpose({
  openDialog
})
</script>

