import { useEffect, useMemo, useState } from 'react'; import { Download, Search } from 'lucide-react'; import { adminApi, type AdminChannel, type DailyQualityReport, type EnterpriseApplication, type TenantOption } from '@/api/adminApi'; import { Breadcrumb, Button, DateRangeInput, Pagination, Select, Tabs, Tag, type DateRangeValue } from '@/components/ui'; import { formatDateTime } from '@/utils/dateTime'; type QualityDimension = 'application' | 'channel' | 'signature' | 'drainage'; const pageSize = 20; const dimensionLabels: Record = { application: '企业应用', channel: '通道', signature: '签名', drainage: '引流信息', }; export function AdminQualityReportsPage() { const [dimension, setDimension] = useState('application'); const [rows, setRows] = useState([]); const [tenants, setTenants] = useState([]); const [applications, setApplications] = useState([]); const [channels, setChannels] = useState([]); const [dateRange, setDateRange] = useState(defaultDateRange()); const [tenantId, setTenantId] = useState(''); const [applicationId, setApplicationId] = useState(''); const [channelId, setChannelId] = useState(''); const [page, setPage] = useState(1); const [total, setTotal] = useState(0); const [loading, setLoading] = useState(true); const [error, setError] = useState(''); const [exporting, setExporting] = useState(false); useEffect(() => { Promise.all([adminApi.listTenants(), adminApi.listEnterpriseApplications(), adminApi.listChannels()]) .then(([nextTenants, nextApplications, nextChannels]) => { setTenants(nextTenants); setApplications(nextApplications); setChannels(nextChannels); }) .catch(() => { setTenants([]); setApplications([]); setChannels([]); }); }, []); useEffect(() => { void loadData(); }, [dimension, page, dateRange.start, dateRange.end, tenantId, applicationId, channelId]); async function loadData() { setLoading(true); setError(''); try { const response = await adminApi.listQualityReports({ dimensionType: dimension, dateFrom: dateRange.start, dateTo: dateRange.end, tenantId: dimension === 'channel' ? undefined : tenantId || undefined, applicationId: dimension === 'channel' ? undefined : applicationId || undefined, channelId: dimension === 'channel' ? channelId || undefined : undefined, page, pageSize, }); setRows(response.items); setTotal(response.total); } catch (failure) { setRows([]); setTotal(0); setError(failure instanceof Error ? failure.message : '发送质量报表加载失败'); } finally { setLoading(false); } } async function exportData() { setExporting(true); setError(''); try { downloadBlob(await adminApi.exportQualityReports({ dimensionType: dimension, dateFrom: dateRange.start, dateTo: dateRange.end, tenantId: dimension === 'channel' ? undefined : tenantId || undefined, applicationId: dimension === 'channel' ? undefined : applicationId || undefined, channelId: dimension === 'channel' ? channelId || undefined : undefined }), '发送质量报表.csv'); } catch (failure) { setError(failure instanceof Error ? failure.message : '发送质量报表导出失败'); } finally { setExporting(false); } } const availableApplications = useMemo(() => applications.filter((application) => !tenantId || application.tenantId === tenantId), [applications, tenantId]); const totalPages = Math.max(1, Math.ceil(total / pageSize)); function changeDimension(value: string) { setDimension(value as QualityDimension); setTenantId(''); setApplicationId(''); setChannelId(''); setPage(1); } const reportPanel = (
{ setDateRange(value); setPage(1); }} value={dateRange} /> {dimension === 'channel' ? { setTenantId(event.target.value); setApplicationId(''); setPage(1); }} options={[{ label: '全部企业', value: '' }, ...tenants.map((tenant) => ({ label: tenant.name, value: tenant.id }))]} value={tenantId} />} {dimension === 'channel' ?
: