import Image from "next/image";
import Link from 'next/link';
import { createSlug } from '@/core/utils/slug';
import useDevice from '@/core/hooks/useDevice';
import { Swiper, SwiperSlide } from 'swiper/react';
import 'swiper/css';
import { useQuery } from 'react-query';
import { useRouter } from 'next/router';
import {
    ChevronDownIcon,
    ChevronUpIcon, // Import ChevronUpIcon for toggling
    DocumentCheckIcon,
    HeartIcon,
} from '@heroicons/react/24/outline';
import { useState } from 'react'; // Import useState
import { getIdFromSlug } from '@/core/utils/slug'

const LobSectionCategory = ({ categories }) => {
    const { isDesktop, isMobile } = useDevice();
    const [isOpenCategory, setIsOpenCategory] = useState(false); // State to manage category visibility

    const handleToggleCategories = () => {
        setIsOpenCategory(!isOpenCategory);
    };

    const displayedCategories = categories[0]?.categoryIds;
    return (
        <section>
            {isDesktop && (
                <div className="group/item grid grid-flow-col gap-y-2 gap-x-4 w-full h-full">
                    {displayedCategories?.map((category) => (
                        <Link
                        href={createSlug('/shop/category/', category?.name, category?.id)}
                        key={category?.id}
                        passHref
                        className="block hover:scale-105 transition-transform duration-300 bg-cover bg-center h-[144px]"
                        style={{
                          backgroundImage: `url('${category?.image ? category?.image : 'https://erp.indoteknik.com/web/image?model=x_banner.banner&id=5&field=x_banner_image&unique=09202023100557'}')`,
                        }}
                      >
                      </Link>
                    ))}
                </div>
            )}

            {isMobile && (
                <div className="py-4">
                    <Swiper slidesPerView={1.2} spaceBetween={10}>
                        {displayedCategories?.map((category) => (
                            <SwiperSlide key={category?.id}>
                                <Link
                                    href={createSlug('/shop/category/', category?.name, category?.id)}
                                    key={category?.id}
                                    passHref
                                    className="block  bg-cover bg-center h-[144px]"
                                    style={{
                                    backgroundImage: `url('${category?.image ? category?.image : 'https://erp.indoteknik.com/web/image?model=x_banner.banner&id=5&field=x_banner_image&unique=09202023100557'}')`,
                                    }}
                                >
                                </Link>
                            </SwiperSlide>
                        ))}
                    </Swiper>
                    {categories.length > 10 && (
                        <div className="w-full flex justify-end mt-4">
                            <button
                                onClick={handleToggleCategories}
                                className="flex justify-end mt-4 bg-red-500 text-white text-sm px-4 py-2 rounded"
                            >
                                {isOpenCategory ? 'Sembunyikan Semua' : 'Lihat Semua'}
                            </button>
                        </div>
                    )}
                </div>
            )}
        </section>
    )
}

export default LobSectionCategory
