"use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; interface House { id: string; title: string; description: string; price: number; district: string; address: string; phone: string; images: string[]; status: string; reject_reason?: string; reviewed_at?: string; createdAt: string; } export default function OwnerDashboard() { const router = useRouter(); const [houses, setHouses] = useState([]); const [districts, setDistricts] = useState([]); const [loading, setLoading] = useState(true); const [showForm, setShowForm] = useState(false); const [editingHouse, setEditingHouse] = useState(null); const [formData, setFormData] = useState({ title: "", description: "", price: "", district: "", address: "", phone: "", }); const [images, setImages] = useState([]); const [uploading, setUploading] = useState(false); const [error, setError] = useState(""); const [submitting, setSubmitting] = useState(false); useEffect(() => { checkAuth(); }, []); async function fetchDistricts() { try { const res = await fetch("/api/districts"); const data = await res.json(); setDistricts(data.districts || []); if (data.districts?.length > 0) { setFormData(prev => ({ ...prev, district: data.districts[0] })); } } catch (error) { console.error("获取地区失败", error); } } async function checkAuth() { try { const res = await fetch("/api/auth/me"); const data = await res.json(); if (!data.user) { router.push("/owner"); return; } fetchHouses(); } catch (error) { router.push("/owner"); } } async function fetchHouses() { try { const res = await fetch("/api/owner/houses"); const data = await res.json(); setHouses(data.houses || []); } catch (error) { console.error("获取房源失败", error); } finally { setLoading(false); } } async function handleLogout() { await fetch("/api/auth/me", { method: "DELETE" }); router.push("/owner"); } function openForm(house?: House) { fetchDistricts(); if (house) { setEditingHouse(house); setFormData({ title: house.title, description: house.description, price: String(house.price), district: house.district, address: house.address, phone: house.phone, }); setImages(house.images); } else { setEditingHouse(null); setFormData({ title: "", description: "", price: "", district: "", address: "", phone: "", }); setImages([]); } setError(""); setShowForm(true); } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(""); setSubmitting(true); try { const body = { title: formData.title, description: formData.description, price: Number(formData.price), district: formData.district, address: formData.address, phone: formData.phone, images, }; let res; if (editingHouse) { res = await fetch(`/api/houses/${editingHouse.id}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); } else { res = await fetch("/api/houses", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); } const data = await res.json(); if (!res.ok) { setError(data.error || "操作失败"); return; } setShowForm(false); fetchHouses(); } catch (error) { setError("网络错误,请重试"); } finally { setSubmitting(false); } } async function handleDelete(id: string) { if (!confirm("确定要删除这个房源吗?")) return; try { const res = await fetch(`/api/houses/${id}`, { method: "DELETE" }); if (res.ok) { fetchHouses(); } } catch (error) { alert("删除失败"); } } async function handleImageUpload(e: React.ChangeEvent) { const files = e.target.files; if (!files || files.length === 0) return; setUploading(true); const newImages: string[] = []; for (let i = 0; i < files.length; i++) { const file = files[i]; const formData = new FormData(); formData.append('file', file); try { const res = await fetch('/api/upload', { method: 'POST', body: formData, }); const data = await res.json(); if (data.url) { newImages.push(data.url); } } catch (error) { console.error('上传图片失败', error); } } setImages(prev => [...prev, ...newImages]); setUploading(false); e.target.value = ''; } function removeImage(index: number) { setImages(prev => prev.filter((_, i) => i !== index)); } return (

房源管理

{loading ? (
{[1, 2].map((i) => (
))}
) : houses.length === 0 ? (
🏠

您还没有发布房源

点击上方按钮发布您的第一套房源

) : (
{houses.map((house) => (
{house.images && house.images.length > 0 ? ( {house.title} ) : (
🏠
)}

{house.title}

{house.address}

¥{house.price}/月 {house.district}
{house.status === 'pending' && ( 审核中 )} {house.status === 'approved' && ( 审核通过 )} {house.status === 'rejected' && ( 审核驳回 )}
{house.status === 'rejected' && house.reject_reason && (

驳回原因:{house.reject_reason}

)}
))}
)}
{showForm && (

{editingHouse ? "编辑房源" : "发布新房源"}

{error && (
{error}
)}
setFormData({ ...formData, title: e.target.value })} placeholder="如:南山区科技园精装单间" className="w-full px-3 py-2 rounded-lg border border-gray-200 focus:border-orange-500 outline-none" required />
setFormData({ ...formData, price: e.target.value })} placeholder="1500" className="w-full px-3 py-2 rounded-lg border border-gray-200 focus:border-orange-500 outline-none" required />
setFormData({ ...formData, address: e.target.value })} placeholder="如:南山区科技园南区A栋" className="w-full px-3 py-2 rounded-lg border border-gray-200 focus:border-orange-500 outline-none" required />
setFormData({ ...formData, phone: e.target.value })} placeholder="13800138000" className="w-full px-3 py-2 rounded-lg border border-gray-200 focus:border-orange-500 outline-none" required />