'use client';
import dynamic from 'next/dynamic';
import Link from 'next/link';
import * as React from 'react';
import { AddToCompareButton } from '@/components/comparison/add-to-compare-button';
import { ImageGallery } from '@/components/listings/image-gallery';
import { InquiryModal } from '@/components/listings/inquiry-modal';
import { PriceHistoryChart } from '@/components/listings/price-history-chart';
import { SocialShare } from '@/components/listings/social-share';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { AiEstimateButton } from '@/components/valuation/ai-estimate-button';
import { formatPrice, formatPricePerM2 } from '@/lib/currency';
import type { ListingDetail, NeighborhoodScoreResult, PriceHistoryItem } from '@/lib/listings-api';
import { listingsApi } from '@/lib/listings-api';
import { PROPERTY_TYPES, DIRECTIONS, TRANSACTION_TYPES } from '@/lib/validations/listings';
const NeighborhoodRadarChart = dynamic(
() => import('@/components/neighborhood').then((m) => m.NeighborhoodRadarChart),
{ ssr: false },
);
const ListingMap = dynamic(
() => import('@/components/map/listing-map').then((mod) => mod.ListingMap),
{
ssr: false,
loading: () => (
{'\u0110ang t\u1ea3i b\u1ea3n \u0111\u1ed3...'}
),
},
);
function getLabel(list: readonly { value: string; label: string }[], value: string | null) {
if (!value) return null;
return list.find((item) => item.value === value)?.label ?? value;
}
interface ListingDetailClientProps {
listing: ListingDetail;
}
function mapScoreToCategories(result: NeighborhoodScoreResult) {
return [
{ category: 'education', label: 'Giáo dục', score: result.educationScore },
{ category: 'healthcare', label: 'Y tế', score: result.healthcareScore },
{ category: 'transport', label: 'Giao thông', score: result.transportScore },
{ category: 'shopping', label: 'Mua sắm', score: result.shoppingScore },
{ category: 'environment', label: 'Môi trường', score: result.greeneryScore },
{ category: 'safety', label: 'An ninh', score: result.safetyScore },
];
}
export function ListingDetailClient({ listing }: ListingDetailClientProps) {
const { property, seller, agent } = listing;
const transactionLabel = getLabel(TRANSACTION_TYPES, listing.transactionType);
const propertyTypeLabel = getLabel(PROPERTY_TYPES, property.propertyType);
const [inquiryOpen, setInquiryOpen] = React.useState(false);
const [neighborhoodScore, setNeighborhoodScore] = React.useState(null);
const [priceHistory, setPriceHistory] = React.useState([]);
React.useEffect(() => {
if (!property.district || !property.city) return;
listingsApi
.getNeighborhoodScore(property.district, property.city)
.then(setNeighborhoodScore)
.catch(() => {/* silently ignore — section simply won't render */});
}, [property.district, property.city]);
React.useEffect(() => {
listingsApi
.getPriceHistory(listing.id)
.then(setPriceHistory)
.catch(() => {/* silently ignore */});
}, [listing.id]);
return (
{/* Breadcrumb */}
Trang ch\u1ee7
/
T\u00ecm ki\u1ebfm
/
{property.title}
{/* Header */}
{transactionLabel && (
{transactionLabel}
)}
{propertyTypeLabel && {propertyTypeLabel} }
{property.title}
{property.address}, {property.ward}, {property.district}, {property.city}
{formatPrice(listing.priceVND)} VND
{listing.pricePerM2 != null && (
~{formatPricePerM2(listing.pricePerM2)}
)}
{listing.rentPriceMonthly && (
Thu\u00ea: {formatPrice(listing.rentPriceMonthly)}/th\u00e1ng
)}
{/* Image gallery */}
{/* Quick specs bar */}
{property.bedrooms != null && (
)}
{property.bathrooms != null && (
)}
{property.floors != null && (
)}
{property.direction && (
)}
{/* Main content */}
{/* Description */}
Mô tả
{property.description}
{/* Details */}
Thông tin chi tiết
{/* Amenities */}
{property.amenities && property.amenities.length > 0 && (
Tiện ích
{property.amenities.map((a) => (
{a}
))}
)}
{/* Map */}
Vị trí trên bản đồ
{/* Price History Chart */}
{priceHistory.length > 0 && (
Lịch sử giá
)}
{/* Neighborhood Score Radar Chart */}
Đánh giá khu vực
{neighborhoodScore ? (
<>
7
? 'success'
: neighborhoodScore.totalScore >= 5
? 'warning'
: 'destructive'
}
className="px-3 py-1 text-lg font-bold"
>
{neighborhoodScore.totalScore.toFixed(1)}/10
Điểm tổng khu vực
>
) : (
Chưa có dữ liệu đánh giá khu vực này
)}
{/* Sidebar */}
{/* Contact card */}
Liên hệ
{seller.fullName}
{seller.phone}
Gọi ngay
setInquiryOpen(true)}>
Nhắn tin
{agent && (
Môi giới
{agent.agency &&
{agent.agency}
}
{listing.commissionPct != null && (
Hoa hồng: {listing.commissionPct}%
)}
)}
{/* Social sharing + QR code */}
{/* AI Estimate */}
{/* Stats */}
{listing.viewCount}
Lượt xem
{listing.saveCount}
Lượt lưu
{listing.inquiryCount}
Liên hệ
{listing.publishedAt && (
Đăng ngày {new Date(listing.publishedAt).toLocaleDateString('vi-VN')}
)}
);
}
function QuickStat({ icon, label, value }: { icon: string; label: string; value: string }) {
const icons: Record = {
area: (
),
bed: (
),
bath: (
),
floors: (
),
compass: (
),
};
return (
);
}
function InfoItem({ label, value }: { label: string; value: string }) {
return (
);
}