<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"
              @change="handleDeptChange"> 
              <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="classNo">
            <el-input 
              v-model="searchForm.classNo" 
              placeholder="请输入班号" 
              clearable
              style="width: 200px" />
          </el-form-item>
          <el-form-item label="联院" prop="isUnion">
            <el-select 
              v-model="searchForm.isUnion" 
              placeholder="请选择联院" 
              clearable
              style="width: 200px">
              <el-option label="是" value="1" />
              <el-option label="否" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label="流失率" prop="stuLoseRate">
            <el-input 
              v-model="searchForm.stuLoseRate" 
              placeholder="请输入流失率" 
              clearable
              style="width: 200px" />
          </el-form-item>
          <el-form-item label="入学年份" prop="grade">
            <el-input 
              v-model="searchForm.grade" 
              placeholder="请输入入学年份" 
              clearable
              style="width: 200px" />
          </el-form-item>
          <el-form-item label="班主任" prop="teacherRealName">
            <el-input 
              v-model="searchForm.teacherRealName" 
              placeholder="请输入班主任姓名" 
              clearable
              style="width: 200px" />
          </el-form-item>
          <el-form-item label="班级状态" prop="classStatus">
            <el-select 
              v-model="searchForm.classStatus" 
              placeholder="请选择班级状态" 
              clearable
              style="width: 200px">
              <el-option label="正常" value="0" />
              <el-option label="离校" value="1" />
            </el-select>
          </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="Link" 
                type="success" 
                class="ml10" 
                @click="handleLinkRule">
                关联门禁规则
              </el-button>
              <el-button 
                icon="Download" 
                type="warning" 
                class="ml10" 
                @click="handleExport">
                导出
              </el-button>
              <el-button 
                icon="DocumentAdd" 
                type="info" 
                class="ml10" 
                @click="handleGenerateAssessment">
                生成考核班级
              </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"
          class="modern-table"
          row-key="id"
          @selection-change="handleSelectionChange"
          @sort-change="sortChangeHandle">
          <el-table-column type="selection" width="55" align="center" />
          <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>
        <el-table-column 
          v-for="col in visibleColumnsSorted" 
          :key="col.prop"
          :prop="col.prop" 
          :label="col.label" 
          :width="col.width"
          :min-width="col.minWidth"
          :show-overflow-tooltip="col.showOverflowTooltip !== false"
          :align="col.align">
          <template #header>
            <el-icon v-if="col.icon"><component :is="col.icon" /></el-icon>
            <span :style="{ marginLeft: col.icon ? '4px' : '0' }">{{ col.label }}</span>
          </template>
          <template #default="scope" v-if="col.prop === 'stuNum'">
            <el-tag size="small" type="primary" effect="plain">
              {{ scope.row.stuNum || 0 }}/{{ scope.row.preStuNum || 0 }}
            </el-tag>
          </template>
          <template #default="scope" v-else-if="col.prop === 'classStatus'">
            <StatusTag 
              :value="scope.row.classStatus"
              :options="[{ label: '正常', value: '0' }, { label: '离校', value: '1' }]"
              :type-map="{ '0': { type: 'success', effect: 'light' }, '1': { type: 'warning', effect: 'light' } }"
            />
          </template>
          <template #default="scope" v-else-if="col.prop === 'stuLoseRate'">
            <el-tag size="small" type="danger" effect="plain">
              {{ scope.row.stuLoseRate ? `${scope.row.stuLoseRate}%` : '0%' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="260">
          <template #default="scope">
            <el-button 
              icon="View" 
              link 
              type="primary" 
              @click="handleViewDetail(scope.row)">
              班级概况
            </el-button>
            <el-button 
              icon="Edit" 
              link 
              type="primary" 
              @click="formDialogRef.openDialog(scope.row.id)">
              编辑
            </el-button>
            <el-button 
              icon="Delete" 
              link 
              type="danger" 
              @click="handleDelete([scope.row.id])">
              删除
            </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(false)" />
    
    <!-- 班级概况详情 -->
    <DetailDialog ref="detailDialogRef" />
    
    <!-- 关联门禁规则对话框 -->
    <el-dialog 
      title="关联门禁规则" 
      v-model="linkRuleDialogVisible"
      :close-on-click-modal="false" 
      draggable
      width="600px">
      <el-form :model="linkRuleForm" label-width="120px">
        <el-form-item label="选择规则">
          <el-select 
            v-model="linkRuleForm.ruleId" 
            placeholder="请选择门禁规则" 
            clearable
            filterable
            style="width: 100%">
            <el-option
              v-for="item in ruleList"
              :key="item.id"
              :label="item.ruleName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="linkRuleDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmLinkRule" :loading="linkRuleLoading">确 认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="BasicClass">
import { ref, reactive, defineAsyncComponent, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList, delObj, putObjs, classExportData, getDeptList, getClassListByRole } from "/@/api/basic/basicclass";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { fetchList as getRuleList } from "/@/api/stuwork/entrancerule";
import { downBlobFile, adaptationUrl } from "/@/utils/other";
import TableColumnControl from '/@/components/TableColumnControl/index.vue'
import { List, OfficeBuilding, Grid, Document, UserFilled, Phone, User, Lock, CircleCheck, TrendCharts, Setting, Menu, Search } from '@element-plus/icons-vue'
import { useTableColumnControl } from '/@/hooks/tableColumn'
import { defineAsyncComponent as defineStatusTag } from 'vue'
const StatusTag = defineStatusTag(() => import('/@/components/StatusTag/index.vue'))

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const DetailDialog = defineAsyncComponent(() => import('./detail.vue'));

// 定义变量内容
const route = useRoute()
const formDialogRef = ref()
const detailDialogRef = ref()
const searchFormRef = ref()
const columnControlRef = ref()
// 搜索变量
const showSearch = ref(true)
const deptList = ref<any[]>([])
const classList = ref<any[]>([])
const ruleList = ref<any[]>([])
const linkRuleDialogVisible = ref(false)
const linkRuleLoading = ref(false)
const selectedClassCodes = ref<string[]>([])

// 表格列配置
const tableColumns = [
  { prop: 'deptName', label: '学院', icon: OfficeBuilding },
  { prop: 'classNo', label: '班号', icon: Grid },
  { prop: 'classProName', label: '班级规范名称', icon: Document },
  { prop: 'teacherRealName', label: '班主任', icon: UserFilled },
  { prop: 'teacherTel', label: '班主任电话号码', icon: Phone },
  { prop: 'stuNum', label: '班级人数/原始人数', icon: User },
  { prop: 'ruleName', label: '门禁规则', icon: Lock },
  { prop: 'classStatus', label: '班级状态', icon: CircleCheck },
  { prop: 'stuLoseRate', label: '流失率', icon: TrendCharts }
]

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

// 搜索表单
const searchForm = reactive({
  deptCode: '',
  classNo: '',
  isUnion: '',
  stuLoseRate: '',
  grade: '',
  teacherRealName: '',
  classStatus: ''
})

// 关联门禁规则表单
const linkRuleForm = reactive({
  ruleId: ''
})

// 配置 useTable
const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: searchForm,
  pageList: fetchList,
  props: {
    item: 'records',
    totalCount: 'total'
  },
  createdIsNeed: true
})

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

// 表格多选
const handleSelectionChange = (rows: any[]) => {
  selectedClassCodes.value = (rows || [])
    .map((item) => item.classCode)
    .filter((item) => !!item)
}

// 学院选择变化
const handleDeptChange = () => {
  // 可以根据需要清空班号选择
}

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

// 重置
const handleReset = () => {
  searchFormRef.value?.resetFields()
  searchForm.deptCode = ''
  searchForm.classNo = ''
  searchForm.isUnion = ''
  searchForm.stuLoseRate = ''
  searchForm.grade = ''
  searchForm.teacherRealName = ''
  searchForm.classStatus = ''
  getDataList()
}

// 删除
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('确定要删除选中的记录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
  } catch {
    return
  }

  try {
    // 根据API文件，delObj接受单个id，需要循环删除
    for (const id of ids) {
      await delObj(id)
    }
    useMessage().success('删除成功')
    getDataList()
  } catch (err: any) {
    useMessage().error(err.msg || '删除失败')
  }
}

// 查看班级概况 - 直接使用表格行数据
const handleViewDetail = (row: any) => {
  if (detailDialogRef.value) {
    detailDialogRef.value.openDialog(row)
  }
}

// 关联门禁规则
const handleLinkRule = () => {
  if (!selectedClassCodes.value.length) {
    useMessage().warning('请先勾选要关联的班级')
    return
  }
  linkRuleDialogVisible.value = true
  linkRuleForm.ruleId = ''
  // 加载规则列表
  getRuleListData()
}

// 确认关联门禁规则
const confirmLinkRule = async () => {
  if (!selectedClassCodes.value.length) {
    useMessage().warning('请先勾选要关联的班级')
    return
  }
  if (!linkRuleForm.ruleId) {
    useMessage().warning('请选择门禁规则')
    return
  }

  try {
    linkRuleLoading.value = true
    await putObjs({
      ruleId: linkRuleForm.ruleId,
      classCodes: selectedClassCodes.value
    })
    useMessage().success('关联成功')
    linkRuleDialogVisible.value = false
    getDataList()
  } catch (err: any) {
    useMessage().error(err.msg || '关联失败')
  } finally {
    linkRuleLoading.value = false
  }
}

// 导出
const handleExport = async () => {
  try {
    await downBlobFile(adaptationUrl('/basic/basicclass/classExportData'), searchForm, '班级信息.xlsx')
    useMessage().success('导出成功')
  } catch (err: any) {
    useMessage().error(err.msg || '导出失败')
  }
}

// 生成考核班级（占位，待确认接口）
const handleGenerateAssessment = () => {
  useMessage().warning('生成考核班级功能待确认接口后启用')
}

// 获取学院列表
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 getClassListData = async () => {
  try {
    const res = await getClassListByRole()
    if (res.data) {
      classList.value = Array.isArray(res.data) ? res.data : []
    }
  } catch (err) {
    console.error('获取班号列表失败', err)
    classList.value = []
  }
}

// 获取门禁规则列表
const getRuleListData = async () => {
  try {
    const res = await getRuleList({ current: 1, size: 9999 })
    if (res.data && res.data.records) {
      ruleList.value = Array.isArray(res.data.records) ? res.data.records : []
    } else if (Array.isArray(res.data)) {
      ruleList.value = res.data
    } else {
      ruleList.value = []
    }
  } catch (err) {
    console.error('获取门禁规则列表失败', err)
    ruleList.value = []
  }
}

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

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