'use client'; import { useEffect, useState } from 'react'; import { Search, BookOpen, ExternalLink, Filter, Scale, ChevronRight, Building2, FileText } 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'; import { BackButton } from '@/components/common/BackButton'; import { BookmarkButton } from '@/components/common/BookmarkButton'; interface LawDetailData { lawId: number; lawNameTh: string; lawNameEn: string | null; year: string | null; category: string | null; isLaborLaw: number; status: string | null; fullText: string | null; sourceUrl: string | null; notes: string | null; krisdikaSysid: string | null; lawType: string | null; lawGroup: string | null; lawGoThId: string | null; sections: Array<{ sectionId: number; articleKey: string | null; sectionNumber: string | null; sectionText: string; isLaborRelated: number; isCancelled: number; chapter: string | null; notes: string | null; }>; relatedJudgments?: Array<{ judgmentId: number; judgmentCode: string; dekaNo: string | null; year: string | null; topic: string | null; }>; } export function LawView({ lawId }: { readonly lawId: number }) { const { navigate } = useNavigation(); const [law, setLaw] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [searchQ, setSearchQ] = useState(''); const [laborOnly, setLaborOnly] = useState(false); useEffect(() => { setLoading(true); setError(null); const q = searchQ.trim(); const params = new URLSearchParams({ id: String(lawId) }); if (q) params.set('q', q); const url = `/api/laws?${params.toString()}`; fetch(url) .then(r => { if (!r.ok) throw new Error('Law not found'); return r.json(); }) .then(data => { setLaw(data); // Track in recently viewed import('@/lib/recently-viewed').then(({ addRecentlyViewed }) => { addRecentlyViewed({ type: 'law', id: lawId, label: data.lawNameTh || data.title || `กฎหมาย #${lawId}`, url: `/?view=law&id=${lawId}` }); }); }) .catch(e => setError(e.message)) .finally(() => setLoading(false)); }, [lawId, searchQ]); const visibleSections = (law?.sections || []).filter(s => !laborOnly || s.isLaborRelated === 1); return (
{loading &&
กำลังโหลด…
} {error &&
เกิดข้อผิดพลาด: {error}
} {law && !loading && ( <> {/* Header */}
{law.isLaborLaw === 1 && ( กฎหมายแรงงาน )} {law.lawType && ( {law.lawType} )} {law.lawGroup && ( {law.lawGroup} )} {law.year && พ.ศ. {law.year}} {law.category}

{law.lawNameTh}

{law.lawNameEn && (

{law.lawNameEn}

)}
มาตรา:{' '} {law.sections.length}
มาตราแรงงาน:{' '} {law.sections.filter(s => s.isLaborRelated === 1).length}
{law.krisdikaSysid && ( ดูที่กฤษฎีกา )} {!law.krisdikaSysid && law.sourceUrl && ( ดูที่ law.go.th )}
{/* Search + filter */}
setSearchQ(e.target.value)} className="pl-10 bg-card-soft border-border/60" />
{/* Sections list */} {visibleSections.length === 0 ? (

ไม่พบมาตราที่ตรงกับเงื่อนไข

) : (
{visibleSections.map(s => ( ))}
)} {/* Related Judgments (via cross_references — judgments that cite this law via templates) */} {law.relatedJudgments && law.relatedJudgments.length > 0 && (

คำพิพากษาฎีกาที่เกี่ยวข้อง ({law.relatedJudgments.length})

ฎีกาที่อ้างอิงกฎหมายนี้ผ่านเทมเพลตเอกสาร (cross-references)

{law.relatedJudgments.map(j => ( ))}
)} )}
); } function SectionCard({ section, lawNameTh, }: { readonly section: LawDetailData['sections'][number]; readonly lawNameTh: string; }) { const { navigate } = useNavigation(); return ( ); }