<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="projectName">
            <el-input v-model="state.queryForm.projectName" placeholder="请输入项目名称" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item label="项目代码" prop="projectCode">
            <el-input v-model="state.queryForm.projectCode" 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>
            <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"><Document /></el-icon>
              收费项目管理
            </span>
            <div class="header-actions">
              <el-button icon="FolderAdd" type="primary" @click="formDialogRef.openDialog('add')" v-auth="'finance_recruit_project_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="projectName" label="项目名称" min-width="200" show-overflow-tooltip />
          <el-table-column prop="projectCode" label="项目代码" width="150" align="center" />
          <el-table-column prop="year" label="年份" width="100" align="center" />
          <el-table-column prop="sort" label="排序" width="80" align="center" />
          <el-table-column prop="createTime" label="创建时间" width="180" align="center" />
          <el-table-column label="操作" align="center" fixed="right" width="150">
            <template #default="scope">
              <el-button icon="Edit" link type="primary" v-auth="'finance_recruit_project_edit'" @click="formDialogRef.openDialog('edit', scope.row)">编辑</el-button>
              <el-button icon="Delete" link type="danger" v-auth="'finance_recruit_project_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" />
  </div>
</template>

<script setup lang="ts" name="FinanceRecruitProject">
import { ref, reactive, defineAsyncComponent } from 'vue'
import { BasicTableProps, useTable } from "/@/hooks/table";
import { getPage, delObj } from "/@/api/finance/recruitProject";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { Search, Document } 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: {
    projectName: '',
    projectCode: '',
    year: '',
  },
  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 || '删除失败');
  }
};
</script>

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