import { fmtAmount, fmtDate, statusLabel } from "../../shared/utils.js"; export function DashboardSection({ dashboardData, token, status, apiCall, onOpenRequest, DataTableComponent, StatusLineComponent, UserAvatarComponent, }) { const { useMemo, useState } = React; const DataTable = DataTableComponent; const StatusLine = StatusLineComponent; const UserAvatar = UserAvatarComponent; const [lawyerModal, setLawyerModal] = useState({ open: false, loading: false, error: "", lawyer: null, rows: [], totals: { amount: 0, salary: 0 }, }); const statusCards = useMemo(() => { return Object.entries(dashboardData?.byStatus || {}) .map(([label, value]) => ({ label, value })) .sort((a, b) => String(a.label).localeCompare(String(b.label), "ru")); }, [dashboardData?.byStatus]); const fmtThousandsCompact = (value) => { const amount = Number(value || 0); if (!Number.isFinite(amount)) return "0"; return new Intl.NumberFormat("ru-RU", { minimumFractionDigits: 0, maximumFractionDigits: 1, }).format(amount / 1000); }; const openLawyerModal = async (lawyerRow) => { if (!lawyerRow?.lawyer_id || typeof apiCall !== "function") return; setLawyerModal({ open: true, loading: true, error: "", lawyer: lawyerRow, rows: [], totals: { amount: 0, salary: 0 }, }); try { const data = await apiCall("/api/admin/metrics/lawyers/" + encodeURIComponent(String(lawyerRow.lawyer_id)) + "/active-requests"); setLawyerModal((prev) => ({ ...prev, loading: false, error: "", rows: Array.isArray(data?.rows) ? data.rows : [], totals: { amount: Number(data?.totals?.amount || 0), salary: Number(data?.totals?.salary || 0), }, })); } catch (error) { setLawyerModal((prev) => ({ ...prev, loading: false, error: error.message || "Ошибка загрузки" })); } }; const closeLawyerModal = () => { setLawyerModal({ open: false, loading: false, error: "", lawyer: null, rows: [], totals: { amount: 0, salary: 0 } }); }; const lawyerCards = Array.isArray(dashboardData?.lawyerLoads) ? dashboardData.lawyerLoads : []; return ( <>

Обзор метрик

Состояние заявок, финансы месяца и загрузка юристов.

{(dashboardData?.cards || []).map((card) => (

{card.label}

{card.value}
))}
{statusCards.length ? (

Статусы заявок

Текущая раскладка по всем статусам.

{statusCards.map((card) => (

{card.label}

{String(card.value ?? 0)}
))}
) : null} {dashboardData?.scope === "LAWYER" ? (
{JSON.stringify(dashboardData?.myUnreadByEvent || {}, null, 2)}
) : null}

Загрузка юристов

{lawyerCards.length ? ( lawyerCards.map((row) => ( )) ) : (

Юристы

Нет данных
)}
event.stopPropagation()}>

{lawyerModal.lawyer ? "Юрист: " + (lawyerModal.lawyer.name || lawyerModal.lawyer.email || "-") : "Юрист"}

{lawyerModal.lawyer ? (

{(lawyerModal.lawyer.primary_topic_code || "Тема не указана") + " • " + (lawyerModal.lawyer.email || "")}

) : null}
{lawyerModal.lawyer ? (
В работе{String(lawyerModal.lawyer.active_load ?? 0)}
Новые{String(lawyerModal.lawyer.monthly_assigned_count ?? 0)}
Завершенные{String(lawyerModal.lawyer.monthly_completed_count ?? 0)}
Сумма{fmtAmount(lawyerModal.lawyer.monthly_paid_gross)}
Зарплата{fmtAmount(lawyerModal.lawyer.monthly_salary)}
) : null}
{lawyerModal.loading ?

Загрузка активных заявок...

: null} {lawyerModal.error ?

{lawyerModal.error}

: null} {!lawyerModal.loading ? ( <>
( {statusLabel(row.status_code)} {row.client_name || "-"} {fmtDate(row.created_at)} {fmtAmount(row.invoice_amount)} {fmtAmount(row.month_paid_amount)} {fmtAmount(row.month_salary_amount)} )} />
) : null}
{!lawyerModal.loading ? (
Активных: {String((lawyerModal.rows || []).length)}
Оплаты: {fmtAmount(lawyerModal.totals.amount)}
Зарплата: {fmtAmount(lawyerModal.totals.salary)}
) : null}
); } export default DashboardSection;