import { useEffect, useState } from 'react'; import { Table, Button, Modal, Input, Select, message, Popconfirm, Typography, } from 'antd'; import { ImportOutlined, DeleteOutlined } from '@ant-design/icons'; import { accountApi, userApi } from '../api/modules'; import { getUser, hasPerm } from '../store/auth'; const { TextArea } = Input; const { Text } = Typography; export default function AccountsPage() { const [accounts, setAccounts] = useState([]); const [users, setUsers] = useState([]); const [loading, setLoading] = useState(false); const [importOpen, setImportOpen] = useState(false); const [importText, setImportText] = useState(''); const [importing, setImporting] = useState(false); const user = getUser(); const canViewAll = hasPerm(user, 'account:view_all'); const canImport = hasPerm(user, 'account:import'); const canAssign = hasPerm(user, 'account:assign'); const canDelete = hasPerm(user, 'account:delete'); const loadAccounts = async () => { setLoading(true); try { const data = await accountApi.list(); setAccounts(data); } catch (e: any) { message.error(e.message); } finally { setLoading(false); } }; const loadUsers = async () => { try { const data = await userApi.list(); setUsers(data.filter((u: any) => u.role === 'support')); } catch {} }; useEffect(() => { loadAccounts(); if (canAssign) loadUsers(); }, []); const handleImport = async () => { if (!importText.trim()) { message.warning('请输入账号数据'); return; } setImporting(true); try { const result = await accountApi.import(importText); message.success(result.message); setImportOpen(false); setImportText(''); loadAccounts(); } catch (e: any) { message.error(e.message); } finally { setImporting(false); } }; const handleAssign = async (accountId: number, assignedTo: number | null) => { try { await accountApi.assign(accountId, assignedTo); message.success('已分配'); loadAccounts(); } catch (e: any) { message.error(e.message); } }; const handleDelete = async (id: number) => { try { await accountApi.delete(id); message.success('已删除'); loadAccounts(); } catch (e: any) { message.error(e.message); } }; const columns: any[] = [ { title: 'ID', dataIndex: 'id', width: 60 }, { title: '用户名', dataIndex: 'username' }, ]; if (canViewAll) { columns.push( { title: '密码', dataIndex: 'password', width: 120 }, { title: '邮箱', dataIndex: 'email' }, { title: '邮箱密码', dataIndex: 'email_password', width: 120 }, ); } columns.push({ title: '分配给', dataIndex: 'assigned_username', render: (_: any, record: any) => { if (canAssign) { return (