"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { SearchableSelect } from "@/components/searchable-select"; import { AddressAutocomplete } from "@/components/address-autocomplete"; interface Draft { id: string; fromEmail: string; fromName: string | null; subject: string; rawBody: string; extractedCargoDescription: string | null; extractedLengthMm: number | null; extractedWidthMm: number | null; extractedHeightMm: number | null; extractedWeightKg: number | null; extractedRouteFrom: string | null; extractedRouteTo: string | null; extractedDeadline: string | null; confidence: string; } const CONFIDENCE_LABEL: Record = { high: "Высокая", medium: "Средняя", low: "Низкая" }; export function DraftReviewCard({ draft, customers, managers, currentUserId, }: { draft: Draft; customers: { id: string; name: string }[]; managers: { id: string; name: string }[]; currentUserId: string; }) { const router = useRouter(); const [customerId, setCustomerId] = useState(customers[0]?.id ?? ""); const [managerId, setManagerId] = useState(currentUserId); const [cargoDescription, setCargoDescription] = useState(draft.extractedCargoDescription ?? ""); const [routeFrom, setRouteFrom] = useState(draft.extractedRouteFrom ?? ""); const [routeTo, setRouteTo] = useState(draft.extractedRouteTo ?? ""); const [customsPlace, setCustomsPlace] = useState(""); const [deadline, setDeadline] = useState(draft.extractedDeadline ?? ""); const [loading, setLoading] = useState<"accept" | "reject" | null>(null); const [error, setError] = useState(null); async function accept() { setLoading("accept"); setError(null); const res = await fetch(`/api/email-drafts/${draft.id}/accept`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ customerId, managerId, cargoDescription, routeFrom, routeTo, customsPlace: customsPlace || undefined, deadline: deadline || undefined, lengthMm: draft.extractedLengthMm ?? undefined, widthMm: draft.extractedWidthMm ?? undefined, heightMm: draft.extractedHeightMm ?? undefined, weightKg: draft.extractedWeightKg ?? undefined, }), }); setLoading(null); if (!res.ok) { const data = await res.json().catch(() => null); setError(data?.error ?? "Не удалось создать запрос"); return; } router.refresh(); } async function reject() { setLoading("reject"); await fetch(`/api/email-drafts/${draft.id}/reject`, { method: "POST" }); setLoading(null); router.refresh(); } if (customers.length === 0 || managers.length === 0) { return (

Нужны хотя бы один клиент и один менеджер в справочниках, чтобы подтвердить черновик.

); } return (

{draft.subject}

{draft.fromName} <{draft.fromEmail}>

Уверенность: {CONFIDENCE_LABEL[draft.confidence]}
Исходное письмо

{draft.rawBody}

{error &&

{error}

}
); }