<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="teacherNo">
            <el-select 
              v-model="searchForm.teacherNo" 
              placeholder="请选择教师" 
              clearable
              filterable 
              style="width: 200px">
              <el-option
                v-for="item in teacherList"
                :key="item.teacherNo"
                :label="item.realName"
                :value="item.teacherNo">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="联系方式" prop="telPhone">
            <el-input 
              v-model="searchForm.telPhone" 
              placeholder="请输入联系方式" 
              clearable
              style="width: 200px" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" 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()"
                v-auth="'stuwork_classmasterresume_add'">
                新增
              </el-button>
              <right-toolbar  
                v-model:showSearch="showSearch" 
                :export="'stuwork_classmasterresume_export'"
                @exportExcel="exportExcel" 
                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"
          @sort-change="sortChangeHandle">
          <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" 
              :width="col.width"
              :min-width="col.minWidth"
              :show-overflow-tooltip="col.showOverflowTooltip !== false"
              :align="col.align || 'center'">
              <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>
            </el-table-column>
          </template>
          <el-table-column label="操作" width="180" align="center" fixed="right">
            <template #header>
              <el-icon><Setting /></el-icon>
              <span style="margin-left: 4px">操作</span>
            </template>
            <template #default="scope">
              <el-button 
                icon="View" 
                link
                type="primary" 
                @click="handleViewDetail(scope.row)">
                查看履历详情
              </el-button>
            </template>
          </el-table-column>
          <template #empty>
            <el-empty description="暂无数据" :image-size="120">
              <el-button type="primary" icon="FolderAdd" @click="formDialogRef.openDialog()">新增履历</el-button>
            </el-empty>
          </template>
        </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" />
  </div>
</template>

<script setup lang="ts" name="ClassMasterResume">
import { ref, reactive, defineAsyncComponent, onMounted, nextTick } from 'vue'
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList } from "/@/api/stuwork/classmasterresume";
import request from "/@/utils/request";
import TableColumnControl from '/@/components/TableColumnControl/index.vue'
import { List, User, CreditCard, Phone, Setting, Menu, Search, Document } from '@element-plus/icons-vue'
import { useTableColumnControl } from '/@/hooks/tableColumn'

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

// 定义变量内容
const formDialogRef = ref()
const searchFormRef = ref() 
const detailDialogRef = ref()
const columnControlRef = ref()
// 搜索变量 
const showSearch = ref(true) 
// 教师列表
const teacherList = ref<any[]>([])

// 表格列配置
const tableColumns = [
  { prop: 'teacherNoVal', label: '教师', icon: User },
  { prop: 'teacherNo', label: '工号', icon: CreditCard },
  { prop: 'telPhone', label: '联系方式', icon: Phone }
]

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

// 搜索表单
const searchForm = reactive({
  teacherNo: '',
  telPhone: ''
})

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

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

// 获取教师列表
const getTeacherList = async () => {
  try {
    const res = await request({
      url: '/professional/teacherbase/TeacherBaseList',
      method: 'get'
    })
    teacherList.value = res.data || []
  } catch (err) {
  }
}

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

// 重置
const handleReset = () => {
  searchFormRef.value?.resetFields()
  searchForm.teacherNo = ''
  searchForm.telPhone = ''
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile(
    '/stuwork/classmasterresume/export', 
    searchForm, 
    '班主任履历.xlsx'
  )
}

// 查看履历详情
const handleViewDetail = async (row: any) => {
  if (!row.teacherNo) {
    return
  }
  
  // 确保组件已挂载
  await nextTick()
  
  if (detailDialogRef.value && typeof detailDialogRef.value.openDialog === 'function') {
    detailDialogRef.value.openDialog(row.teacherNo)
  }
}

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

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