<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <!-- 搜索表单 -->
      <search-form 
        v-show="showSearch" 
        :model="search" 
        ref="searchFormRef"
        @keyup-enter="handleFilter"
      >
        <template #default="{ visible }">
          <template v-if="visible">
            <el-form-item label="工号" prop="teacherNo">
              <el-input
                v-model="search.teacherNo"
                clearable
                placeholder="请输入工号"
              />
            </el-form-item>

            <el-form-item label="姓名" prop="teacherName">
              <el-input
                v-model="search.teacherName"
                clearable
                placeholder="请输入姓名"
              />
            </el-form-item>
          </template>
        </template>

        <!-- 操作按钮 -->
        <template #actions>
          <el-form-item>
            <el-button type="primary" @click="handleFilter" icon="Search">查询</el-button>
            <el-button @click="resetQuery" icon="Refresh">重置</el-button>
          </el-form-item>
        </template>
      </search-form>

      <!-- 表格 -->
      <el-table
        ref="tableRef"
        :data="state.dataList"
        v-loading="state.loading"
        border
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        
        <el-table-column label="姓名/工号" min-width="150" align="center">
          <template #default="scope">
            <TeacherNameNo :name="scope.row.teacherName" :no="scope.row.teacherNo" />
          </template>
        </el-table-column>
        
        <el-table-column prop="oldDeptName" label="原部门名称" min-width="150" align="center" show-overflow-tooltip />
        
        <el-table-column prop="newDeptName" label="现部门名称" min-width="150" align="center" show-overflow-tooltip />
        
        <el-table-column prop="changeDate" label="调令日期" min-width="120" align="center" />
        
        <el-table-column label="操作" width="150" align="center" fixed="right">
          <template #default="scope">
            <el-button 
              type="primary"
              link
              icon="Printer"
              @click="doPrint(scope.row)">打印部门调令
            </el-button>
          </template>
        </el-table-column>
      </el-table>

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

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { defineAsyncComponent } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { BasicTableProps, useTable } from '/@/hooks/table'
import { fetchList } from '/@/api/professional/professionaluser/professionalteacherstationchange'

const router = useRouter()
const route = useRoute()

const TeacherNameNo = defineAsyncComponent(() => import('/@/components/TeacherNameNo/index.vue'))

// 表格引用
const tableRef = ref()
const searchFormRef = ref()
const showSearch = ref(true)

// 搜索表单数据
const search = reactive({
  teacherNo: '',
  teacherName: ''
})

// 配置 useTable
const state: BasicTableProps = reactive<BasicTableProps>({
  pageList: async (params: any) => {
    const response = await fetchList(params)
    return {
      data: {
        records: response.data.records || [],
        total: response.data.total || 0
      }
    }
  },
  queryForm: search
})

const { getDataList, currentChangeHandle, sizeChangeHandle, tableStyle } = useTable(state)

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

// 重置查询
const resetQuery = () => {
  Object.assign(search, {
    teacherNo: '',
    teacherName: ''
  })
  getDataList()
}

// 打印部门调令
const doPrint = (row: any) => {
  // 直接使用固定路径跳转到 print 页面
  const routeData = router.resolve({
    path: '/professional/professionalteacherstationchange/print',
    query: { id: row.id } // 使用 query 参数传递 id
  })
  window.open(routeData.href, '_blank')
}

// 表格数据由 useTable（createdIsNeed 默认 true）在挂载时自动请求
</script>

<style lang="scss" scoped>
</style>
