import style from '../styles/item.module.css'
import odooApi from '~/libs/odooApi';
import { useEffect, useState } from 'react';
import { Skeleton, SkeletonProps, Tooltip } from '@chakra-ui/react'
import { InfoIcon } from 'lucide-react'
import Image from 'next/image'
import Link from 'next/link'
import { PROMO_CATEGORY } from '~/constants/promotion'
import formatCurrency from '~/libs/formatCurrency'
import { createSlug } from '~/libs/slug'
import { CartItem as CartItemProps } from '~/types/cart'
import CartItemAction from './ItemAction'
import CartItemPromo from './ItemPromo'
import CartItemSelect from './ItemSelect'
type Props = {
item: CartItemProps
editable?: boolean
selfPicking?: boolean
pilihSemuaCart?: boolean
}
const CartItem = ({ item, editable = true, selfPicking}: Props) => {
const [qtyPickUp, setQtyPickUp] = useState(0);
useEffect(() => {
const fetchData = async () => {
const qty_available = await odooApi(
'GET',
`/api/v1/product_variant/${item.id}/qty_available`
);
setQtyPickUp(qty_available?.qty);
};
fetchData();
}, [item]);
return (
{item.cart_type === 'promotion' && (
{item.promotion_type?.value && (
Paket {PROMO_CATEGORY[item.promotion_type?.value].alias}
)}
Selamat! Pembelian anda lebih hemat{' '}
Rp
{formatCurrency(
(item.package_price || 0) * item.quantity - item.subtotal
)}
)}
{editable &&
}
{qtyPickUp > 0 && (
{item.quantity <= qtyPickUp
? '*Barang ini bisa di pickup maksimal pukul 16.00'
: `*${qtyPickUp} Barang ini bisa di pickup maksimal pukul 16.00`}
)}
{item.cart_type === 'promotion' && (
Rp {formatCurrency((item.package_price || 0))}
Rp {formatCurrency(item.price.price)}
)}
{item.cart_type === 'product' && (
{item.price.discount_percentage > 0 && (
Rp {formatCurrency((item.price.price || 0))}
)}
{item.price.price_discount > 0 && `Rp ${formatCurrency(item.price.price_discount)}`}
{item.price.price_discount === 0 && '-'}
)}
{item.cart_type === 'product' && item.code}
{Math.round(item.weight * 10) / 10} Kg
{editable &&
}
{!editable &&
{item.quantity}
}
{item.products?.map((product) => )}
{item.free_products?.map((product) => )}
)
}
CartItem.Image = function CartItemImage({ item }: { item: CartItemProps }) {
const image = item?.image || item?.parent?.image
const imageProgram = item?.image_program ? item.image_program[0] : item?.parent?.image;
return (
<>
{item.cart_type === 'promotion' && (
{imageProgram &&
}
{!imageProgram &&
No Image
}
)}
{item.cart_type === 'product' && (
{image && }
{!image && No Image
}
)}
>
)
}
CartItem.Name = function CartItemName({ item }: { item: CartItemProps }) {
return (
<>
{item.cart_type === 'promotion' && (
{item.name}
)}
{item.cart_type === 'product' && (
{item.name}
)}
>
)
}
CartItem.Skeleton = function CartItemSkeleton(props: SkeletonProps & { count: number }) {
return Array.from({ length: props.count }).map((_, index) => (
))
}
export default CartItem