import { CommonModule } from '@angular/common'; import { Component, OnInit } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { AdminUserRow, AuthCreditService } from '../../services/auth-credit.service'; @Component({ selector: 'app-admin-users', standalone: true, imports: [CommonModule, FormsModule], templateUrl: './admin-users.component.html', styleUrls: ['./admin-users.component.css'], }) export class AdminUsersComponent implements OnInit { users: AdminUserRow[] = []; loading = false; error = ''; message = ''; adjustAmount: Record = {}; adjustNote: Record = {}; createForm = { username: '', password: '', displayName: '', email: '', phone: '', role: 'user' as 'user' | 'admin', initialCredits: 10, }; constructor(public auth: AuthCreditService) {} ngOnInit(): void { this.reload(); } async reload(): Promise { if (!this.auth.isAdmin) return; this.loading = true; this.error = ''; try { this.users = await this.auth.adminListUsers(); } catch (e: any) { this.error = e?.message || '加载账号明细失败'; } finally { this.loading = false; } } async adjust(user: AdminUserRow): Promise { this.error = ''; try { const amount = Number(this.adjustAmount[user.objectId] || 0); if (!amount) throw new Error('请输入调整积分'); await this.auth.adminAdjustCredit(user.objectId, amount, this.adjustNote[user.objectId] || '管理员调整'); this.adjustAmount[user.objectId] = 0; this.adjustNote[user.objectId] = ''; await this.reload(); } catch (e: any) { this.error = e?.message || '调整失败'; } } async createUser(): Promise { this.error = ''; this.message = ''; try { const user = await this.auth.adminCreateUser(this.createForm); this.message = `账号 ${user.username} 已创建`; this.createForm = { username: '', password: '', displayName: '', email: '', phone: '', role: 'user', initialCredits: 10, }; await this.reload(); } catch (e: any) { this.error = e?.message || '创建账号失败'; } } }