'use client'; import { useEffect, useState } from 'react'; import { Search, Loader2, ChevronRight, Scale, BookOpen, AlertTriangle, FileText, Gavel } from 'lucide-react'; import { useNavigation } from '@/lib/navigation'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Badge } from '@/components/ui/badge'; type SearchType = 'all' | 'sections' | 'judgments' | 'laws' | 'regulations' | 'templates'; interface SearchResults { sections: Array<{ type: 'section'; id: number; lawId: number; lawNameTh: string; category: string; isLaborLaw: boolean; articleKey: string | null; sectionNumber: string | null; snippet: string; isLaborRelated: boolean; }>; judgments: Array<{ type: 'judgment'; id: number; caseNumber: string | null; caseYear: string | null; category: string | null; title: string | null; snippet: string; sourceUrl: string | null; licenseNote: string | null; }>; laws: Array<{ type: 'law'; id: number; lawNameTh: string; lawNameEn: string | null; year: string | null; category: string; isLaborLaw: boolean; sectionCount: number; hitCount?: number; }>; regulations: Array<{ type: 'regulation'; id: number; regulationCode: string; title: string; category: string | null; snippet: string; isRepealed?: boolean; repealStatus?: string; year?: string | null; }>; templates: Array<{ type: 'template'; id: number; templateCode: string; title: string; category: string; snippet: string; }>; total: number; q: string; type: string; } function highlightSnippet(text: string): { __html: string } { // The snippet from FTS contains << and >> markers // Convert to with gold highlight const escaped = text .replaceAll('&', '&') .replaceAll('<', '<') .replaceAll('>', '>'); const html = escaped .replaceAll('<<', '') .replaceAll('>>', ''); return { __html: html }; } export function SearchView({ initialQ, initialType }: { readonly initialQ?: string; readonly initialType?: SearchType }) { const { navigate } = useNavigation(); const [q, setQ] = useState(initialQ || ''); const [type, setType] = useState(initialType || 'all'); const [results, setResults] = useState(null); const [loading, setLoading] = useState(false); const runSearch = (query: string, t: SearchType) => { if (!query.trim()) { setResults(null); return; } setLoading(true); // Update URL const params = new URLSearchParams(); params.set('view', 'search'); params.set('q', query); if (t !== 'all') params.set('type', t); window.history.replaceState({}, '', `/?${params.toString()}`); fetch(`/api/search?q=${encodeURIComponent(query)}&type=${t}&limit=30`) .then(r => r.json()) .then(setResults) .catch(console.error) .finally(() => setLoading(false)); }; useEffect(() => { if (!initialQ) return; setQ(initialQ); setType(initialType || 'all'); runSearch(initialQ, initialType || 'all'); // eslint-disable-next-line react-hooks/exhaustive-deps }, [initialQ, initialType]); const onSubmit = (e: React.SyntheticEvent) => { e.preventDefault(); runSearch(q, type); }; return (

ค้นหาในฐานข้อมูลกฎหมาย

ค้นหาในมาตรากฎหมาย คำพิพากษาฎีกา อนุบัญญัติ และเทมเพลตสัญญา

{/* Search form */}
setQ(e.target.value)} placeholder="เช่น ค่าจ้างล่วงเวลา, เลิกจ้าง, บุริมสิทธิ์, ค่าชดเชย…" className="pl-12 h-14 text-base bg-card-soft border-border/60" autoFocus />
{/* Type filter */}
{([ { v: 'all', label: 'ทั้งหมด' }, { v: 'sections', label: 'มาตรากฎหมาย' }, { v: 'judgments', label: 'คำพิพากษา' }, { v: 'laws', label: 'ชื่อกฎหมาย' }, { v: 'regulations', label: 'อนุบัญญัติ' }, { v: 'templates', label: 'เทมเพลตสัญญา' }, ] as Array<{ v: SearchType; label: string }>).map(opt => ( ))}
{/* Results */} {results && (
พบ {results.total} ผลลัพธ์สำหรับ "{results.q}"
{/* Laws */} {results.laws.length > 0 && ( {results.laws.map(law => ( ))} )} {/* Sections */} {results.sections.length > 0 && ( {results.sections.map(s => ( ))} )} {/* Judgments */} {results.judgments.length > 0 && ( {results.judgments.map(j => ( ))} )} {/* Regulations */} {results.regulations.length > 0 && ( {results.regulations.map(r => { const isSuperseded = r.repealStatus === 'superseded'; return (
{r.regulationCode} {r.category && ( {r.category} )} {r.year && ( พ.ศ. {r.year} )} {r.repealStatus === 'repealed' && ( ยกเลิกแล้ว )} {isSuperseded && ( ฉบับเก่า — มีฉบับใหม่กว่า )} {r.repealStatus === 'active' && ( ใช้บังคับ )}

{r.title}

); })} )} {/* Contract Templates */} {results.templates.length > 0 && ( {results.templates.map(t => ( ))} )} {results.total === 0 && (

ไม่พบผลลัพธ์ ลองค้นหาด้วยคำอื่น

)}
)} {!results && !loading && (

พิมพ์คำค้นหาด้านบนเพื่อเริ่มต้น

ลอง: ค่าจ้าง · เลิกจ้าง · บุริมสิทธิ์ · ค่าชดเชย · สัญญาจ้าง

)}
); } function ResultGroup({ title, icon: Icon, count, children, }: { readonly title: string; readonly icon: any; readonly count: number; readonly children: React.ReactNode; }) { return (

{title} ({count})

{children}
); }