"use client"; import React, { useState, useEffect } from "react"; import { motion } from "motion/react"; import { ChevronRight, ChevronLeft, MapPin, CreditCard, Truck, ShieldCheck, Calendar, Wallet, Clock, ArrowRight, CheckCircle2, Heart, PlusCircle } from "lucide-react"; import { toPersian, cn } from "../lib/utils"; import { useCartStore } from "../lib/store/cartStore"; import { usePetStore } from "../lib/store/usePetStore"; import { useUserStore } from "../lib/store/userStore"; import { Product } from "../lib/data/products"; import { productService } from "../lib/services/productService"; import { toast } from "sonner"; import { useRouter } from 'next/navigation'; export default function CheckoutPage() { const router = useRouter(); const { items, getTotal, getSubtotal, getDiscount, isSubscribed, charityDonation, setCharityDonation, addOrder, clearCart } = useCartStore(); const { getActivePet, updatePet } = usePetStore(); const { profile, updateProfile } = useUserStore(); const [step, setStep] = useState(1); const [loading, setLoading] = useState(false); const [useRoundUp, setUseRoundUp] = useState(false); const [dbProducts, setDbProducts] = useState([]); const [paymentMethod, setPaymentMethod] = useState('online'); useEffect(() => { productService.getProducts({ limit: 999 }).then(res => setDbProducts(res.data)); }, []); const subtotal = getSubtotal(); const roundedAmount = Math.ceil(subtotal / 10000) * 10000; const roundUpDiff = roundedAmount - subtotal; const handleCharityToggle = () => { if (!useRoundUp) { setCharityDonation(Math.max(roundUpDiff, Math.round(subtotal * 0.01))); setUseRoundUp(true); } else { setCharityDonation(0); setUseRoundUp(false); } }; const handleFinalize = async () => { setLoading(true); const activePet = getActivePet(); try { // 1. Register the order in the backend const orderId = await addOrder({ items: [...items], total: getTotal(), charityDonation: charityDonation, petId: activePet?.id }); // 2. Update user charity total if (charityDonation > 0) { updateProfile({ charityDonationTotal: (profile.charityDonationTotal || 0) + charityDonation }); } // 3. Update pet consumptions for the refill logic if (activePet) { const newConsumptions = [...(activePet.consumptions || [])]; items.forEach(item => { const existing = newConsumptions.find(c => c.productId === item.product.id); if (existing) { existing.remaining += item.product.packageSize * item.quantity; existing.packageSize = item.product.packageSize; } else { newConsumptions.push({ productId: item.product.id, packageSize: item.product.packageSize, remaining: item.product.packageSize * item.quantity }); } }); updatePet(activePet.id, { consumptions: newConsumptions }); } clearCart(); toast.success("سفارش شما با موفقیت ثبت شد و به پرونده سلامت همدمتان اضافه شد!"); router.push('/dashboard'); } catch (err: any) { toast.error(err.message || "خطا در ثبت سفارش. لطفاً مجدداً تلاش کنید."); } finally { setLoading(false); } }; if (items.length === 0) { return (

سبد خرید شما خالی است

); } return (
{/* Mobile Navigation & Breadcrumbs */}
router.push('/shop')}>سبد خرید نهایی‌سازی سفارش
{/* Desktop Breadcrumbs (Hidden on Mobile) */}
router.push('/')}>خانه درگاه پرداخت امن و نهایی‌سازی
{/* Main Form */}
{/* Step 1: Shipping */}
۱

اطلاعات ارسال