<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="state.queryForm" ref="searchFormRef" :inline="true" @keyup.enter="getDataList" class="search-form">
          <el-form-item label="姓名" prop="name">
            <el-input 
              v-model="state.queryForm.name" 
              placeholder="请输入姓名" 
              clearable
              style="width: 200px" /> 
          </el-form-item>
          <el-form-item label="用户工号" prop="username">
            <el-input 
              v-model="state.queryForm.username" 
              placeholder="请输入用户工号" 
              clearable
              style="width: 200px" /> 
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="getDataList">查询</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"><User /></el-icon>
              校领导（党委）管理
            </span>
            <div class="header-actions">
              <el-button 
                icon="FolderAdd" 
                type="primary" 
                @click="formDialogRef.openDialog('add')">
                新增
              </el-button>
              <right-toolbar v-model:showSearch="showSearch" class="ml10" @queryTable="getDataList" />
            </div>
          </div>
        </template>

        <!-- 表格 -->
        <el-table 
          ref="tableRef"
          :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>
          </el-table-column>
          <el-table-column prop="name" label="姓名" min-width="120" show-overflow-tooltip>
            <template #header>
              <el-icon><User /></el-icon>
              <span style="margin-left: 4px">姓名</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="用户工号" min-width="150" show-overflow-tooltip>
            <template #header>
              <el-icon><UserFilled /></el-icon>
              <span style="margin-left: 4px">用户工号</span>
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注" min-width="300" show-overflow-tooltip>
            <template #header>
              <el-icon><EditPen /></el-icon>
              <span style="margin-left: 4px">备注</span>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="180" show-overflow-tooltip>
            <template #header>
              <el-icon><Clock /></el-icon>
              <span style="margin-left: 4px">创建时间</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="150">
            <template #default="scope">
              <el-button 
                icon="Edit" 
                link 
                type="primary" 
                @click="formDialogRef.openDialog('edit', scope.row)">
                编辑
              </el-button>
              <el-button 
                icon="Delete" 
                link 
                type="danger" 
                @click="handleDelete(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <pagination
          v-if="state.pagination"
          v-show="state.pagination.total && state.pagination.total > 0"
          :total="state.pagination.total"
          :current="state.pagination.current"
          :size="state.pagination.size"
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
        />
      </el-card>
    </div>

    <!-- 编辑、新增表单对话框 -->
    <FormDialog ref="formDialogRef" @refresh="getDataList" />
  </div>
</template>

<script setup lang="ts" name="PurchasingSchoolLeader">
import { ref, reactive, defineAsyncComponent } from 'vue'
import { BasicTableProps, useTable } from "/@/hooks/table";
import { getPage, delObj } from "/@/api/finance/purchasingschoolleader";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { List, User, UserFilled, EditPen, Clock, Search } from '@element-plus/icons-vue'

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

// 定义变量内容
const tableRef = ref()
const formDialogRef = ref()
const searchFormRef = ref()
const showSearch = ref(true)

/**
 * 定义响应式表格数据
 */
const state: BasicTableProps = reactive<BasicTableProps>({
  pageList: getPage,
  queryForm: {
    name: '',
    username: '',
  },
  createdIsNeed: true
});

/**
 * 使用 useTable 定义表格相关操作
 */
const { getDataList, tableStyle, sizeChangeHandle, currentChangeHandle } = useTable(state);

/**
 * 重置搜索表单
 */
const handleReset = () => {
  searchFormRef.value?.resetFields();
  getDataList();
};

/**
 * 删除当前行
 * @param row - 当前行数据
 */
const handleDelete = async (row: any) => {
  try {
    await useMessageBox().confirm('确定要删除该记录吗？');
  } catch {
    return;
  }

  try {
    await delObj([row.id]);
    useMessage().success('删除成功');
    getDataList();
  } catch (err: any) {
    useMessage().error(err.msg || '删除失败');
  }
};
</script>

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

