104 lines
8.1 KiB
Vue
104 lines
8.1 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, ref } from 'vue'
|
|
import { useI18n } from 'vue-i18n'
|
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
|
import http, { apiErrorMessage } from '../api/http'
|
|
import { downloadApiFile } from '../api/excel'
|
|
|
|
interface WalletStudent { id: string; studentNumber: string; name: string; collegeName: string; majorName: string; className: string; balance: number; lastChangedAt?: string }
|
|
interface Option { id: string; name: string }
|
|
interface MajorOption extends Option { collegeId: string }
|
|
interface ClassOption extends Option { grade: number; majorId: string }
|
|
|
|
const items = ref<WalletStudent[]>([])
|
|
const total = ref(0)
|
|
const page = ref(1)
|
|
const keyword = ref('')
|
|
const loading = ref(false)
|
|
const fileInput = ref<HTMLInputElement | null>(null)
|
|
const pageSize = 20
|
|
const colleges = ref<Option[]>([])
|
|
const majors = ref<MajorOption[]>([])
|
|
const classes = ref<ClassOption[]>([])
|
|
const filters = ref({ grade: undefined as number | undefined, collegeId: '', majorId: '', administrativeClassId: '' })
|
|
const filteredMajors = computed(() => majors.value.filter(x => !filters.value.collegeId || x.collegeId === filters.value.collegeId))
|
|
const filteredClasses = computed(() => classes.value.filter(x => (!filters.value.majorId || x.majorId === filters.value.majorId) && (!filters.value.grade || x.grade === filters.value.grade)))
|
|
const { locale, t } = useI18n()
|
|
const currency = (value: number) => new Intl.NumberFormat(locale.value, { style: 'currency', currency: 'CNY' }).format(value)
|
|
const walletStudent = (value: unknown) => value as WalletStudent
|
|
const formatTime = (value?: string) => value ? new Date(value).toLocaleString(locale.value, { hour12: false }) : t('wallet.neverAdjusted')
|
|
|
|
async function load() {
|
|
loading.value = true
|
|
try {
|
|
const { data } = await http.get('/campus-registrations/wallets/students', { params: { keyword: keyword.value || undefined, ...filters.value, page: page.value, pageSize } })
|
|
items.value = data.items; total.value = data.total
|
|
} catch (error) { ElMessage.error(apiErrorMessage(error)) }
|
|
finally { loading.value = false }
|
|
}
|
|
function resetDependent(level: 'college' | 'major' | 'grade') {
|
|
if (level === 'college') { filters.value.majorId = ''; filters.value.administrativeClassId = '' }
|
|
if (level === 'major' || level === 'grade') filters.value.administrativeClassId = ''
|
|
}
|
|
async function loadOptions() {
|
|
try { const { data } = await http.get('/campus-registrations/wallets/filter-options'); colleges.value = data.colleges; majors.value = data.majors; classes.value = data.classes }
|
|
catch (error) { ElMessage.error(apiErrorMessage(error)) }
|
|
}
|
|
async function adjust(item: WalletStudent) {
|
|
try {
|
|
const { value: amount } = await ElMessageBox.prompt(t('wallet.adjustPrompt', { name: item.name, number: item.studentNumber }), t('wallet.adjustTitle'), { inputPattern: /^-?\d+(\.\d{1,2})?$/, inputErrorMessage: t('wallet.amountError') })
|
|
const { value: reason } = await ElMessageBox.prompt(t('wallet.reasonPrompt'), t('wallet.reasonTitle'), { inputValidator: value => value.trim() ? true : t('wallet.reasonRequired') })
|
|
await http.post(`/campus-registrations/wallets/students/${item.id}/adjust`, { amount: Number(amount), reason })
|
|
ElMessage.success(t('wallet.adjusted')); await load()
|
|
} catch (error) { if (error !== 'cancel' && error !== 'close') ElMessage.error(apiErrorMessage(error)) }
|
|
}
|
|
async function downloadTemplate() {
|
|
try { await downloadApiFile('/campus-registrations/wallets/adjustment-template.xlsx', t('wallet.templateFilename')) }
|
|
catch (error) { ElMessage.error(apiErrorMessage(error)) }
|
|
}
|
|
async function exportWallets() {
|
|
try {
|
|
await downloadApiFile('/campus-registrations/wallets/export.xlsx', t('wallet.exportFilename'), {
|
|
params: { keyword: keyword.value || undefined, ...filters.value },
|
|
})
|
|
}
|
|
catch (error) { ElMessage.error(apiErrorMessage(error)) }
|
|
}
|
|
async function importFile(event: Event) {
|
|
const file = (event.target as HTMLInputElement).files?.[0]; if (!file) return
|
|
const form = new FormData(); form.append('file', file)
|
|
try { const { data } = await http.post('/campus-registrations/wallets/import-adjustments', form); ElMessage.success(t('wallet.imported', { count: data.adjusted })); await load() }
|
|
catch (error) { ElMessage.error(apiErrorMessage(error)) }
|
|
finally { if (fileInput.value) fileInput.value.value = '' }
|
|
}
|
|
onMounted(async () => { await loadOptions(); await load() })
|
|
</script>
|
|
|
|
<template>
|
|
<section class="wallet-page" v-loading="loading">
|
|
<header><p>FINANCIAL LEDGER</p><h2>{{ t('wallet.title') }}</h2><span>{{ t('wallet.description') }}</span></header>
|
|
<el-card>
|
|
<div class="toolbar">
|
|
<el-input v-model="keyword" clearable :placeholder="t('wallet.searchPlaceholder')" @keyup.enter="page = 1; load()" />
|
|
<el-select v-model="filters.grade" clearable :placeholder="t('wallet.grade')" @change="resetDependent('grade')"><el-option v-for="value in [...new Set(classes.map(x => x.grade))].sort()" :key="value" :label="t('wallet.gradeOption', { grade: value })" :value="value" /></el-select>
|
|
<el-select v-model="filters.collegeId" clearable :placeholder="t('wallet.college')" @change="resetDependent('college')"><el-option v-for="item in colleges" :key="item.id" :label="item.name" :value="item.id" /></el-select>
|
|
<el-select v-model="filters.majorId" clearable :placeholder="t('wallet.major')" @change="resetDependent('major')"><el-option v-for="item in filteredMajors" :key="item.id" :label="item.name" :value="item.id" /></el-select>
|
|
<el-select v-model="filters.administrativeClassId" clearable :placeholder="t('wallet.class')"><el-option v-for="item in filteredClasses" :key="item.id" :label="t('wallet.classOption', { grade: item.grade, name: item.name })" :value="item.id" /></el-select>
|
|
<el-button type="primary" @click="page = 1; load()">{{ t('wallet.search') }}</el-button>
|
|
<el-button @click="downloadTemplate">{{ t('wallet.downloadTemplate') }}</el-button>
|
|
<el-button @click="fileInput?.click()">{{ t('wallet.import') }}</el-button>
|
|
<el-button @click="exportWallets">{{ t('wallet.export') }}</el-button>
|
|
<input ref="fileInput" type="file" accept=".xlsx" hidden @change="importFile" />
|
|
</div>
|
|
<el-alert type="info" :closable="false" :title="t('wallet.batchHint')" />
|
|
<el-table :data="items" style="margin-top:16px"><el-table-column :label="t('wallet.student')" min-width="210"><template #default="{ row }"><b>{{ walletStudent(row).studentNumber }} · {{ walletStudent(row).name }}</b><small>{{ walletStudent(row).collegeName }} · {{ walletStudent(row).majorName }} · {{ walletStudent(row).className }}</small></template></el-table-column><el-table-column :label="t('wallet.balance')" width="150"><template #default="{ row }"><strong>{{ currency(walletStudent(row).balance) }}</strong></template></el-table-column><el-table-column :label="t('wallet.lastChanged')" width="190"><template #default="{ row }">{{ formatTime(walletStudent(row).lastChangedAt) }}</template></el-table-column><el-table-column :label="t('wallet.actions')" width="120" fixed="right"><template #default="{ row }"><el-button type="primary" link @click="adjust(walletStudent(row))">{{ t('wallet.adjust') }}</el-button></template></el-table-column></el-table>
|
|
<el-empty v-if="items.length === 0" :description="t('wallet.empty')" />
|
|
<el-pagination v-if="total > pageSize" v-model:current-page="page" :total="total" :page-size="pageSize" layout="prev, pager, next" @current-change="load" />
|
|
</el-card>
|
|
</section>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.wallet-page{display:grid;gap:20px}.wallet-page header p{margin:0;color:#17867c;font-size:12px;font-weight:800;letter-spacing:.16em}.wallet-page h2{margin:6px 0;color:#183b56}.wallet-page header span,small{color:#738596}.toolbar{display:flex;flex-wrap:wrap;gap:10px;margin-bottom:16px}.toolbar .el-input{max-width:260px}.toolbar .el-select{width:150px}.el-table b,.el-table small{display:block}.el-table strong{color:#17867c;font-size:16px}.el-pagination{justify-content:flex-end;margin-top:16px}@media(max-width:720px){.toolbar{flex-direction:column}.toolbar .el-input,.toolbar .el-select,.toolbar .el-button{max-width:none;width:100%}}
|
|
</style>
|