"use client"; import { useState, useEffect } from "react"; import { X } from "lucide-react"; import { fetchApi } from "../utils/fetch"; export default function ExchangeForm({ currentUser, setCurrentUser, item, onClose, setAlert, }) { const [formData, setFormData] = useState({}); const [isSubmitting, setIsSubmitting] = useState(false); useEffect(() => { if (currentUser && currentUser.account) { setFormData((prevData) => ({ ...prevData, account: currentUser.account, })); } }, []); const handleInputChange = (e) => { setFormData({ ...formData, [e.target.name]: e.target.value }); }; const handleSubmit = async (e) => { e.preventDefault(); setIsSubmitting(true); try { const params = { userId: currentUser.id, username: currentUser.username, item, status: "待兑换未审核", exchangeInfo: { account: formData.account || "", address: formData.address || "", name: formData.name || "", phone: formData.phone || "", }, exchangeTime: new Date(), // 添加兑换时间 // exchangeCount: formData.exchangeCount || 1, }; // console.log("params", params); const res = await fetchApi("/api/exchange-history", { method: "POST", body: JSON.stringify(params), }); if (res.success) { setAlert({ type: "success", message: "提交成功,正在审核中", }); onClose(); const updatedUser = { ...currentUser, points: currentUser.points - item.points, account: formData.account || currentUser.account, }; localStorage.setItem("currentUser", JSON.stringify(updatedUser)); setCurrentUser(updatedUser); } else { setAlert({ type: "error", message: res.error }); } } catch (error) { console.error("兑换请求失败:", error); setAlert({ type: "error", message: res.error }); } finally { setIsSubmitting(false); } }; return (