import React, { useEffect, useState } from 'react' import Image from '@/core/components/elements/Image/Image' import BottomPopup from '@/core/components/elements/Popup/BottomPopup' import CountDown2 from '@/core/components/elements/CountDown/CountDown2' import currencyFormat from '@/core/utils/currencyFormat' import { getPromotionProgram } from '../api/homepageApi' const PromotionType = ({ isModal = false, variantId, setPromotionActiveId, promotionActiveId, quantity, product = null, setProducts = null }) => { const [selectedPromo, setSelectedPromo] = useState(null) const [promotionType, setPromotionType] = useState(false) const [promos, setPromotionList] = useState(null) const [activeTitle, setActiveTitle] = useState(null) const [quantitySet, setQuantity] = useState(null) useEffect(() => { const id = variantId const listProgram = async () => { const programs = await getPromotionProgram({ id }) if (programs?.length > 0) { setPromotionList(programs) setActiveTitle(programs?.[0].type.value) } } listProgram() setSelectedPromo(promotionActiveId) if (product) { const variant = product.variants.find((variant) => variant.id === variantId) setQuantity(variant.quantity) }else{ setQuantity(quantity) } }, []) const groupingData = promos?.reduce((groups, item) => { const promoType = item.type.value if (!groups[promoType]) { groups[promoType] = [] } groups[promoType].push(item) return groups }, {}) const handlePromoClick = (promoId, minQty) => { if (quantitySet >= minQty) { if (promoId == selectedPromo) { setSelectedPromo(null) setPromotionActiveId(null) if (product) { const updateProdcuts = () => { let variantIndex = product.variants.findIndex((varian) => varian.id == variantId) product.variants[variantIndex].programActive = null setProducts(product) } updateProdcuts() } } else { setSelectedPromo(promoId) setPromotionActiveId(promoId) if (product) { const updateProdcuts = () => { let variantIndex = product.variants.findIndex((varian) => varian.id == variantId) product.variants[variantIndex].programActive = promoId setProducts(product) } updateProdcuts() } } } } const handlePopUp = () => { if (isModal == false) { setPromotionType(true) } } return ( promos && ( <>
Promo Tersedia
{isModal === true ? (
{Object.keys(groupingData).map((index) => { return ( <> ) })}
{activeTitle && groupingData[activeTitle].map((item, i) => (
handlePromoClick(item.id, item.minimumPurchaseQty)} className={`border border-solid mb-5 w-full hover:cursor-pointer ${ selectedPromo ? selectedPromo === item.id ? 'opacity-100 border-red-500 bg-red-100' : 'opacity-50 pointer-events-none' : 'opacity-100' } ${ quantitySet >= item.minimumPurchaseQty ? '' : 'opacity-50 pointer-events-none' } `} >
{item.name}
Waktu Tersisa

{item.name}

{/* {item.type.value === 'bundling' && ( <> */}
{currencyFormat(item.totalSavings)}
Gratis
{quantitySet < item.minimumPurchaseQty ? 'Tambah ' + (parseInt(item.minimumPurchaseQty) - parseInt(quantitySet)) + ' lagi' : ''}
{/* )} */} {/* {item.type.value === 'special_price' && ( <>
{currencyFormat(item.totalSavings)}
)} {item.type.value === 'discount_loading' && ( <>
{currencyFormat(item.totalSavings)}
{quantitySet < item.minimumPurchaseQty ? 'Tambah ' + (parseInt(item.minimumPurchaseQty) - parseInt(quantitySet)) + ' lagi' : ''}
)} */}
))}
) : ( promos.map((promo, index) => { if (index > 2) { return null } else { if (index === 2 && promos.length > 2) { return ( <>
setPromotionType(true)} className={` border border-solid bg-white mb-5 w-full hover:cursor-pointer opacity-100 flex flex-col justify-center items-center`} >
Lihat Promo Lainya
) } return ( <>
setPromotionType(true)} className={`border border-solid bg-white mb-5 w-full hover:cursor-pointer`} >
{promo.name}
{promo.type.label}

{promo.name}

{/* {currencyFormat(promo.totalSavings)} */}
{/*
*/}
) } }) )} setPromotionType(false)} >
) ) } export default PromotionType