<template>
  <div class="modern-page-container">
    <div class="page-wrapper">
      <!-- 搜索表单卡片 -->
      <el-card v-show="showSearch" class="search-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span class="card-title">
              <el-icon class="title-icon"><Search /></el-icon>
              筛选条件
            </span>
          </div>
        </template>
        <el-form :model="searchForm" ref="searchFormRef" :inline="true" @keyup.enter="handleSearch" class="search-form">
          <el-form-item label="学院" prop="deptCode">
            <el-select 
              v-model="searchForm.deptCode" 
              placeholder="请选择学院" 
              clearable
              filterable 
              style="width: 200px">
              <el-option
                v-for="item in deptList"
                :key="item.deptCode"
                :label="item.deptName"
                :value="item.deptCode">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="楼号" prop="buildingNo">
            <el-select 
              v-model="searchForm.buildingNo" 
              placeholder="请选择楼号" 
              clearable
              filterable 
              style="width: 200px">
              <el-option
                v-for="item in buildingList"
                :key="item.buildingNo"
                :label="item.buildingNo"
                :value="item.buildingNo">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-select 
              v-model="searchForm.gender" 
              placeholder="请选择性别" 
              clearable
              style="width: 200px">
              <el-option label="男" value="1" />
              <el-option label="女" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="筛选类型" prop="dormdataType">
            <el-select 
              v-model="searchForm.dormdataType" 
              placeholder="请选择筛选类型" 
              clearable
              style="width: 200px"
              @change="handleDormDataTypeChange">
              <el-option label="所有" value="" />
              <el-option label="空宿舍" value="0" />
              <el-option label="1人宿舍" value="1" />
              <el-option label="2人宿舍" value="2" /> 
              <el-option label="3人宿舍" value="3" />
              <el-option label="4人宿舍" value="4" />
              <el-option label="5人宿舍" value="5" /> 
            </el-select>
          </el-form-item>
          <el-form-item label="宿舍号" prop="roomNo">
            <TreeSelect
              v-model="searchForm.roomNo"
              :options="dormRoomTreeList"
              :objMap="treeProps"
              placeholder="请选择宿舍号"
              style="width: 200px" />
          </el-form-item>
          <el-form-item label="房间号" prop="roomNoInput">
            <el-input 
              v-model="searchForm.roomNoInput" 
              placeholder="请输入房间号" 
              clearable
              style="width: 200px" />
          </el-form-item>
          <el-form-item label="班号" prop="classNo">
            <el-input 
              v-model="searchForm.classNo" 
              placeholder="请输入班号" 
              clearable
              style="width: 200px" />
          </el-form-item>
          <el-form-item label="学号" prop="stuNo">
            <el-input 
              v-model="searchForm.stuNo" 
              placeholder="请输入学号" 
              clearable
              style="width: 200px" />
          </el-form-item>
          <el-form-item label="姓名" prop="realName">
            <el-input 
              v-model="searchForm.realName" 
              placeholder="请输入姓名" 
              clearable
              style="width: 200px" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plain icon="Search" @click="handleSearch">查询</el-button>
            <el-button icon="Refresh" @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- 内容卡片 -->
      <el-card class="content-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span class="card-title">
              <el-icon class="title-icon"><Document /></el-icon>
              宿舍学生列表
            </span>
            <div class="header-actions">
              <el-button 
                icon="FolderAdd" 
                type="primary" 
                @click="formDialogRef.openDialog()">
                新增住宿生
              </el-button>
              <el-button 
                icon="Printer" 
                type="success" 
                class="ml10" 
                @click="handlePrintCard">
                打印宿舍卡
              </el-button>
              <el-button 
                icon="Switch" 
                type="warning" 
                class="ml10" 
                @click="handleRoomSwap">
                宿舍互换
              </el-button>
              <el-button 
                icon="Download" 
                type="info" 
                class="ml10" 
                @click="handleExport">
                导出
              </el-button>
              <el-button 
                icon="Document" 
                type="primary" 
                plain
                class="ml10" 
                @click="handleExportList">
                名单导出
              </el-button>
              <right-toolbar  
                v-model:showSearch="showSearch" 
                class="ml10"
                @queryTable="getDataList">
                <TableColumnControl
                  ref="columnControlRef"
                  :columns="tableColumns"
                  v-model="visibleColumns"
                  trigger-type="default"
                  trigger-circle
                  @change="handleColumnChange"
                  @order-change="handleColumnOrderChange"
                >
                  <template #trigger>
                    <el-tooltip class="item" effect="dark" content="列设置" placement="top">
                      <el-button circle style="margin-left: 0;">
                        <el-icon><Menu /></el-icon>
                      </el-button>
                    </el-tooltip>
                  </template>
                </TableColumnControl>
              </right-toolbar>
            </div>
          </div>
        </template>

        <!-- 表格 -->
        <el-table 
          :data="state.dataList" 
          v-loading="state.loading" 
          stripe
          :cell-style="tableStyle.cellStyle"
          :header-cell-style="tableStyle.headerCellStyle"
          @sort-change="sortChangeHandle"
          class="modern-table">
          <el-table-column type="index" label="序号" width="70" align="center">
            <template #header>
              <el-icon><List /></el-icon>
            </template>
            <template #default="{ $index }">
              {{ $index + 1 + ((state.pagination?.current || 1) - 1) * (state.pagination?.size || 10) }}
            </template>
          </el-table-column>
          <template v-for="col in visibleColumnsSorted" :key="col.prop || col.label">
            <el-table-column 
              v-if="checkColumnVisible(col.prop || '') && col.prop !== '操作'"
              :prop="col.prop" 
              :label="col.label" 
              show-overflow-tooltip
              align="center"
              :min-width="col.minWidth"
              :width="col.width">
              <template #header>
                <el-icon><component :is="columnConfigMap[col.prop || '']?.icon || OfficeBuilding" /></el-icon>
                <span style="margin-left: 4px">{{ col.label }}</span>
              </template>
            <!-- 床位号列：haveStudent 为 true 时变色标记有人 -->
            <template v-if="col.prop === 'bedNo'" #default="scope">
              <el-tag
                v-if="scope.row.bedNo"
                size="small"
                :type="scope.row.haveStudent ? 'warning' : 'info'"
                effect="plain">
                {{ scope.row.bedNo }}
              </el-tag>
              <span v-else>-</span>
            </template>
            <!-- 是否舍长列特殊模板-->
            <template v-else-if="col.prop === 'isLeader'" #default="scope">
              <StatusTag 
                :value="scope.row.isLeader"
                :options="[{ label: '是', value: '1' }, { label: '否', value: '0' }]"
                :type-map="{ '1': { type: 'success', effect: 'light' }, '0': { type: 'info', effect: 'light' } }"
              />
            </template>
            </el-table-column>
          </template>
          <el-table-column label="操作" width="150" align="center" fixed="right">
            <template #header>
              <el-icon><Setting /></el-icon>
              <span style="margin-left: 4px">操作</span>
            </template>
            <template #default="scope">
              <el-button 
                icon="Edit" 
                link 
                type="primary" 
                @click="handleEdit(scope.row)">
                编辑
              </el-button>
              <el-button 
                icon="Switch" 
                link 
                type="primary" 
                @click="handleTransfer(scope.row)">
                转宿
              </el-button>
              <el-button 
                icon="Delete" 
                link 
                type="danger" 
                @click="handleCheckout(scope.row)">
                退宿
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-wrapper">
          <pagination 
            @size-change="sizeChangeHandle" 
            @current-change="currentChangeHandle" 
            v-bind="state.pagination" />
        </div>
      </el-card>
    </div>

    <!-- 新增/转宿表单弹窗 -->
    <FormDialog ref="formDialogRef" @refresh="getDataList" />

    <!-- 转宿弹窗 -->
    <TransferDialog ref="transferDialogRef" @refresh="getDataList" />

    <!-- 宿舍互换弹窗 -->
    <SwapDialog ref="swapDialogRef" @refresh="getDataList" />

    <!-- 打印宿舍卡弹窗 -->
    <PrintCardDialog ref="printCardDialogRef" />
  </div>
</template>

<script setup lang="ts" name="DormRoomStudent">
import { reactive, ref, onMounted, computed, nextTick } from 'vue'
import { useRoute } from 'vue-router'
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList, delObjs, exportEmptyPeopleRoomExcel } from "/@/api/stuwork/dormroomstudent";
import { getDeptList } from "/@/api/basic/basicclass";
import { getBuildingList } from "/@/api/stuwork/dormbuilding";
import { fetchDormRoomTreeList } from "/@/api/stuwork/dormroom";
import { useMessage, useMessageBox } from "/@/hooks/message";
import TableColumnControl from '/@/components/TableColumnControl/index.vue'
import FormDialog from './form.vue';
import TransferDialog from './transfer.vue';
import SwapDialog from './swap.vue';
import PrintCardDialog from './printCard.vue';
import TreeSelect from '/@/components/TreeSelect/index.vue';
import { List, OfficeBuilding, House, Grid, UserFilled, Phone, CreditCard, Avatar, User, Setting, Menu, Search, Document } from '@element-plus/icons-vue'
import { useTableColumnControl } from '/@/hooks/tableColumn'

import { defineAsyncComponent } from 'vue'
const StatusTag = defineAsyncComponent(() => import('/@/components/StatusTag/index.vue'))

// 定义变量内容
const route = useRoute()
const formDialogRef = ref()
const columnControlRef = ref<any>()
const transferDialogRef = ref()
const swapDialogRef = ref()
const printCardDialogRef = ref()
const searchFormRef = ref()
const showSearch = ref(true)
const deptList = ref<any[]>([])
const buildingList = ref<any[]>([])
const dormRoomTreeList = ref<any[]>([])

// 表格列配置
const tableColumns = [
  { prop: 'deptName', label: '学院' },
  { prop: 'roomNo', label: '房间号' },
  { prop: 'classNo', label: '班号' },
  { prop: 'teacherRealName', label: '班主任' },
  { prop: 'teacherPhone', label: '班主任电话' },
  { prop: 'stuNo', label: '学号' },
  { prop: 'realName', label: '姓名' },
  { prop: 'bedNo', label: '床位号' },
  { prop: 'isLeader', label: '是否舍长' },
  { prop: 'phone', label: '学生电话' },
  { prop: 'tel', label: '家长电话' }
]

// 列配置映射（用于图标）
const columnConfigMap: Record<string, { icon: any }> = {
  deptName: { icon: OfficeBuilding },
  roomNo: { icon: House },
  classNo: { icon: Grid },
  teacherRealName: { icon: UserFilled },
  teacherPhone: { icon: Phone },
  stuNo: { icon: CreditCard },
  realName: { icon: Avatar },
  bedNo: { icon: House },
  isLeader: { icon: User },
  phone: { icon: Phone },
  tel: { icon: Phone }
}

// 使用表格列控制hook
const {
  visibleColumns,
  visibleColumnsSorted,
  checkColumnVisible,
  handleColumnChange,
  handleColumnOrderChange
} = useTableColumnControl(tableColumns)

// 树形选择器配置
const treeProps = {
  value: 'id', // 值字段
  label: 'id', // 显示字段（id 和 name 一样，所以直接用 id）
  children: 'children' // 子节点字段
}

// 搜索表单
const searchForm = reactive({
  deptCode: '',
  buildingNo: '',
  gender: '',
  dormdataType: '', // 筛选类型：空宿舍、空几人宿舍等
  roomNo: '', // 树形选择的宿舍号
  roomNoInput: '', // 手动输入的房间号
  classNo: '',
  stuNo: '',
  realName: ''
})

// 配置 useTable
const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: searchForm,
  pageList: async (queryParams: any) => {
    // 处理查询参数：如果树形选择有值，优先使用树形选择的值；否则使用手动输入的值
    const params = {
      ...queryParams,
      roomNo: queryParams.roomNo || queryParams.roomNoInput || ''
    }
    // 移除 roomNoInput，只保留 roomNo
    delete params.roomNoInput
    return await fetchList(params)
  },
  props: {
    item: 'records',
    totalCount: 'total'
  },
  createdIsNeed: true
})

// table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  tableStyle
} = useTable(state)

// 查询
const handleSearch = () => {
  getDataList()
}

// 重置
const handleReset = () => {
  searchFormRef.value?.resetFields()
  searchForm.deptCode = ''
  searchForm.buildingNo = ''
  searchForm.gender = ''
  searchForm.dormdataType = ''
  searchForm.roomNo = ''
  searchForm.roomNoInput = ''
  searchForm.classNo = ''
  searchForm.stuNo = ''
  searchForm.realName = ''
  // 重置时重新获取所有宿舍树形列表
  getDormRoomTreeListData()
  getDataList()
}

// 筛选类型变化时重新获取树形列表
const handleDormDataTypeChange = (dormdataType: string) => {
  // 清空已选择的宿舍号
  searchForm.roomNo = ''
  // 重新获取树形列表
  getDormRoomTreeListData(dormdataType)
}

// 打印宿舍卡（按房间号查询后打印）
const handlePrintCard = () => {
  printCardDialogRef.value?.openDialog()
}

// 宿舍互换（两名学生互换宿舍）
const handleRoomSwap = () => {
  const query = {
    deptCode: searchForm.deptCode,
    buildingNo: searchForm.buildingNo,
    gender: searchForm.gender,
    roomNo: searchForm.roomNo || searchForm.roomNoInput,
    classNo: searchForm.classNo,
    stuNo: searchForm.stuNo,
    realName: searchForm.realName
  }
  swapDialogRef.value?.openDialog(query)
}

// 导出：空 n 人宿舍导出（按当前筛选条件传参）
const handleExport = async () => {
  try {
    const params = {
      deptCode: searchForm.deptCode,
      buildingNo: searchForm.buildingNo,
      gender: searchForm.gender,
      dormdataType: searchForm.dormdataType,
      roomNo: searchForm.roomNo || searchForm.roomNoInput,
      classNo: searchForm.classNo,
      stuNo: searchForm.stuNo,
      realName: searchForm.realName
    }
    const res = await exportEmptyPeopleRoomExcel(params)
    const blob = res instanceof Blob ? res : new Blob([res as BlobPart], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
    const url = window.URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = url
    link.download = `空宿舍导出_${Date.now()}.xlsx`
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    window.URL.revokeObjectURL(url)
    useMessage().success('导出成功')
  } catch (err: any) {
    useMessage().error(err?.msg || '导出失败')
  }
}

// 名单导出：与导出共用空 n 人宿舍导出接口，文件名区分
const handleExportList = async () => {
  try {
    const params = {
      deptCode: searchForm.deptCode,
      buildingNo: searchForm.buildingNo,
      gender: searchForm.gender,
      dormdataType: searchForm.dormdataType,
      roomNo: searchForm.roomNo || searchForm.roomNoInput,
      classNo: searchForm.classNo,
      stuNo: searchForm.stuNo,
      realName: searchForm.realName
    }
    const res = await exportEmptyPeopleRoomExcel(params)
    const blob = res instanceof Blob ? res : new Blob([res as BlobPart], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
    const url = window.URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = url
    link.download = `住宿学生名单_${Date.now()}.xlsx`
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    window.URL.revokeObjectURL(url)
    useMessage().success('导出成功')
  } catch (err: any) {
    useMessage().error(err?.msg || '导出失败')
  }
}

// 编辑（与转宿共用接口 edit，修改房间/床位/是否舍长）
const handleEdit = (row: any) => {
  transferDialogRef.value?.openDialog(row)
}

// 转宿
const handleTransfer = (row: any) => {
  transferDialogRef.value?.openDialog(row)
}

// 退宿
const handleCheckout = async (row: any) => {
  try {
    await useMessageBox().confirm('确定要退宿该学生吗？')
    await delObjs([row.id])
    useMessage().success('退宿成功')
    getDataList()
  } catch (err: any) {
    if (err !== 'cancel') {
      useMessage().error(err.msg || '退宿失败')
    }
  }
}

// 获取学院列表
const getDeptListData = async () => {
  try {
    const res = await getDeptList()
    if (res.data) {
      deptList.value = Array.isArray(res.data) ? res.data : []
    }
  } catch (err) {
    deptList.value = []
  }
}

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

// 获取宿舍树状列表
const getDormRoomTreeListData = async (dormdataType?: string) => {
  try {
    const res = await fetchDormRoomTreeList(dormdataType || searchForm.dormdataType)
    if (res.data) {
      // 处理树状数据，确保数据结构符合 TreeSelect 组件要求
      dormRoomTreeList.value = Array.isArray(res.data) ? res.data : []
      
      // 如果返回的数据结构不同，可能需要转换
      // 例如：如果返回的是 { buildingNo: '1', rooms: [...] } 的结构
      // 需要转换为树形结构
      if (dormRoomTreeList.value.length === 0 && res.data) {
        // 尝试处理不同的数据结构
        if (typeof res.data === 'object' && !Array.isArray(res.data)) {
          // 如果是对象，可能需要转换为数组
          dormRoomTreeList.value = [res.data]
        }
      }
    }
  } catch (err) {
    dormRoomTreeList.value = []
  }
}

// 初始化
onMounted(() => {
  getDeptListData()
  getBuildingListData()
  getDormRoomTreeListData()
})
</script>

<style scoped lang="scss">
@import '/@/assets/styles/modern-page.scss';
</style>
