import React from "react";
import { formatDateTime } from "../../utils/formatters";
import { Badge } from "../UI/Badge";
import { Button } from "../UI/Button";
import { Select } from "../UI/Select";
import { Panel } from "../UI/Panel";
import { DriverShipmentPanel } from "../driver/DriverShipmentPanel";
import {
getOrderGroupDeliveryStatusLabel,
getOrderGroupDisplayStatusLabel,
getOrderGroupStatusTone,
} from "../../services/orderGroupViews";
const DELIVERY_TIME_OPTIONS = ["Первая половина дня", "Вторая половина дня"];
const WEEK_DAY_LABELS = ["ПН", "ВТ", "СР", "ЧТ", "ПТ", "СБ", "ВС"];
const DELIVERY_TIME_ALIASES = {
"До обеда": "Первая половина дня",
"После обеда": "Вторая половина дня",
};
const renderList = (values) => {
if (!Array.isArray(values) || !values.length) {
return
Карточка группы доставки
{order.displayTitle || order.customerName || order.groupKey}
{order.displaySubtitle || [order.customerPhone, order.customerDate].filter(Boolean).join(" · ") || "Не указано"}
{getOrderGroupDisplayStatusLabel(order)}
Дата доставки
{formatDeliveryDateDisplay(order.deliveryDate)}
Время доставки
{renderValue(order.deliveryTime || order.deliveryHalfDay)}
Водитель
{order.assignedDriverId ? renderValue(order.assignedDriverName) : "Не назначен"}
Адрес доставки
{renderValue(order.deliveryAddress)}
Номер счёта
{renderValue(order.orderNumberSummary)}
Клиент
{renderValue(order.customerName)}
Дата счёта
{renderValue(order.customerDate)}
Всего заказов
{order.ordersCount ?? 0}
Готово
{order.readyCount ?? 0}
Не готово
{order.notReadyCount ?? 0}
Обновлена
{formatDateTime(order.updatedAt)}
Статус доставки
{getOrderGroupDeliveryStatusLabel(order.deliveryStatus || order.delivery_status)}
{canManageDelivery ? (
Ручное согласование доставки
{isDeliveryAgreed
? "Дата и половина дня доставки уже зафиксированы."
: "Если клиент согласовал доставку по телефону, сохраните дату и половину дня здесь."}
{isDeliveryAgreed ? (
Доставка согласована
{agreedDeliveryLabel || "Дата и время сохранены"}
Согласовано
) : (
setIsCalendarOpen((current) => !current)}
>
{formatDateForDisplay(deliveryDate)}
▾
{isCalendarOpen ? (
Календарь доставки
{monthLabel}
setCurrentMonth((month) => addMonths(month, -1))}
>
‹
setCurrentMonth((month) => addMonths(month, 1))}
>
›
{WEEK_DAY_LABELS.map((day) => (
{day}
))}
{calendarDays.map((day, index) => {
if (!day) {
return
;
}
const dateKey = toDateKey(day);
const isWeekend = isWeekendDate(day);
const isSelectable = isSelectableCalendarDate(day, minSelectableDateKey);
const isSelected = dateKey === deliveryDate;
const isDisabled = !isSelectable;
const dayNumber = String(day.getDate()).padStart(2, "0");
return (
{
if (isDisabled) {
return;
}
setDeliveryDate(dateKey);
setFormMessage("");
setIsCalendarOpen(false);
}}
>
{dayNumber}
{isWeekend ? (
) : null}
);
})}
Выходные отмечены пунктиром и недоступны.
) : null}
{DELIVERY_TIME_OPTIONS.map((option) => (
{
setDeliveryTime(option);
setFormMessage("");
}}
>
{option}
))}
{isSavingDeliveryChoice ? "Сохраняем..." : "Согласовать"}
)}
{formMessage ? (
{formMessage}
) : null}
) : null}
{canManageDelivery && ["manager", "logistician", "admin"].includes(userRole) ? (
Назначение водителя
{order.assignedDriverId
? `Назначен водитель: ${order.assignedDriverName || "Неизвестно"}. Вы можете изменить назначение.`
: "Выберите водителя для доставки."}
{
setSelectedDriverId(e.target.value);
setDriverMessage("");
}}
disabled={isSavingDeliveryChoice}
>
{order.assignedDriverId ? "Сменить водителя..." : "Выберите водителя..."}
{drivers.map((driver) => (
{driver.name || driver.email}
))}
{isSavingDeliveryChoice ? "Назначаем..." : "Назначить"}
{driverMessage ? (
{driverMessage}
) : null}
) : null}
{["manager", "logistician", "admin"].includes(userRole) && order && onChangeDeliveryStatus ? (
Статус доставки
Измените статус, если водитель забыл обновить или нужна корректировка.
{[
{ value: "pending_confirmation", label: "Ожидает согласования" },
{ value: "agreed", label: "Согласовано" },
{ value: "driver_assigned", label: "Назначен водитель" },
{ value: "loaded", label: "Загружено" },
{ value: "on_route", label: "В пути" },
{ value: "delivered", label: "Доставлено" },
{ value: "problem", label: "Проблема" },
{ value: "cancelled", label: "Отменено" },
].map((statusOption) => (
{
onChangeDeliveryStatus({
orderGroupId: order.id,
status: statusOption.value,
}).then((response) => {
if (!response.success) {
setFormMessage(response.error || "Не удалось обновить статус");
} else {
setFormMessage("");
}
});
}}
disabled={isSavingDeliveryChoice}
>
{statusOption.label}
))}
) : null}
{["manager", "logistician", "admin"].includes(userRole) && order && onChangeDeliveryStatus ? (
) : null}
{userRole === "driver" && order ? (
) : null}
{userRole === "driver" && order && onChangeDeliveryStatus ? (
Статус доставки
Обновите статус по мере выполнения доставки.
{[
{ value: "loaded", label: "Загружено" },
{ value: "on_route", label: "В пути" },
{ value: "delivered", label: "Доставлено" },
{ value: "problem", label: "Проблема" },
].map((statusOption) => (
{
if (statusOption.value === "delivered" && shipmentState && !shipmentState.canMarkDelivered) {
setFormMessage("Укажите причину для каждой неотгруженной позиции перед завершением доставки.");
return;
}
onChangeDeliveryStatus({
orderGroupId: order.id,
status: statusOption.value,
}).then((response) => {
if (!response.success) {
setFormMessage(response.error || "Не удалось обновить статус");
} else {
setFormMessage("");
}
});
}}
disabled={isSavingDeliveryChoice}
>
{statusOption.label}
))}
{formMessage ? (
{formMessage}
) : null}
) : null}
Номера заказов
{renderList(order.orderNumbers)}
{userRole !== "driver" ? (
Дополнительные данные
{order.firstSmsSentAt ? (
1-е SMS отправлено
{formatDateTime(order.firstSmsSentAt)}
) : null}
{order.secondSmsSentAt ? (
2-е SMS отправлено
{formatDateTime(order.secondSmsSentAt)}
) : null}
{!order.firstSmsSentAt && !order.secondSmsSentAt ? (
) : null}
Ручное согласование выполнено
{order.manualConfirmationAt ? formatDateTime(order.manualConfirmationAt) : "Нет"}
Платное хранение
{order.paidStorageAt ? formatDateTime(order.paidStorageAt) : "Нет"}
{order.createdFromExchangeAt ? (
Создано из обмена
{formatDateTime(order.createdFromExchangeAt)}
) : null}
) : null}
);
};