'use client'; import { createColumnHelper, flexRender, getCoreRowModel, getSortedRowModel, useReactTable, type SortingState, } from '@tanstack/react-table'; import { ArrowUpDown, MapPin } from 'lucide-react'; import { useState } from 'react'; import { Badge } from '@/components/ui/badge'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card'; import { formatPrice, formatPricePerM2 } from '@/lib/currency'; import type { ValuationComparable } from '@/lib/valuation-api'; interface ComparablesTableProps { comparables: ValuationComparable[]; } const columnHelper = createColumnHelper(); function getSimilarityBadge(similarity: number): { label: string; variant: 'success' | 'warning' | 'info'; } { const pct = Math.round(similarity * 100); if (pct >= 85) return { label: `${pct}% tương tự`, variant: 'success' }; if (pct >= 70) return { label: `${pct}% tương tự`, variant: 'info' }; return { label: `${pct}% tương tự`, variant: 'warning' }; } const columns = [ columnHelper.accessor('title', { header: 'Bất động sản', cell: (info) => { const row = info.row.original; return (

{info.getValue()}

{row.district} {row.address ? ` — ${row.address}` : ''}

); }, }), columnHelper.accessor('areaM2', { header: 'Diện tích', cell: (info) => {info.getValue()} m², }), columnHelper.accessor('priceVND', { header: 'Giá', cell: (info) => ( {formatPrice(info.getValue())} ), }), columnHelper.accessor('pricePerM2', { header: 'Giá/m²', cell: (info) => ( {formatPricePerM2(info.getValue())} ), }), columnHelper.accessor('similarity', { header: 'Tương đồng', cell: (info) => { const badge = getSimilarityBadge(info.getValue()); return {badge.label}; }, sortDescFirst: true, }), ]; export function ComparablesTable({ comparables }: ComparablesTableProps) { const [sorting, setSorting] = useState([ { id: 'similarity', desc: true }, ]); const table = useReactTable({ data: comparables, columns, state: { sorting }, onSortingChange: setSorting, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), }); if (comparables.length === 0) return null; return ( Bất động sản tương tự {comparables.length} bất động sản có đặc điểm tương tự trong khu vực
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( ))} ))} {table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( ))} ))}
{header.isPlaceholder ? null : ( )}
{flexRender(cell.column.columnDef.cell, cell.getContext())}
); }