import React, { useState, useMemo, useRef } from "react"; import { useRouter } from "next/router"; import { useQuery } from "@apollo/client"; import { GET_LAXIMO_BRANDS } from "@/lib/graphql"; import { LaximoBrand } from "@/types/laximo"; import { Combobox } from '@headlessui/react'; const tabs = [ "Техническое обслуживание", "Легковые", "Грузовые", "Коммерческие", ]; type Brand = { name: string; code?: string }; const BrandSelectionSection: React.FC = () => { const [activeTab, setActiveTab] = useState(0); const [selectedBrand, setSelectedBrand] = useState(null); const [brandQuery, setBrandQuery] = useState(''); const router = useRouter(); const { data, loading, error } = useQuery<{ laximoBrands: LaximoBrand[] }>(GET_LAXIMO_BRANDS, { errorPolicy: 'all' }); const staticBrands: Brand[] = [ { name: "Audi" }, { name: "BMW" }, { name: "Cadillac" }, { name: "Chevrolet" }, { name: "Citroen" }, { name: "Fiat" }, { name: "Mazda" } ]; let brands: Brand[] = staticBrands; if (data?.laximoBrands && data.laximoBrands.length > 0) { brands = data.laximoBrands.map(brand => ({ name: brand.name, code: brand.code })); } else if (error) { console.warn('Laximo API недоступен, используются статические данные:', error.message); } // Combobox фильтрация const filteredBrands = useMemo(() => { if (!brandQuery) return brands; return brands.filter(b => b.name.toLowerCase().includes(brandQuery.toLowerCase())); }, [brands, brandQuery]); const handleBrandClick = (brand: Brand) => { if (brand.code) { router.push(`/brands?selected=${brand.code}`); } else { console.warn('Brand code not available for', brand.name); } }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (selectedBrand) { const found = brands.find(b => b.code === selectedBrand.code || b.name === selectedBrand.name); if (found && found.code) { router.push(`/brands?selected=${found.code}`); return; } } router.push("/brands"); }; if (loading) { return (

Подбор по маркам

Загрузка брендов...
); } return (

Подбор по маркам

{tabs.map((tab, idx) => (
setActiveTab(idx)} style={{ cursor: "pointer" }} > {tab}
))}
{[...Array(5)].map((_, colIdx) => (
{brands.slice(colIdx * Math.ceil(brands.length / 5), (colIdx + 1) * Math.ceil(brands.length / 5)).map((brand, idx) => ( ))}
))}

ПОДБОР АВТОЗАПЧАСТЕЙ ПО МАРКЕ АВТО

brand?.name || ''} onChange={e => setBrandQuery(e.target.value)} placeholder="Марка" autoComplete="off" /> {filteredBrands.length === 0 && (
Бренды не найдены
)} {filteredBrands.map(brand => ( `px-6 py-4 cursor-pointer hover:!bg-[rgb(236,28,36)] hover:!text-white text-sm transition-colors ${selected ? 'bg-red-50 font-semibold text-gray-950' : 'text-neutral-500'}` } > {brand.name} ))}
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
); }; export default BrandSelectionSection;