"use client";
import { useState } from "react";
import { Swiper, SwiperSlide } from "swiper/react";
import type { Swiper as SwiperType } from "swiper/types";
import { Navigation } from "swiper/modules";
import "swiper/css";
import "swiper/css/navigation";
import Image from "next/image";

const MOCK_DATA = [
  [
    {
      name: "Hd Lace Wig",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
    },
    {
      name: "Ready To Go",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
    },
    {
      name: "Colored Wig",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
    },
    {
      name: "U&V Part Wig",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
    },
    {
      name: "New Arrival",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
    },
    {
      name: "360 Wig",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
    },
  ],

  [
    {
      name: "13x4 Lace Wig",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
    },
    {
      name: "Body Wave Wig",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
    },
    {
      name: "Deep Wave Wig",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
    },
    {
      name: "Straight Wig",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
    },
    {
      name: "Curly Wig",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
    },
    {
      name: "Bob Wig",
      img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
    },
  ],
];

export default function JinGangSwiper() {
  const [activeIndex, setActiveIndex] = useState(0);

  const [swiperRef, setSwiperRef] = useState<SwiperType | null>(null);

  const handleSlideChange = (swiper:any) => {
    setActiveIndex(swiper.activeIndex);
  };

  const handleIndicatorClick = (index:any) => {
    if (swiperRef) {
      swiperRef.slideTo(index);
      setActiveIndex(index);
    }
  };

  return (
    <div className="max-w-screen-md mx-auto p-4">
      <Swiper
        modules={[Navigation]}
        spaceBetween={10}
        slidesPerView={1}
         onSwiper={(swiper) => setSwiperRef(swiper)}
        onSlideChange={handleSlideChange}
        className="mb-6"
      >
        <SwiperSlide>
          <div className="grid grid-cols-3 gap-2">
            {MOCK_DATA[0].map((item, idx) => (
              <div key={`block1-${idx}`} className="relative w-full aspect-[3/4]">
                <Image
                  src={item.img}
                  alt={item.name}
                  fill
                  className="object-cover rounded-md"
                />
                {/* <div className="absolute bottom-2 left-2 text-white text-sm font-medium bg-black/50 px-2 py-1 rounded">
                  {item.name}
                </div> */}
              </div>
            ))}
          </div>
        </SwiperSlide>

        {/* 第二大块：6个分类卡片（2行3列） */}
        <SwiperSlide>
          <div className="grid grid-cols-3 gap-2">
            {MOCK_DATA[1].map((item, idx) => (
              <div key={`block2-${idx}`} className="relative w-full aspect-[3/4]">
                <Image
                  src={item.img}
                  alt={item.name}
                  fill
                  className="object-cover rounded-md"
                />
                {/* <div className="absolute bottom-2 left-2 text-white text-sm font-medium bg-black/50 px-2 py-1 rounded">
                  {item.name}
                </div> */}
              </div>
            ))}
          </div>
        </SwiperSlide>
      </Swiper>

      {/* 底部指示器按钮：2个，激活态黑色，未激活灰色 */}
      <div className="flex justify-center gap-2">
        <button
          onClick={() => handleIndicatorClick(0)}
          className={`w-6 h-[5px] rounded-full  opacity-100 ${
            activeIndex === 0 ? "bg-[#333]" : "bg-[#E5E5E5]"
          }`}
          aria-label="切换到第一块"
        />
        <button
          onClick={() => handleIndicatorClick(1)}
          className={`w-6 h-[5px] rounded-full  opacity-100 ${
            activeIndex === 1 ? "bg-[#333]" : "bg-[#E5E5E5]"
          }`}
          aria-label="切换到第二块"
        />
      </div>
    </div>
  );
}
