<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="roomNo">
        <el-select 
          v-model="form.roomNo" 
          placeholder="请选择房间号" 
          clearable
          filterable
          style="width: 100%">
          <el-option
            v-for="item in roomList"
            :key="item.roomNo"
            :label="item.roomNo"
            :value="item.roomNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="整改时间" prop="reformDate">
        <el-date-picker
          v-model="form.reformDate"
          type="date"
          placeholder="选择日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          style="width: 100%" />
      </el-form-item>
      <el-form-item label="整改内容" prop="reformContent">
        <el-input 
          v-model="form.reformContent" 
          type="textarea" 
          :rows="4"
          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="DormReformFormDialog">
import { ref, reactive, nextTick, onMounted } from 'vue'
import { useMessage } from '/@/hooks/message'
import { addObj, putObj } from '/@/api/stuwork/dormreform'
import { getRoomList } from '/@/api/stuwork/dormroom'

const emit = defineEmits(['refresh'])

// 定义变量内容
const dataFormRef = ref()
const visible = ref(false)
const loading = ref(false)
const operType = ref('add') // add 或 edit
const roomList = ref<any[]>([])

// 提交表单数据
const form = reactive({
  id: '',
  roomNo: '',
  reformDate: '',
  reformContent: ''
})

// 定义校验规则
const dataRules = {
  roomNo: [
    { required: true, message: '请选择房间号', trigger: 'change' }
  ],
  reformDate: [
    { required: true, message: '请选择整改时间', trigger: 'change' }
  ],
  reformContent: [
    { 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.roomNo = ''
    form.reformDate = ''
    form.reformContent = ''
    
    // 编辑时填充数据
    if (type === 'edit' && row) {
      form.id = row.id
      form.roomNo = row.roomNo || ''
      // 处理日期格式
      if (row.reformDate) {
        form.reformDate = row.reformDate.split(' ')[0] || row.reformDate
      }
      form.reformContent = row.reformContent || ''
    }
  })
}

// 获取房间号列表
const getRoomListData = async () => {
  try {
    const res = await getRoomList()
    if (res.data) {
      roomList.value = Array.isArray(res.data) ? res.data : []
    }
  } catch (err) {
    roomList.value = []
  }
}

// 初始化
onMounted(() => {
  getRoomListData()
})

// 提交表单
const onSubmit = async () => {
  if (!dataFormRef.value) return
  
  await dataFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    
    loading.value = true
    try {
      const submitData: any = {
        roomNo: form.roomNo,
        reformDate: form.reformDate,
        reformContent: form.reformContent
      }
      
      if (operType.value === 'add') {
        await addObj(submitData)
        useMessage().success('新增成功')
      } else {
        submitData.id = form.id
        await putObj(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>

