<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="serialNumber">
            <el-input v-model="state.queryForm.serialNumber" placeholder="请输入学号" clearable style="width: 150px" />
          </el-form-item>
          <el-form-item label="姓名" prop="realName">
            <el-input v-model="state.queryForm.realName" placeholder="请输入姓名" clearable style="width: 150px" />
          </el-form-item>
          <el-form-item label="年份" prop="year">
            <el-date-picker v-model="state.queryForm.year" type="year" placeholder="选择年份" value-format="YYYY" style="width: 150px" />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="state.queryForm.phone" placeholder="请输入手机号" clearable style="width: 150px" />
          </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')" v-auth="'finance_recruit_stu_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" />
          <el-table-column prop="serialNumber" label="学号" width="120" align="center" />
          <el-table-column prop="realName" label="姓名" width="100" align="center" />
          <el-table-column prop="stuSource" label="学生来源" width="100" align="center">
            <template #default="{ row }">
              <span v-if="row.stuSource === '1'">本地</span>
              <span v-else-if="row.stuSource === '2'">外地</span>
              <span v-else-if="row.stuSource === '3'">其他</span>
              <span v-else>{{ row.stuSource }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="year" label="年份" width="100" align="center" />
          <el-table-column prop="phone" label="手机号" width="140" align="center" />
          <el-table-column prop="payCode" label="缴费码" width="150" show-overflow-tooltip />
          <el-table-column prop="isCancal" label="是否取消" width="100" align="center">
            <template #default="{ row }">
              <el-tag v-if="row.isCancal === '1'" type="danger">已取消</el-tag>
              <el-tag v-else type="success">正常</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="180" align="center" />
          <el-table-column label="操作" align="center" fixed="right" width="200">
            <template #default="scope">
              <el-button icon="View" link type="primary" v-auth="'finance_recruit_stu_project_look'" @click="viewFeeDetail(scope.row)">收费明细</el-button>
              <el-button icon="Edit" link type="primary" v-auth="'finance_recruit_stu_edit'" @click="formDialogRef.openDialog('edit', scope.row)">编辑</el-button>
              <el-button icon="Delete" link type="danger" v-auth="'finance_recruit_stu_del'" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="state.total > 0" :total="state.total" v-model:page="state.page" v-model:limit="state.limit" @pagination="getDataList" />
      </el-card>
    </div>

    <FormDialog ref="formDialogRef" @refresh="getDataList" />
    <FeeDetailDialog ref="feeDetailDialogRef" />
  </div>
</template>

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

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

const tableRef = ref()
const formDialogRef = ref()
const feeDetailDialogRef = ref()
const searchFormRef = ref()
const showSearch = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  pageList: getPage,
  queryForm: {
    serialNumber: '',
    realName: '',
    year: '',
    phone: '',
  },
  createdIsNeed: true
});

const { getDataList, tableStyle } = useTable(state);

const handleReset = () => {
  searchFormRef.value?.resetFields();
  getDataList();
};

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 || '删除失败');
  }
};

const viewFeeDetail = (row: any) => {
  feeDetailDialogRef.value.openDialog(row.serialNumber, row.realName);
};
</script>

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