'use client'; import { useState, useEffect } from 'react'; import { Aircraft } from '@/lib/api'; interface SearchModalProps { isOpen: boolean; onClose: () => void; aircraft: Aircraft[]; searchType: 'organization' | 'dashboard' | 'aircraft'; onNavigate?: (path: string, data?: any) => void; } export default function SearchModal({ isOpen, onClose, aircraft, searchType, onNavigate, }: SearchModalProps) { const [searchQuery, setSearchQuery] = useState(''); const [searchTypeFilter, setSearchTypeFilter] = useState<'registration' | 'aircraftType' | 'operator'>('registration'); const [results, setResults] = useState([]); useEffect(() => { if (searchQuery.trim() === '') { setResults([]); return; } const query = searchQuery.toLowerCase().trim(); let filtered: Aircraft[] = []; if (searchType === 'organization') { // Поиск только по номеру ВС или типу if (searchTypeFilter === 'registration') { filtered = aircraft.filter(a => a.registrationNumber.toLowerCase().includes(query) ); } else if (searchTypeFilter === 'aircraftType') { filtered = aircraft.filter(a => a.aircraftType.toLowerCase().includes(query) ); } } else { // Поиск по номеру ВС, типу или оператору filtered = aircraft.filter(a => a.registrationNumber.toLowerCase().includes(query) || a.aircraftType.toLowerCase().includes(query) || (a.operator && a.operator.toLowerCase().includes(query)) ); } setResults(filtered); }, [searchQuery, searchTypeFilter, aircraft, searchType]); const handleResultClick = (item: Aircraft) => { if (searchType === 'dashboard' && onNavigate) { // Определяем тип поиска и переходим на соответствующую страницу const query = searchQuery.toLowerCase().trim(); if (item.registrationNumber.toLowerCase().includes(query)) { // Поиск по номеру ВС - переход на страницу ВС onNavigate(`/aircraft?highlight=${item.id}`); } else if (item.aircraftType.toLowerCase().includes(query)) { // Поиск по типу ВС - переход на страницу ВС с фильтром по типу onNavigate(`/aircraft?type=${encodeURIComponent(item.aircraftType)}`); } else if (item.operator && item.operator.toLowerCase().includes(query)) { // Поиск по оператору - переход на страницу организаций onNavigate(`/organizations?operator=${encodeURIComponent(item.operator)}`); } else { // По умолчанию - переход на страницу ВС onNavigate(`/aircraft?highlight=${item.id}`); } } onClose(); }; if (!isOpen) { return null; } return (
e.stopPropagation()} >

{searchType === 'organization' || searchType === 'aircraft' ? 'Поиск воздушных судов' : 'Поиск'}

{searchType === 'organization' && (
)}
setSearchQuery(e.target.value)} style={{ width: '100%', padding: '12px', border: '1px solid #ccc', borderRadius: '4px', fontSize: '14px', }} autoFocus />
{results.length > 0 && (
Найдено: {results.length}
{results.map((item) => (
handleResultClick(item)} style={{ padding: '16px', border: '1px solid #e0e0e0', borderRadius: '4px', marginBottom: '8px', cursor: 'pointer', backgroundColor: '#f9f9f9', transition: 'background-color 0.2s', }} onMouseEnter={(e) => { e.currentTarget.style.backgroundColor = '#f0f0f0'; }} onMouseLeave={(e) => { e.currentTarget.style.backgroundColor = '#f9f9f9'; }} >
{item.registrationNumber}
Тип: {item.aircraftType}
{item.operator && item.operator !== 'Не указан' && (
Оператор: {item.operator}
)}
Статус: {item.status} | Налет: {item.flightHours} ч
))}
)} {searchQuery.trim() !== '' && results.length === 0 && (
Ничего не найдено
)} {searchQuery.trim() === '' && (
Введите запрос для поиска
)}
); }