"use client" import { useState } from "react" import { useRouter } from "next/navigation" import { useCart } from "@/contexts/cart-context" import { Button } from "@/components/ui/button" import { Label } from "@/components/ui/label" import { Textarea } from "@/components/ui/textarea" import { CheckCircle2 } from "lucide-react" // Mock function for YooMoney payment const processYooMoneyPayment = async (amount: number): Promise => { // Simulate API call await new Promise((resolve) => setTimeout(resolve, 2000)) // Simulate successful payment (in real app, this would be the actual API response) return true } export function CheckoutForm() { const [step, setStep] = useState(1) const [address, setAddress] = useState("") const [isProcessing, setIsProcessing] = useState(false) const [isOrderPlaced, setIsOrderPlaced] = useState(false) const { items, clearCart } = useCart() const router = useRouter() // Define the getTotalPrice function inside the component const getTotalPrice = () => { return items.reduce((total, item) => total + item.price * item.quantity, 0) } const handleAddressSubmit = (e: React.FormEvent) => { e.preventDefault() if (address.trim()) { setStep(2) } } const handlePayment = async () => { setIsProcessing(true) const totalPrice = getTotalPrice() // Calculate total price here const paymentSuccess = await processYooMoneyPayment(totalPrice) if (paymentSuccess) { setIsOrderPlaced(true) clearCart() } else { alert("Оплата не прошла. Пожалуйста, попробуйте еще раз.") } setIsProcessing(false) } if (isOrderPlaced) { return (

Заказ успешно оформлен!

Спасибо за покупку. Ваш заказ будет доставлен по указанному адресу.

) } return (
{step === 1 && (

Шаг 1: Адрес доставки