<template>
  <el-dialog v-model="visible" title="发起调宿申请" width="600px">
    <el-form ref="formRef" :model="form" label-width="100px">
      <el-form-item label="调宿模式">
        <el-radio-group v-model="form.type">
          <el-radio-button label="single">单人换房 (换至空床)</el-radio-button>
          <el-radio-button label="exchange">双人互换 (两两对调)</el-radio-button>
        </el-radio-group>
      </el-form-item>
      
      <el-form-item label="申请人">
        <el-select v-model="form.studentId" filterable remote :remote-method="remoteMethod" placeholder="请输入姓名/学号搜索" style="width: 100%">
          <el-option v-for="item in studentOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <template v-if="form.type === 'single'">
        <el-form-item label="意向楼宇">
           <el-select v-model="form.building" placeholder="请选择楼宇">
            <el-option label="北区1号楼" value="1" />
            <el-option label="南区A栋" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="意向房间">
          <el-cascader 
            v-model="form.targetBed" 
            :options="roomOptions" 
            placeholder="请选择具体床位"
            style="width: 100%" 
          />
        </el-form-item>
      </template>

      <template v-else>
        <el-form-item label="互换对象">
           <el-select v-model="form.targetStudentId" filterable remote :remote-method="remoteMethod" placeholder="搜索互换对象" style="width: 100%">
            <el-option v-for="item in studentOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <div class="text-xs text-gray mt-5">
            <vab-icon icon="information-line" /> 互换模式下，无需空床位，直接交换双方床位及门禁权限。
          </div>
        </el-form-item>
      </template>

      <el-form-item label="申请原因">
        <el-input v-model="form.reason" type="textarea" rows="3" placeholder="请详细说明调宿理由..." />
      </el-form-item>
      
      <el-form-item label="证明材料">
        <el-upload action="#" list-type="picture-card" :auto-upload="false">
          <el-icon><plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">提交申请</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const emit = defineEmits(['success'])

const visible = ref(false)
const studentOptions = ref([])

const form = reactive({
  type: 'single',
  studentId: '',
  building: '',
  targetBed: [],
  targetStudentId: '',
  reason: ''
})

const roomOptions = [
  {
    value: '3F',
    label: '3层',
    children: [
      {
        value: '305',
        label: '305室',
        children: [
          { value: '1', label: '1号床 (空闲)' },
          { value: '2', label: '2号床 (空闲)' },
        ]
      }
    ]
  }
]

const open = () => {
  Object.assign(form, {
    type: 'single', studentId: '', building: '', targetBed: [], targetStudentId: '', reason: ''
  })
  visible.value = true
}

const remoteMethod = (query) => {
  if (query) {
    setTimeout(() => {
      studentOptions.value = [
        { value: '31901001', label: '梁健 (31901001)' },
        { value: '31901002', label: '鲁永波 (31901002)' },
        { value: '31901003', label: '王应哲 (31901003)' },
      ]
    }, 200)
  } else {
    studentOptions.value = []
  }
}

const handleSubmit = () => {
  if (!form.studentId) {
    ElMessage.warning('请选择申请人')
    return
  }
  visible.value = false
  ElMessage.success('申请已提交，请等待辅导员及宿管审批')
  emit('success')
}

defineExpose({
  open
})
</script>

<style lang="scss" scoped>
.text-gray { color: #909399; }
.text-xs { font-size: 12px; }
.mt-5 { margin-top: 5px; }
</style>
