<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="projectCode">
            <el-input v-model="state.queryForm.projectCode" placeholder="请输入项目代码" clearable style="width: 150px" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="state.queryForm.status" placeholder="请选择状态" clearable style="width: 150px">
              <el-option label="未缴费" value="0" />
              <el-option label="已缴费" value="10" />
            </el-select>
          </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"><Money /></el-icon>
              学生收费管理
            </span>
            <div class="header-actions">
              <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="projectCode" label="项目代码" width="150" align="center" />
          <el-table-column prop="money" label="应缴金额" width="120" align="right">
            <template #default="{ row }">
              {{ row.money ? Number(row.money).toFixed(2) : '0.00' }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="100" align="center">
            <template #default="{ row }">
              <el-tag v-if="row.status === '0'" type="info">未缴费</el-tag>
              <el-tag v-else-if="row.status === '10'" type="success">已缴费</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="payType" label="缴费方式" width="120" align="center">
            <template #default="{ row }">
              <span v-if="row.payType === '0'">-</span>
              <span v-else>{{ row.payType }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="paiedMoney" label="实缴金额" width="120" align="right">
            <template #default="{ row }">
              {{ row.paiedMoney ? Number(row.paiedMoney).toFixed(2) : '-' }}
            </template>
          </el-table-column>
          <el-table-column prop="payTime" label="缴费时间" width="180" align="center" />
          <el-table-column prop="createTime" label="创建时间" width="180" align="center" />
          <el-table-column label="操作" align="center" fixed="right" width="200">
            <template #default="scope">
              <template v-if="scope.row.status === '0'">
                <el-button link type="primary" v-auth="'finance_recruit_stu_project_edit'" @click="handlePay(scope.row)">缴费</el-button>
                <el-button link type="danger" v-auth="'finance_recruit_stu_project_del'" @click="handleDelete(scope.row)">删除</el-button>
              </template>
              <template v-else>
                <el-button link type="warning" v-auth="'finance_recruit_stu_project_edit'" @click="handleCancelPay(scope.row)">取消缴费</el-button>
              </template>
            </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>

    <el-dialog title="缴费登记" v-model="payDialogVisible" width="400px">
      <el-form label-width="100px">
        <el-form-item label="缴费金额">
          <el-input-number v-model="payForm.money" :min="0" :precision="2" style="width: 100%" />
        </el-form-item>
        <el-form-item label="缴费方式">
          <el-select v-model="payForm.payType" placeholder="请选择缴费方式" style="width: 100%">
            <el-option label="现金" value="20" />
            <el-option label="银行转账" value="30" />
            <el-option label="微信" value="40" />
            <el-option label="支付宝" value="21" />
            <el-option label="其他" value="22" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="payDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitPay">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

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

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

const state: BasicTableProps = reactive<BasicTableProps>({
  pageList: getPage,
  queryForm: {
    serialNumber: '',
    projectCode: '',
    status: '',
  },
  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 payDialogVisible = ref(false);
const payForm = reactive({
  id: '',
  money: 0,
  payType: '',
});

const handlePay = (row: any) => {
  payForm.id = row.id;
  payForm.money = Number(row.money) || 0;
  payForm.payType = '';
  payDialogVisible.value = true;
};

const submitPay = async () => {
  if (!payForm.payType) {
    useMessage().warning('请选择缴费方式');
    return;
  }
  try {
    await payRegister(payForm.id, payForm.payType);
    useMessage().success('缴费成功');
    payDialogVisible.value = false;
    getDataList();
  } catch (err: any) {
    useMessage().error(err.msg || '缴费失败');
  }
};

const handleCancelPay = async (row: any) => {
  try {
    await useMessageBox().confirm('确定要取消该缴费记录吗？', '提示', { type: 'warning' });
    await cancelPay(row.id);
    useMessage().success('取消成功');
    getDataList();
  } catch {
    // 用户取消操作
  }
};
</script>

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