<template>
  <el-dialog 
    title="教室安排" 
    v-model="visible" 
    :width="600" 
    :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="buildingNo">
            <el-select 
              v-model="form.buildingNo" 
              placeholder="请选择楼号" 
              clearable
              filterable
              style="width: 100%">
              <el-option
                v-for="item in buildingList"
                :key="item.buildingNo"
                :label="item.buildingNo"
                :value="item.buildingNo">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="教室位置" prop="position">
            <el-select 
              v-model="form.position" 
              placeholder="请选择教室位置" 
              clearable
              filterable
              style="width: 100%">
              <el-option
                v-for="item in classroomList"
                :key="item.position"
                :label="item.position"
                :value="item.position">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="班级" prop="classCode">
            <el-select 
              v-model="form.classCode" 
              placeholder="请选择班级" 
              clearable
              filterable
              style="width: 100%">
              <el-option
                v-for="item in classList"
                :key="item.classCode"
                :label="item.classNo"
                :value="item.classCode">
              </el-option>
            </el-select>
          </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="ClassroomBaseArrangeDialog">
import { ref, reactive, nextTick, onMounted } from 'vue'
import { useMessage } from '/@/hooks/message'
import { addClassRoomAssign } from '/@/api/stuwork/teachclassroomassign'
import { queryAllClass } from '/@/api/basic/basicclass'
import { getClassRoomList } from '/@/api/stuwork/teachclassroom'
import { getBuildingList } from '/@/api/stuwork/teachbuilding'

const emit = defineEmits(['refresh'])

// 定义变量内容
const dataFormRef = ref()
const visible = ref(false)
const loading = ref(false)
const classList = ref<any[]>([])
const classroomList = ref<any[]>([])
const buildingList = ref<any[]>([])

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

// 定义校验规则
const dataRules = {
  buildingNo: [
    { required: true, message: '请选择楼号', trigger: 'change' }
  ],
  position: [
    { required: true, message: '请选择教室位置', trigger: 'change' }
  ],
  classCode: [
    { required: true, message: '请选择班级', trigger: 'change' }
  ]
}

// 打开弹窗
const openDialog = async (row: any) => {
  visible.value = true
  resetForm()
  
  // 填充现有数据
  if (row) {
    form.id = row.id || ''
    form.buildingNo = row.buildingNo || ''
    form.classCode = row.classCode || ''
    form.position = row.position || ''
  }
  
  nextTick(() => {
    dataFormRef.value?.clearValidate()
  })
}

// 重置表单
const resetForm = () => {
  form.id = ''
  form.buildingNo = ''
  form.classCode = ''
  form.position = ''
}

// 提交表单
const onSubmit = async () => {
  if (!dataFormRef.value) return
  
  await dataFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    
    loading.value = true
    try {
      await addClassRoomAssign({
        buildingNo: form.buildingNo,
        position: form.position,
        classCode: form.classCode
      })
      useMessage().success('教室安排成功')
      visible.value = false
      emit('refresh')
    } catch (_err) {
      // 错误由 request 拦截器统一提示
    } finally {
      loading.value = false
    }
  })
}

// 获取班级列表
const getClassListData = async () => {
  try {
    const res = await queryAllClass()
    if (res.data) {
      classList.value = Array.isArray(res.data) ? res.data : []
    }
  } catch (err) {
    classList.value = []
  }
}

// 获取教室列表
const getClassroomListData = async () => {
  try {
    const res = await getClassRoomList()
    if (res.data) {
      classroomList.value = Array.isArray(res.data) ? res.data : []
    }
  } catch (err) {
    classroomList.value = []
  }
}

// 获取楼号列表
const getBuildingListData = async () => {
  try {
    const res = await getBuildingList()
    if (res.data) {
      buildingList.value = Array.isArray(res.data) ? res.data : []
    }
  } catch (err) {
    buildingList.value = []
  }
}

// 初始化
onMounted(() => {
  getClassListData()
  getClassroomListData()
  getBuildingListData()
})

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

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

