<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <!-- 搜索表单 -->
      <el-row v-show="showSearch">
        <el-form :model="searchForm" ref="searchFormRef" :inline="true" @keyup.enter="handleSearch">
          <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-row>

      <!-- 操作按钮 -->
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button 
            icon="FolderAdd" 
            type="primary" 
            class="ml10" 
            @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 mr20"
            style="float: right;"
            @queryTable="getDataList">
          </right-toolbar>
        </div>
      </el-row>

      <!-- 表格 -->
      <el-table 
        :data="state.dataList" 
        v-loading="state.loading" 
        border 
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
        @sort-change="sortChangeHandle">
        <el-table-column type="index" label="序号" width="60" align="center">
          <template #header>
            <el-icon><List /></el-icon>
          </template>
        </el-table-column>
        <el-table-column prop="deptName" label="学院" show-overflow-tooltip align="center">
          <template #header>
            <el-icon><OfficeBuilding /></el-icon>
            <span style="margin-left: 4px">学院</span>
          </template>
        </el-table-column>
        <el-table-column prop="roomNo" label="房间号" show-overflow-tooltip align="center">
          <template #header>
            <el-icon><House /></el-icon>
            <span style="margin-left: 4px">房间号</span>
          </template>
        </el-table-column>
        <el-table-column prop="classNo" label="班号" show-overflow-tooltip align="center">
          <template #header>
            <el-icon><Grid /></el-icon>
            <span style="margin-left: 4px">班号</span>
          </template>
        </el-table-column>
        <el-table-column prop="teacherRealName" label="班主任" show-overflow-tooltip align="center">
          <template #header>
            <el-icon><UserFilled /></el-icon>
            <span style="margin-left: 4px">班主任</span>
          </template>
        </el-table-column>
        <el-table-column prop="teacherPhone" label="班主任电话" show-overflow-tooltip align="center">
          <template #header>
            <el-icon><Phone /></el-icon>
            <span style="margin-left: 4px">班主任电话</span>
          </template>
        </el-table-column>
        <el-table-column prop="stuNo" label="学号" show-overflow-tooltip align="center">
          <template #header>
            <el-icon><CreditCard /></el-icon>
            <span style="margin-left: 4px">学号</span>
          </template>
        </el-table-column>
        <el-table-column prop="realName" label="姓名" show-overflow-tooltip align="center">
          <template #header>
            <el-icon><Avatar /></el-icon>
            <span style="margin-left: 4px">姓名</span>
          </template>
        </el-table-column>
        <el-table-column prop="bedNo" label="床位号" show-overflow-tooltip align="center">
          <template #header>
            <el-icon><House /></el-icon>
            <span style="margin-left: 4px">床位号</span>
          </template>
          <template #default="scope">
            <el-tag v-if="scope.row.bedNo" size="small" type="info" effect="plain">
              {{ scope.row.bedNo }}
            </el-tag>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="isLeader" label="是否舍长" show-overflow-tooltip align="center">
          <template #header>
            <el-icon><User /></el-icon>
            <span style="margin-left: 4px">是否舍长</span>
          </template>
          <template #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>
        <el-table-column prop="phone" label="学生电话" show-overflow-tooltip align="center">
          <template #header>
            <el-icon><Phone /></el-icon>
            <span style="margin-left: 4px">学生电话</span>
          </template>
        </el-table-column>
        <el-table-column prop="tel" label="家长电话" show-overflow-tooltip align="center">
          <template #header>
            <el-icon><Phone /></el-icon>
            <span style="margin-left: 4px">家长电话</span>
          </template>
        </el-table-column>
        <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="Switch" 
              text 
              type="primary" 
              @click="handleTransfer(scope.row)">
              转宿
            </el-button>
            <el-button 
              icon="Delete" 
              text 
              type="danger" 
              @click="handleCheckout(scope.row)">
              退宿
            </el-button>
          </template>
        </el-table-column>
      </el-table>

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

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

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

<script setup lang="ts" name="DormRoomStudent">
import { reactive, ref, onMounted } from 'vue'
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList, delObjs } 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 FormDialog from './form.vue';
import TransferDialog from './transfer.vue';
import TreeSelect from '/@/components/TreeSelect/index.vue';
import { List, OfficeBuilding, House, Grid, UserFilled, Phone, CreditCard, Avatar, User, Setting } from '@element-plus/icons-vue'
import { defineAsyncComponent } from 'vue'
const StatusTag = defineAsyncComponent(() => import('/@/components/StatusTag/index.vue'))

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

// 树形选择器配置
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 = () => {
  useMessage().warning('功能开发中')
}

// 宿舍互换
const handleRoomSwap = () => {
  useMessage().warning('功能开发中')
}

// 导出
const handleExport = () => {
  useMessage().warning('功能开发中')
}

// 名单导出
const handleExportList = () => {
  useMessage().warning('功能开发中')
}

// 转宿
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) {
    console.error('获取学院列表失败', err)
    deptList.value = []
  }
}

// 获取楼号列表
const getBuildingListData = async () => {
  try {
    const res = await getBuildingList()
    if (res.data) {
      buildingList.value = Array.isArray(res.data) ? res.data : []
    }
  } catch (err) {
    console.error('获取楼号列表失败', 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) {
    console.error('获取宿舍树状列表失败', err)
    dormRoomTreeList.value = []
  }
}

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

