"use client";
import React, { useEffect, useState } from "react";
import Image from "next/image";
import axios from "axios";

export default function Gallery() {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    async function fetchData() {
      try {
        setLoading(true);
        const res = await axios.get("/api/get-component-data", {
          params: { component_name: "Gallery" },
        });
        setData(res.data.data);
      } catch (err) {
        setError("获取Gallery数据失败");
      } finally {
        setLoading(false);
      }
    }
    fetchData();
  }, []);

  if (loading) return <div className="text-center py-20">Loading...</div>;
  if (error || !data) return null;

  // 主表字段
  const title = data.title || "Our Products";
  const subtitle = data.subtitle || "";
  const buttonText = data.button_text || "MORE";
  const buttonLink = data.button_link || "#";
  // 子表items
  const items = Array.isArray(data.items) ? data.items.slice(0, 6) : [];

  return (
    <section className="wrapper bg-[#f6f6f7]  py-12">
      <div className="container mx-auto px-4">
        <div className="flex items-center justify-between mb-8">
          <div>
            <h2 className="text-3xl md:text-4xl font-extrabold text-[#222] tracking-tight">{title}</h2>
            {subtitle && <div className="text-base text-gray-500 mt-1">{subtitle}</div>}
          </div>
          <a
            href={buttonLink}
            className="inline-flex items-center px-5 py-2 bg-[#1fc76f] rounded shadow hover:bg-[#1e9e4a] transition-colors duration-200 text-base font-semibold"
            style={{ color: "#f8fafc" }}
          >
            {buttonText}
            <span className="ml-2">
              <svg width="18" height="18" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M7 13l5-4-5-4" stroke="#f8fafc" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>
            </span>
          </a>
        </div>
        <div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-5">
          {items.map((item, idx) => {
            const link = item.item_link || "#";
            return (
              <a
                key={item.no || idx}
                href={link}
                className="relative group rounded-lg overflow-hidden shadow-md bg-white/60 hover:shadow-xl transition-all duration-300 cursor-pointer block"
                style={{ minHeight: 240 }}
                target={link.startsWith('http') ? '_blank' : undefined}
                rel={link.startsWith('http') ? 'noopener noreferrer' : undefined}
              >
                <Image
                  src={item.item_image || "/assets/img/photos/about2.jpg"}
                  alt={item.item_title || "product"}
                  width={400}
                  height={240}
                  className="object-cover w-full h-56 group-hover:scale-105 transition-transform duration-300"
                />
                <div className="absolute inset-0 flex flex-col items-center justify-center bg-black/10">
                  <div className="mb-2 flex items-center justify-center">
                    <span className="bg-white/80 group-hover:bg-white/95 transition-colors duration-200 rounded-full p-3 shadow-lg flex items-center justify-center">
                      <img src={item.item_icon || "/files/icon.svg"} alt="icon" className="w-9 h-9" />
                    </span>
                  </div>
                  <div className="text-white font-extrabold text-lg md:text-xl drop-shadow-lg group-hover:text-[#fff] group-hover:scale-105 transition-all duration-200 text-center">
                    {item.item_title || "-"}
                  </div>
                </div>
              </a>
            );
          })}
        </div>
      </div>
    </section>
  );
} 