"use client"; import React, { useState } from "react"; import { Sidebar } from "@/components/dashboard/sidebar"; import { useSidebar } from "@/hooks/useSidebar"; import { useRouter } from "next/navigation"; import { DirectSupplyCreation } from "./direct-supply-creation"; import { WholesalerProductsPage } from "./wholesaler-products-page"; import { TabsHeader } from "./tabs-header"; import { WholesalerGrid } from "./wholesaler-grid"; import { CartSummary } from "./cart-summary"; import { FloatingCart } from "./floating-cart"; import { WholesalerForCreation, WholesalerProduct, SelectedProduct, CounterpartyWholesaler, } from "./types"; import { useQuery } from "@apollo/client"; import { GET_MY_COUNTERPARTIES, GET_ALL_PRODUCTS } from "@/graphql/queries"; export function CreateSupplyPage() { const router = useRouter(); const { getSidebarMargin } = useSidebar(); const [activeTab, setActiveTab] = useState<"cards" | "wholesaler">("cards"); const [selectedWholesaler, setSelectedWholesaler] = useState(null); const [selectedProducts, setSelectedProducts] = useState( [] ); const [showSummary, setShowSummary] = useState(false); const [searchQuery, setSearchQuery] = useState(""); const [canCreateSupply, setCanCreateSupply] = useState(false); const [isCreatingSupply, setIsCreatingSupply] = useState(false); // Загружаем контрагентов-оптовиков const { data: counterpartiesData, loading: counterpartiesLoading } = useQuery( GET_MY_COUNTERPARTIES ); // Загружаем товары для выбранного оптовика const { data: productsData, loading: productsLoading } = useQuery( GET_ALL_PRODUCTS, { skip: !selectedWholesaler, variables: { search: null, category: null }, } ); // Фильтруем только оптовиков const wholesalers: CounterpartyWholesaler[] = ( counterpartiesData?.myCounterparties || [] ).filter((org: { type: string }) => org.type === "WHOLESALE"); // Фильтруем товары по выбранному оптовику const wholesalerProducts: WholesalerProduct[] = selectedWholesaler ? (productsData?.allProducts || []).filter( (product: { organization: { id: string } }) => product.organization.id === selectedWholesaler.id ) : []; const formatCurrency = (amount: number) => { return new Intl.NumberFormat("ru-RU", { style: "currency", currency: "RUB", minimumFractionDigits: 0, }).format(amount); }; const updateProductQuantity = (productId: string, quantity: number) => { const product = wholesalerProducts.find((p) => p.id === productId); if (!product || !selectedWholesaler) return; setSelectedProducts((prev) => { const existing = prev.find( (p) => p.id === productId && p.wholesalerId === selectedWholesaler.id ); if (quantity === 0) { return prev.filter( (p) => !(p.id === productId && p.wholesalerId === selectedWholesaler.id) ); } if (existing) { return prev.map((p) => p.id === productId && p.wholesalerId === selectedWholesaler.id ? { ...p, selectedQuantity: quantity } : p ); } else { return [ ...prev, { ...product, selectedQuantity: quantity, wholesalerId: selectedWholesaler.id, wholesalerName: selectedWholesaler.name, }, ]; } }); }; const getTotalAmount = () => { return selectedProducts.reduce((sum, product) => { const discountedPrice = product.discount ? product.price * (1 - product.discount / 100) : product.price; return sum + discountedPrice * product.selectedQuantity; }, 0); }; const getTotalItems = () => { return selectedProducts.reduce( (sum, product) => sum + product.selectedQuantity, 0 ); }; const handleCreateSupply = () => { if (activeTab === "cards") { console.log("Создание поставки с карточками Wildberries"); } else { console.log("Создание поставки с товарами:", selectedProducts); } router.push("/supplies"); }; const handleGoBack = () => { if (selectedWholesaler) { setSelectedWholesaler(null); setShowSummary(false); } else { router.push("/supplies"); } }; const handleRemoveProduct = (productId: string, wholesalerId: string) => { setSelectedProducts((prev) => prev.filter( (p) => !(p.id === productId && p.wholesalerId === wholesalerId) ) ); }; const handleCartQuantityChange = ( productId: string, wholesalerId: string, quantity: number ) => { setSelectedProducts((prev) => prev.map((p) => p.id === productId && p.wholesalerId === wholesalerId ? { ...p, selectedQuantity: quantity } : p ) ); }; const handleSupplyComplete = () => { router.push("/supplies"); }; const handleCreateSupplyClick = () => { setIsCreatingSupply(true); }; const handleCanCreateSupplyChange = (canCreate: boolean) => { setCanCreateSupply(canCreate); }; const handleSupplyCompleted = () => { setIsCreatingSupply(false); handleSupplyComplete(); }; // Рендер страницы товаров оптовика if (selectedWholesaler && activeTab === "wholesaler") { return ( ); } // Главная страница с табами return (
router.push("/supplies")} cartInfo={ activeTab === "wholesaler" && selectedProducts.length > 0 ? { itemCount: selectedProducts.length, totalAmount: getTotalAmount(), formatCurrency, } : undefined } onCartClick={() => setShowSummary(true)} onCreateSupply={handleCreateSupplyClick} canCreateSupply={canCreateSupply} isCreatingSupply={isCreatingSupply} /> {/* Контент карточек - новый компонент прямого создания поставки */} {activeTab === "cards" && (
)} {/* Контент оптовиков */} {activeTab === "wholesaler" && (
setShowSummary(false)} formatCurrency={formatCurrency} visible={showSummary && selectedProducts.length > 0} /> setShowSummary(true)} visible={selectedProducts.length > 0 && !showSummary} />
)}
); }