"use client" import { useCallback, useEffect, useState } from "react" import { CalendarClockIcon, MoreHorizontalIcon, PlusIcon, RefreshCwIcon, SearchIcon, Trash2Icon, } from "lucide-react" import { toast } from "sonner" import { createAgentTeamSchedule, deleteAgentTeamSchedule, fetchAgentTeamSchedules, fetchAgentTeams, updateAgentTeamSchedule, type AdminAgentTeam, type AdminAgentTeamSchedule, type CreateAdminAgentTeamSchedulePayload, type PageResult, } from "@/lib/api/admin" import { formatDateTime } from "@/lib/utils" import { EditDialog } from "./_components/edit" import { Button } from "@/components/ui/button" import { ButtonGroup } from "@/components/ui/button-group" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { Input } from "@/components/ui/input" import { ListPagination } from "@/components/list-pagination" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" export default function DashboardAgentTeamSchedulesPage() { const [teamFilterInput, setTeamFilterInput] = useState("all") const [teamFilter, setTeamFilter] = useState("all") const [page, setPage] = useState(1) const [limit, setLimit] = useState(20) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [actionLoadingId, setActionLoadingId] = useState(null) const [dialogOpen, setDialogOpen] = useState(false) const [editingItem, setEditingItem] = useState(null) const [teams, setTeams] = useState([]) const [result, setResult] = useState>({ results: [], page: { page: 1, limit: 20, total: 0 }, }) const loadData = useCallback(async () => { setLoading(true) try { const data = await fetchAgentTeamSchedules({ teamId: teamFilter === "all" ? undefined : teamFilter, page, limit, }) setResult(data) } catch (error) { toast.error(error instanceof Error ? error.message : "加载客服组排班失败") } finally { setLoading(false) } }, [limit, page, teamFilter]) const loadTeams = useCallback(async () => { try { const data = await fetchAgentTeams() setTeams(data) } catch (error) { toast.error(error instanceof Error ? error.message : "加载客服组选项失败") } }, []) useEffect(() => { void loadData() }, [loadData]) useEffect(() => { void loadTeams() }, [loadTeams]) function applyFilters() { setTeamFilter(teamFilterInput) setPage(1) } function handlePageChange(nextPage: number) { if (nextPage < 1 || nextPage === page) { return } setPage(nextPage) } function openCreateDialog() { setEditingItem(null) setDialogOpen(true) } function openEditDialog(item: AdminAgentTeamSchedule) { setEditingItem(item) setDialogOpen(true) } function handleDialogOpenChange(open: boolean) { if (saving) { return } if (!open) { setEditingItem(null) } setDialogOpen(open) } async function handleSubmit(payload: CreateAdminAgentTeamSchedulePayload) { if (saving) { return } setSaving(true) try { if (editingItem) { await updateAgentTeamSchedule({ id: editingItem.id, ...payload }) toast.success("已更新客服组排班") } else { await createAgentTeamSchedule(payload) toast.success("已创建客服组排班") } setDialogOpen(false) setEditingItem(null) await loadData() } catch (error) { toast.error(error instanceof Error ? error.message : "保存客服组排班失败") } finally { setSaving(false) } } async function handleDelete(item: AdminAgentTeamSchedule) { setActionLoadingId(item.id) try { await deleteAgentTeamSchedule(item.id) toast.success("已删除客服组排班") await loadData() } catch (error) { toast.error(error instanceof Error ? error.message : "删除客服组排班失败") } finally { setActionLoadingId(null) } } return ( <>
客服组 时间范围 来源 操作 {result.results.map((item) => (
{item.teamName || `客服组#${item.teamId}`}
组ID:{item.teamId}
{formatDateTime(item.startAt)}
{formatDateTime(item.endAt)}
{item.sourceType}
} aria-label={`更多操作 ${item.startAt}`} > void handleDelete(item)} className="text-destructive focus:text-destructive" > {actionLoadingId === item.id ? "删除中..." : "删除"}
))} {!loading && result.results.length === 0 ? ( 没有匹配的客服组排班 ) : null}
{ setLimit(nextLimit) setPage(1) }} />
) }