"use client"; import { useState } from "react"; import { api } from "@/lib/api"; import type { CostResult, Destination } from "@/lib/types"; interface Props { destinations: Destination[] } export default function Calculator({ destinations }: Props) { const [slug, setSlug] = useState(destinations[0]?.slug || ""); const [housing, setHousing] = useState("mid"); const [months, setMonths] = useState(3); const [result, setResult] = useState(null); const [loading, setLoading] = useState(false); const calculate = async () => { setLoading(true); try { const data = await api.calculateCost({ destination_slug: slug, housing, months }); setResult(data); } catch { setResult(null); } finally { setLoading(false); } }; return (
🧮 CALCULATOR

旅居费用计算器

根据目的地和生活方式,精准估算你的旅居预算

{[{ v: "budget", l: "💰 经济" }, { v: "mid", l: "🏠 舒适" }, { v: "premium", l: "✨ 豪华" }].map((o) => ( ))}
setMonths(+e.target.value)} />
{result ? (
{result.emoji}

{result.destination}

{result.months} 个月旅居预算

{Object.entries(result.breakdown).map(([k, v]) => (
{k}
¥{v.toLocaleString()}
))}
月均¥{result.per_month.toLocaleString()}
总计¥{result.total.toLocaleString()}
) : (
🧮

选择目的地和参数,点击计算查看预算明细

)}
); }