<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="stuNo">
            <el-input
              v-model="searchForm.stuNo"
              placeholder="请输入学号"
              clearable
              style="width: 160px" />
          </el-form-item>
          <el-form-item label="班号" prop="classNo">
            <el-input
              v-model="searchForm.classNo"
              placeholder="请输入班号"
              clearable
              style="width: 160px" />
          </el-form-item>
          <el-form-item label="预约时间" prop="reservationTime">
            <el-date-picker
              v-model="searchForm.reservationTime"
              type="date"
              placeholder="选择预约时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              clearable
              style="width: 180px" />
          </el-form-item>
          <el-form-item label="是否处理" prop="isHandle">
            <el-select
              v-model="searchForm.isHandle"
              placeholder="请选择"
              clearable
              style="width: 120px">
              <el-option label="是" value="1" />
              <el-option label="否" value="0" />
            </el-select>
          </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>
              <template v-if="col.prop === 'isHandle'" #default="scope">
                <el-tag :type="scope.row.isHandle === '1' ? 'success' : 'info'" size="small">
                  {{ scope.row.isHandle === '1' ? '是' : '否' }}
                </el-tag>
              </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="PsychologicalCounselingReservation">
import { reactive, ref, onMounted, defineAsyncComponent } from 'vue'
import { useRoute } from 'vue-router'
import { BasicTableProps, useTable } from '/@/hooks/table'
import { fetchList, delObj } from '/@/api/stuwork/psychologicalcounselingreservation'
import { useMessage, useMessageBox } from '/@/hooks/message'
import TableColumnControl from '/@/components/TableColumnControl/index.vue'
import {
  List,
  Calendar,
  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: 'reservationTime', label: '预约时间', icon: Calendar, minWidth: 160 },
  { prop: 'realName', label: '值班老师', icon: UserFilled },
  { prop: 'classNo', label: '班号', icon: Document },
  { prop: 'stuNo', label: '学号', icon: UserFilled },
  { prop: 'stuName', label: '学生姓名', icon: UserFilled },
  { prop: 'phone', label: '联系方式', icon: Phone, minWidth: 120 },
  { prop: 'isHandle', label: '是否处理', icon: Document },
  { prop: 'teaRemark', label: '老师备注', icon: EditPen, minWidth: 120 },
  { prop: 'remarks', label: '学生留言', icon: EditPen, minWidth: 120 }
]

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

// 搜索表单
const searchForm = reactive({
  stuNo: '',
  classNo: '',
  reservationTime: '',
  isHandle: ''
})

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.stuNo = ''
  searchForm.classNo = ''
  searchForm.reservationTime = ''
  searchForm.isHandle = ''
  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>
