'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, useApplyDiscountCode, useCheckoutCart } 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 applyDiscountMutation = useApplyDiscountCode() const checkoutMutation = useCheckoutCart() const [selectedPaymentMethod, setSelectedPaymentMethod] = useState('') const [discountCode, setDiscountCode] = useState('') 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 (
سبد خرید شما خالی است
) } // بارگذاری انتخاب‌های ارسال از localStorage const savedShipments = typeof window !== 'undefined' ? localStorage.getItem('selectedShipments') : null const selectedShipments = savedShipments ? JSON.parse(savedShipments) : {} // محاسبه هزینه ارسال بر اساس انتخاب‌های کاربر const totalShippingCost = shippingData?.results?.shipping?.reduce((total, shop) => { const selectedShipperId = selectedShipments[shop.shopId] const selectedShipper = shop.shippers?.find(shipper => shipper.shipperId === selectedShipperId) return total + (selectedShipper?.totalShippingCost || 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 } try { await applyDiscountMutation.mutateAsync({ discount_code: discountCode }) toast('کد تخفیف با موفقیت اعمال شد', 'success') setDiscountCode('') } catch (error: unknown) { const errorMessage = (error as { response?: { data?: { message?: string } } })?.response?.data?.message || 'کد تخفیف نامعتبر است' toast(errorMessage, 'error') } } const handlePayment = async () => { if (!selectedPaymentMethod) { toast('لطفاً روش پرداخت را انتخاب کنید', 'error') return } if (!profile?.userAddress) { toast('لطفاً ابتدا آدرس خود را تکمیل کنید', 'error') return } const response = await checkoutMutation.mutateAsync({ payment_method_id: selectedPaymentMethod }) if (response?.results?.paymentData?.redirectUrl) { window.location.href = response.results.paymentData.redirectUrl } else { toast('خطا در دریافت لینک پرداخت', 'error') } } return (
انتخاب روش پرداخت
{paymentMethods.map((method) => (
))}
) } export default function CartPaymentWithLayout() { return ( ) }