import React, { useEffect, useState } from 'react';
import {fetchProductManagementSolr} from '../hooks/useCategoryManagement';
import NextImage from 'next/image';
import Link from "next/link"
import router from 'next/router';
import { createSlug } from '@/core/utils/slug'

const CategoryDynamic =    () => {
  const [promoItems, setPromoItems] = useState([]);
  useEffect(() => {
    const loadPromo = async () => {
      
      try {
        const items = await fetchProductManagementSolr();
        setPromoItems(items);

      } catch (loadError) {
        // console.error("Error loading promo items:", loadError)
      }
    }
      loadPromo()

  },[]);

  console.log("promoItems",promoItems)

  return (
    <div>
      {/* <div className='font-semibold sm:text-h-lg mb-2 px-4 sm:px-0'>Kategori Pilihan</div> */}
      {/* Render category data */}
      {promoItems && promoItems.map((category) => (
        <div key={category.id}>
          <div className='bagian-judul flex flex-row justify-start items-center gap-3 mb-4 mt-4'>
              <div className='font-semibold sm:text-h-lg mr-2'>{category.name}</div>
              <p className='text-gray_r-10 text-sm'>999 rb+ Produk tersedia</p>
            <Link href={createSlug('/shop/category/', category?.name, category?.category_id)} className="!text-red-500 font-semibold">Lihat Semua</Link>
          </div>
          <div className='grid grid-cols-3 gap-2'>
            {category.category_id2.map((index)=> (
              <div key={index.id} className='border justify-start items-start'>
                <div className='p-3'>
                    <div className='flex flex-row border mb-2 justify-start items-center'>
                      <NextImage
                        src={index.image? index.image : "https://erp.indoteknik.com/api/image/product.template/image_256/120726?ratio=square"}
                        alt={index.name}
                        width={90}
                        height={30}
                        className='object-fit'
                      />
                      <div className='bagian-judul flex flex-col justify-center items-start gap-2 ml-2'>
                          <div className='font-semibold text-lg mr-2'>{index.name}</div>
                          <p className='text-gray_r-10 text-sm'>999 rb+ Produk</p>
                          <Link href={createSlug('/shop/category/', index?.name, index?.id_level_2)} className="!text-red-500 font-semibold">Lihat Semua</Link>
                      </div>
                    </div>
                    <div className='grid grid-cols-2 gap-2 overflow-y-auto max-h-[240px]' >
                        {index.child_frontend_id_i.map((x)=> (
                          <div key={x.id}>
                                <Link href={createSlug('/shop/category/', x?.name, x?.id_level_3)} className="flex flex-row gap-2 border group hover:border-red-500">
                                    <NextImage
                                      src={x.image? x.image : "https://erp.indoteknik.com/api/image/product.template/image_256/127221?ratio=square"}
                                      alt={x.name}
                                      width={40}
                                      height={40}
                                    />
                                    <div className='bagian-judul flex flex-col justify-center items-center gap-2 break-words line-clamp-2 group-hover:text-red-500'>
                                        <div className='font-semibold line-clamp-2 group-hover:text-red-500 text-sm mr-2'>{x.name}</div>
                                    </div>
                                </Link>
                            </div>
                          ))}
                    </div>
                </div>
              </div>
            ))}
          </div>
        </div>
      ))}
    </div>
  );
}

export default CategoryDynamic;
