'use client' import CartSummary from '@/components/CartSummary' import Layout from '@/hoc/Layout' import { NextPage } from 'next' import TitleBack from '../components/TitleBack' import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" import DiscountCard from '../components/DiscountCard' import { usePaymentMethods, useGetCart, useShippingCost } from '../hooks/useCartData' import { useState } from 'react' import { toast } from '@/components/Toast' import { useGetProfile } from '@/app/profile/hooks/useProfileData' const CartPayment: NextPage = () => { const { data: paymentMethodsData, isLoading: paymentMethodsLoading } = usePaymentMethods() const { data: cartData, isLoading: cartLoading } = useGetCart() const { data: shippingData, isLoading: shippingLoading } = useShippingCost() const { data: profileData, isLoading: profileLoading } = useGetProfile() const [selectedPaymentMethod, setSelectedPaymentMethod] = useState('') const [discountCode, setDiscountCode] = useState('') const [isApplyingDiscount, setIsApplyingDiscount] = useState(false) const isLoading = paymentMethodsLoading || cartLoading || shippingLoading || profileLoading if (isLoading) { return (
در حال بارگذاری...
) } const cart = cartData?.results?.cart const paymentMethods = paymentMethodsData?.results?.paymentMethods || [] const profile = profileData?.results?.info if (!cart?.items?.length) { return (
سبد خرید شما خالی است
) } // محاسبه هزینه ارسال const totalShippingCost = shippingData?.results?.shipping?.reduce((total, shop) => { return total + (shop.shippers?.[0]?.totalShippingCost || 0) }, 0) || 0 // محاسبه قیمت نهایی const finalTotal = (cart?.payable_price || 0) + totalShippingCost const handlePaymentMethodChange = (value: string) => { setSelectedPaymentMethod(value) } const handleDiscountCodeSubmit = async () => { if (!discountCode.trim()) { toast('لطفاً کد تخفیف را وارد کنید', 'error') return } setIsApplyingDiscount(true) try { // TODO: پیاده‌سازی API اعمال کد تخفیف // await applyDiscountCode(discountCode) toast('کد تخفیف با موفقیت اعمال شد', 'success') } catch { toast('کد تخفیف نامعتبر است', 'error') } finally { setIsApplyingDiscount(false) } } const handlePayment = async () => { if (!selectedPaymentMethod) { toast('لطفاً روش پرداخت را انتخاب کنید', 'error') return } if (!profile?.userAddress) { toast('لطفاً ابتدا آدرس خود را تکمیل کنید', 'error') return } try { // TODO: پیاده‌سازی API پرداخت نهایی // await processPayment({ // paymentMethodId: selectedPaymentMethod, // discountCode: discountCode || null, // shippingAddress: profile.userAddress // }) toast('پرداخت با موفقیت انجام شد', 'success') } catch { toast('خطا در پردازش پرداخت', 'error') } } return (
انتخاب روش پرداخت
{paymentMethods.map((method) => (
))}
) } export default function CartPaymentWithLayout() { return ( ) }