<template>
  <div class="vacation-stay-container">
    <vab-card>
      <vab-query-form>
        <vab-query-form-left-panel>
          <el-form :inline="true" :model="queryForm" @submit.prevent>
            <el-form-item label="假期类型">
              <el-select v-model="queryForm.type" placeholder="全部" clearable style="width: 120px">
                <el-option label="寒假" value="winter" />
                <el-option label="暑假" value="summer" />
              </el-select>
            </el-form-item>
            <el-form-item label="年份">
              <el-date-picker v-model="queryForm.year" type="year" placeholder="选择年份" style="width: 120px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">查询</el-button>
            </el-form-item>
          </el-form>
        </vab-query-form-left-panel>
        <vab-query-form-right-panel>
          <el-button type="warning" @click="handleConcentrate"> <vab-icon icon="layout-grid-fill" /> 集中住宿配置</el-button>
          <el-button type="success" @click="handleRestore"> <vab-icon icon="history-line" /> 一键还原房源</el-button>
        </vab-query-form-right-panel>
      </vab-query-form>

      <el-table v-loading="loading" :data="tableData" border stripe>
        <el-table-column prop="batchName" label="批次名称" min-width="150" />
        <el-table-column prop="timeRange" label="集中住宿时间" width="220" />
        <el-table-column prop="studentCount" label="涉及人数" width="100" />
        <el-table-column prop="area" label="集中区域" min-width="150" />
        <el-table-column prop="status" label="当前状态" width="120">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">{{ getStatusLabel(row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button type="text" size="small" @click="handleDetail(row)">明细</el-button>
            <el-button 
              v-if="row.status === 'active'" 
              type="primary" 
              size="small" 
              link 
              @click="handleRestoreBatch(row)"
            >
              结束还原
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </vab-card>
    
    <!-- 集中住宿配置弹窗 -->
    <concentrate-dialog ref="concentrateDialogRef" @success="handleSuccess" />

    <!-- 批次明细抽屉 -->
    <batch-detail-drawer ref="detailDrawerRef" />

    <!-- 还原确认弹窗 -->
    <el-dialog v-model="restoreVisible" title="房源还原" width="400px">
      <div class="p-20 text-center">
        <vab-icon icon="alert-line" class="text-warning text-xl mb-10" />
        <p>确定要结束本次集中住宿并将所有学生还原至原宿舍吗？</p>
        <p class="text-xs text-gray mt-5">系统将自动恢复原门禁权限并生成换房记录</p>
      </div>
      <template #footer>
        <el-button @click="restoreVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmRestore">确认还原</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import ConcentrateDialog from './components/ConcentrateDialog.vue'
import BatchDetailDrawer from './components/BatchDetailDrawer.vue'

defineOptions({
  name: 'VacationStay',
})

const queryForm = reactive({
  type: '',
  year: ''
})

const loading = ref(false)
const tableData = ref([])
const concentrateDialogRef = ref()
const detailDrawerRef = ref()
const restoreVisible = ref(false)

const getStatusLabel = (status) => {
  const map = { active: '集中住宿中', restored: '已还原', processing: '处理中' }
  return map[status]
}
const getStatusType = (status) => {
  const map = { active: 'success', restored: 'info', processing: 'warning' }
  return map[status]
}

const handleQuery = () => {
  loading.value = true
  setTimeout(() => {
    tableData.value = [
      {
        id: 1,
        batchName: '2026寒假留校集中住宿',
        timeRange: '2026-01-20 ~ 2026-02-25',
        studentCount: 156,
        area: '1号楼(男), 3号楼(女)',
        status: 'active'
      },
      {
        id: 2,
        batchName: '2025暑假留校集中住宿',
        timeRange: '2025-07-05 ~ 2025-08-30',
        studentCount: 320,
        area: '2号楼(男), 4号楼(女)',
        status: 'restored'
      }
    ]
    loading.value = false
  }, 500)
}

const handleConcentrate = () => {
  concentrateDialogRef.value.open()
}

const handleRestore = () => {
  restoreVisible.value = true
}

const handleRestoreBatch = (row) => {
  restoreVisible.value = true
}

const confirmRestore = () => {
  ElMessage.success('还原任务已提交，正在恢复原宿舍分配...')
  restoreVisible.value = false
  handleQuery()
}

const handleDetail = (row) => {
  detailDrawerRef.value.open(row)
}

const handleSuccess = () => {
  handleQuery()
}

onMounted(() => {
  handleQuery()
})
</script>

<style lang="scss" scoped>
.text-center { text-align: center; }
.text-warning { color: var(--el-color-warning); }
.text-gray { color: #909399; }
.text-xl { font-size: 24px; }
.text-xs { font-size: 12px; }
.mb-10 { margin-bottom: 10px; }
.mt-5 { margin-top: 5px; }
.p-20 { padding: 20px; }
</style>
