"use client"; import {useState} from "react"; import { useRouter } from 'next/navigation'; import {CartDetail} from "@/types/cart/type"; import { useCustomToast } from "@/utils/hooks/useToast"; import {useSaveCheckoutCart} from "@/utils/hooks/useSaveCheckoutCart"; import { formatCartDetail } from "@/utils/cartDetailTools"; import { overlayLoading } from "@/components/theme/ui/kernel/loading/api"; import { confirmDialog } from "@/components/theme/ui/kernel/confirm/api"; import CommonModal from "@/components/theme/ui/CommonModal"; import {useLoginModal} from "@/providers/LoginModalProvider"; // 问题: plus金额,折扣,时间是前端写死的。写死金额只显示美金数字,不随货币改变; const staticVipData = { price: '$19.90', linePrice: '$29.90', days: '180', discount: '5%' }; export default function CheckoutVipPlus({ cartData, onSwitchVip }:{ cartData: CartDetail onSwitchVip: (payload:CartDetail) => void; }) { const router = useRouter(); const {openLoginModal} = useLoginModal(); const { showToast } = useCustomToast(); const {saveCheckoutCart} = useSaveCheckoutCart(); const [isShow, setIsShow] = useState(false); const [vipChecked, setVipChecked] = useState(cartData.vipPlusAmount > 0); const toggleModal = (bool: boolean) => { setIsShow(bool); }; const clickSwitchButton = (value: boolean) => { if(!cartData.isGuest) { // 登录用户 const status = value; switchVipPlus(status); } else { // 游客 // 弹出登录弹窗 // 登录注册 // 登录成功 -> 合并购物车 // // 重新获取 数据 // guestUserClick(); } }; const switchVipPlus = async (checked:boolean) => { const paramVip = checked ? '1' : ''; // '' 表示取消应用 const res = await setVipPlus(paramVip); if(res) { setVipChecked(checked); } }; const setVipPlus = async (paramVip: string) => { overlayLoading.start(); const saveRes = await saveCheckoutCart({ // shippingMethod: '-1', // paymentMethod: '-1', // couponCode: '-1', // giftCardNumber: '-1', memberDiscount: paramVip }); overlayLoading.stop(); if(!saveRes.error) { if(saveRes.data) { const newCartDetail = formatCartDetail(saveRes.data); onSwitchVip(newCartDetail); } return true; } else { showToast(saveRes.msg, 'danger'); return false; } }; const guestUserClick = () => { openLoginModal({ onFinalResult: async (res) => { if(res.mergeCartSuccess) { // 购物车合并成功 if(!res.cartAfterMerge?.isVip && res.cartAfterMerge?.vipPlusAmount === 0) { await switchVipPlus(true); } } else { // 购物车合并失败 // 三种 情况: 登录成功; 注册成功; if(res.loginSuccess) { // 登录成功 需要刷新页面 showToast('You have logged in successfully. But something wrong','danger'); await confirmDialog({ title: "Warning", content: "You have logged in successfully. But something wrong. You must refresh the page.", noCancel: true, }); window.location.reload(); } if(res.registerSuccess && !res.loginSuccess) { // 注册成功,去登录 await confirmDialog({ title: "Warning", content: "You have registered successfully. But something wrong. You have to login.", noCancel: true, }); router.replace('/customer/login'); } } } }); }; // vipExpireDate: "2027-01-30T22:40:33-08:00" const getVipLeftDays = (vipExpireDate: string) => { const target = new Date(vipExpireDate); const now = new Date(); // 当前时间 // 计算相差的毫秒数 let diffMs = target.getTime() - now.getTime(); if(diffMs <= 0) { diffMs = 0; } // 转换为天数 1.1天 显示2天 const diffDays = Math.ceil(diffMs / (1000 * 60 * 60 * 24)); return diffDays; } return (<> {cartData.isVip &&
Asteriahair Plus
{getVipLeftDays(cartData.vipExpireDate)} days left
Plus Save -{cartData.formattedVipDiscountAmount}
} {!cartData.isVip &&
Asteriahair Plus
Pay {staticVipData.price} save extra {staticVipData.discount} off every order
{staticVipData.days} days validity
{staticVipData.price} {staticVipData.linePrice}
}
Asteriahair Plus

In order to provide customers with a better shopping experience, Asteriahair has specially launched Asteriahair PLUS membership (no refund after purchase). PLUS members can enjoy unlimited free shipping, 5% off on each order within 180 days, exclusive instant discounts, exclusive customer service, free returns and other membership privileges.

We hope to establish a long-term win-win relationship with you through PLUS. At the same time, we will continue to enrich the rights and interests of PLUS members and provide you with more complete services.

Thank you for choosing Asteriahair. We are dedicated to creating the ultimate shopping experience for you!(The final right of interpretation belongs to Asteriahair)

Note: PLUS membership can't be used for wholesale orders, clearance and flash sale products.

} /> ); }