<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="realName">
            <el-input
              v-model="searchForm.realName"
              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()">
                新增
              </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">
          <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"
              :min-width="col.minWidth"
              :width="col.width"
              show-overflow-tooltip
              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="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="Delete"
                link
                type="danger"
                @click="handleDelete(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" />
  </div>
</template>

<script setup lang="ts" name="PsychologicalCounselingTeacher">
import { reactive, ref, onMounted, defineAsyncComponent } from 'vue'
import { useRoute } from 'vue-router'
import { BasicTableProps, useTable } from '/@/hooks/table'
import { fetchList, delObj } from '/@/api/stuwork/psychologicalcounselingteacher'
import { useMessage, useMessageBox } from '/@/hooks/message'
import TableColumnControl from '/@/components/TableColumnControl/index.vue'
import { List, UserFilled, Phone, EditPen, Setting, Menu, Search, Document, FolderAdd } from '@element-plus/icons-vue'
import { useTableColumnControl } from '/@/hooks/tableColumn'

const FormDialog = defineAsyncComponent(() => import('./form.vue'))

const route = useRoute()
const formDialogRef = ref()
const searchFormRef = ref()
const columnControlRef = ref<any>()
const showSearch = ref(true)

// 表格列配置（与其它页面一致，icon 写在列上）
const tableColumns = [
  { prop: 'userName', label: '用户名', icon: UserFilled },
  { prop: 'realName', label: '教师姓名', icon: UserFilled },
  { prop: 'phone', label: '联系方式', icon: Phone, minWidth: 120 },
  { prop: 'remarks', label: '备注', icon: EditPen, minWidth: 150 }
]

const {
  visibleColumns,
  visibleColumnsSorted,
  checkColumnVisible,
  handleColumnChange,
  handleColumnOrderChange
} = useTableColumnControl(tableColumns, route.path)

// 搜索表单
const searchForm = reactive({
  realName: ''
})

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

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

const handleSearch = () => {
  getDataList()
}

const handleReset = () => {
  searchFormRef.value?.resetFields()
  searchForm.realName = ''
  getDataList()
}

const handleEdit = (row: any) => {
  formDialogRef.value?.openDialog('edit', row)
}

const handleDelete = async (row: any) => {
  try {
    await useMessageBox().confirm('确定要删除该预约师吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
  } catch {
    return
  }
  try {
    await delObj([row.id])
    useMessage().success('删除成功')
    getDataList()
  } catch (err: any) {
    useMessage().error(err.msg || '删除失败')
  }
}

onMounted(() => {})
</script>

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