import clsx from 'clsx';
import ImageNext from 'next/image';
import { useRouter } from 'next/router';
import { useMemo, useEffect, useState } from 'react';
import Image from '@/core/components/elements/Image/Image';
import Link from '@/core/components/elements/Link/Link';
import currencyFormat from '@/core/utils/currencyFormat';
import { sellingProductFormat } from '@/core/utils/formatValue';
import { createSlug } from '@/core/utils/slug';
import whatsappUrl from '@/core/utils/whatsappUrl';
import useUtmSource from '~/hooks/useUtmSource';
import useDevice from '@/core/hooks/useDevice';
const ProductCard = ({ product, simpleTitle, variant = 'vertical' }) => {
const router = useRouter();
const utmSource = useUtmSource();
const [discount, setDiscount] = useState(0);
const { isDesktop, isMobile } = useDevice();
let voucherPastiHemat = 0;
voucherPastiHemat = product?.newVoucherPastiHemat[0]
? product?.newVoucherPastiHemat[0]
: product?.newVoucherPastiHemat;
const callForPriceWhatsapp = whatsappUrl('product', {
name: product.name,
manufacture: product.manufacture?.name,
url: createSlug('/shop/product/', product.name, product.id, true),
});
const image = useMemo(() => {
if (!isDesktop && product.image_mobile) {
return product.image_mobile + '?ratio=square';
} else {
if (product.image) return product.image + '?ratio=square';
return '/images/noimage.jpeg';
}
}, [product.image, product.image_mobile]);
const URL = {
product:
createSlug('/shop/product/', product?.name, product?.id) +
`?utm_source=${utmSource}`,
manufacture: createSlug(
'/shop/brands/',
product?.manufacture?.name,
product?.manufacture.id
),
};
const hitungDiscountVoucher = () => {
let countDiscount = 0;
if (voucherPastiHemat.discountType === 'percentage') {
countDiscount =
product?.lowestPrice.priceDiscount *
(voucherPastiHemat.discountAmount / 100);
if (
voucherPastiHemat.maxDiscount > 0 &&
countDiscount > voucherPastiHemat.maxDiscount
) {
countDiscount = voucherPastiHemat.maxDiscount;
}
} else {
countDiscount = voucherPastiHemat.discountAmount;
}
setDiscount(countDiscount);
};
useEffect(() => {
hitungDiscountVoucher();
}, []);
if (variant == 'vertical') {
return (
{product?.isSni && (
)}
{product?.isTkdn && (
)}
{router.pathname != '/' && product?.flashSale?.id > 0 && (
{Math.floor(product?.lowestPrice.discountPercentage)}%
{product?.flashSale?.tag != 'false' ||
product?.flashSale?.tag
? product?.flashSale?.tag
: 'FLASH SALE'}
)}
{product.variantTotal > 1 && (
{product.variantTotal} Varian
)}
{product?.manufacture?.name ? (
{product.manufacture.name}
) : (
-
)}
{product?.isInBu && (
)}
{product?.name}
{product?.flashSale?.id > 0 &&
product?.lowestPrice.discountPercentage > 0 ? (
<>
{currencyFormat(product.lowestPrice.price)}
{Math.floor(product?.lowestPrice.discountPercentage)}%
{product?.lowestPrice.priceDiscount > 0 ? (
currencyFormat(product?.lowestPrice.priceDiscount)
) : (
Call for Inquiry
)}
>
) : (
{product?.lowestPrice.price > 0 ? (
<>
{currencyFormat(product?.lowestPrice.price)}
Inc. PPN:{' '}
{currencyFormat(
product.lowestPrice.price * process.env.NEXT_PUBLIC_PPN
)}
>
) : (
Call for Inquiry
)}
)}
{discount > 0 && product?.flashSale?.id < 1 && (
Voucher : {currencyFormat(discount)}
)}
{product?.stockTotal > 0 && (
Ready Stock
)}
{/*
{product?.stockTotal > 5 ? '> 5' : '< 5'}
*/}
{product?.qtySold > 0 && (
{sellingProductFormat(product?.qtySold) + ' Terjual'}
)}
);
}
if (variant == 'horizontal') {
return (
{product?.isSni && (
)}
{product?.isTkdn && (
)}
{product.variantTotal > 1 && (
{product.variantTotal} Varian
)}
{product.flashSale.id > 0 && (
{' '}
{product?.flashSale?.tag != 'false' || product?.flashSale?.tag
? product?.flashSale?.tag
: 'FLASH SALE'}
)}
{product?.manufacture?.name ? (
{product.manufacture.name}
{/* {product?.is_in_bu && (
Click & Pickup
)} */}
) : (
-
)}
{product?.name}
{product?.flashSale?.id > 0 &&
product?.lowestPrice?.discountPercentage > 0 ? (
<>
{product?.lowestPrice.discountPercentage > 0 && (
{Math.floor(product?.lowestPrice?.discountPercentage)}%
{currencyFormat(product?.lowestPrice?.price)}
)}
{product?.lowestPrice?.priceDiscount > 0 ? (
currencyFormat(product?.lowestPrice?.priceDiscount)
) : (
Call for Inquiry
)}
>
) : (
{product?.lowestPrice.price > 0 ? (
<>
{currencyFormat(product?.lowestPrice.price)}
Inc. PPN:{' '}
{currencyFormat(
product.lowestPrice.price * process.env.NEXT_PUBLIC_PPN
)}
>
) : (
Call for Inquiry
)}
)}
{discount > 0 && product?.flashSale?.id < 1 && (
Voucher : {currencyFormat(discount)}
)}
{product?.stockTotal > 0 && (
Ready Stock
)}
{/*
{product?.stockTotal > 5 ? '> 5' : '< 5'}
*/}
{product?.qtySold > 0 && (
{sellingProductFormat(product?.qtySold) + ' Terjual'}
)}
);
}
};
export default ProductCard;