"use client"
import {useState, useEffect, useRef} from "react";
import s from "./index.module.scss";
import {Link} from "@/lib/i18n/navigation";
import Image from "@/modules/common/components/image";
import { Swiper, SwiperSlide } from 'swiper/react';
import 'swiper/css';
import 'swiper/css/navigation';
import type {Swiper as SwiperType} from 'swiper';
import Button from "@/modules/common/components/button/border";
import { Product, ProductCategory } from "@/types/gpl/graphql";
type Props = {
param: {
categories: ProductCategory[];
products: Product[];
};
}
export default function ProductOne({ param }: Props) {
const swiperInsRef = useRef<SwiperType|null>(null);
const {categories, products} = param;
const [currentCategory, setCurrentCategory] = useState<ProductCategory>(categories[0]);
const [productList, setProductList] = useState<Product[]>(products?.filter((item) => item?.product_category?.slug === currentCategory?.slug));
useEffect(() => {
setProductList(products?.filter((item) => item?.product_category?.slug === currentCategory?.slug) || []);
}, [currentCategory, products]);
return (
<div className={s.container}>
<div className={s.content}>
<div className={s.tabSwitcher}>
<div className={s.tabArrow} onClick={() => swiperInsRef.current?.slidePrev()}>
<svg width="30" height="30" viewBox="0 0 30 30" fill="none" data-astro-cid-pjn6vdth="true">
<path d="M15 20L10 15L15 10M10 15H20M27.5 15C27.5 21.9036 21.9036 27.5 15 27.5C8.09644 27.5 2.5 21.9036 2.5 15C2.5 8.09644 8.09644 2.5 15 2.5C21.9036 2.5 27.5 8.09644 27.5 15Z" stroke="#1E1E1E" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</div>
<Swiper
slidesPerView={2} //预览
spaceBetween={0} //间距
className={s.mySwiper}
breakpoints={{
//断点
0: {
slidesPerView: 1,
},
480: {
slidesPerView: 2,
},
768: {
slidesPerView: 3,
},
1024: {
slidesPerView: 4,
},
1200: {
slidesPerView: 5,
}
}}
onSwiper={(swiper) => swiperInsRef.current = swiper} //2.实例化时存入swiper
>
{
categories?.map((item,index) =>
<SwiperSlide key={index}>
<div className={s.item}>
<div
className={`${s.tabItem} ${currentCategory?.slug === item?.slug ? s.active : ''}`}
data-index={index}
onClick={() => setCurrentCategory(item)}
>
<div className={s.tabOverLay}></div>
<div className={s.tabItemImg}>
{item?.cover &&
<Image image={item?.cover} sizes="60px"/>}
</div>
<div className={s.tabItemTitle}>
{item?.name}
</div>
</div>
</div>
</SwiperSlide>
)
}
</Swiper>
<div className={s.tabArrow} onClick={() => swiperInsRef.current?.slideNext()}>
<svg width="30" height="30" viewBox="0 0 30 30" fill="none" data-astro-cid-pjn6vdth="true">
<path d="M15 10L20 15L15 20M20 15H10M27.5 15C27.5 21.9036 21.9036 27.5 15 27.5C8.09644 27.5 2.5 21.9036 2.5 15C2.5 8.09644 8.09644 2.5 15 2.5C21.9036 2.5 27.5 8.09644 27.5 15Z" stroke="#1E1E1E" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</div>
</div>
<div className={s.productLayoutList}>
<div className={s.productLayout}>
<div className={s.featureBox}>
{productList?.[0] && (
<div className={s.featureCard}>
{/* <Link href={`product/${item?.products?.[0]?.slug}`} class="absolute-link" aria-label={item?.products?.[0]?.name}></Link> */}
<div className={s.featureImg}>
<div className={s.featureImgCenter}>
<Image image={productList?.[0]?.cover}
sizes="768:100vw,1024:50vw,192px"/>
</div>
</div>
<div className={s.featureContent}>
<div className={s.productTitle}>
{productList?.[0]?.name}
</div>
<div className={s.productDescription}>
{productList?.[0]?.desc}
</div>
<div className={s.buttonBox}>
<Link href={`product/${productList?.[0]?.slug}`}>
<div className={s.centerBox}>
<span>Learn More</span>
<svg fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" className="size-6" data-astro-cid-i6d4wzmg="true">
<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 4.5 21 12m0 0-7.5 7.5M21 12H3"></path>
</svg>
</div>
</Link>
</div>
</div>
</div>
)}
</div>
<div className={s.productBox}>
{[...(productList?.slice(1,5) || []), ...Array(Math.max(0, 4 - (productList?.slice(1,5)?.length || 0)))].map((product:Product, index) => (
product ? (
<div className={s.productItem} key={index}>
{/* <a href={localService?.url(`product/${product?.slug}`)} class="absolute-link" aria-label={product?.name}></a> */}
<div className={s.productImg}>
<div className={s.productImgCenter}>
<Image image={product?.cover}
sizes="768:100vw,1024:50vw,122px"/>
</div>
</div>
<div className={s.productContent}>
<div className={s.productTitle}>
{product?.name}
</div>
<div className={s.productDescription}>
{product?.desc}
</div>
<div className={s.buttonBox}>
<Link href={`product/${product?.slug}`}>
<div className={s.centerBox}>
<span>Learn More</span>
<svg fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" className="size-6" data-astro-cid-i6d4wzmg="true">
<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 4.5 21 12m0 0-7.5 7.5M21 12H3"></path>
</svg>
</div>
</Link>
</div>
</div>
</div>
) : (
<div className={s.productItemPlaceholder}></div>
)
))}
</div>
</div>
</div>
</div>
</div>
);
}
.container {
width: 100%;
padding: 70px 20px;
.content {
width: 100%;
max-width: var(--max-width);
margin: 0 auto;
.tabSwitcher {
display: flex;
align-items: center;
justify-content: center;
gap: 24px;
margin-bottom: 40px;
}
.tabArrow {
width: 40px;
height: 40px;
border: 0;
background: transparent;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
color: #111;
flex-shrink: 0;
svg {
width: 24px;
height: 24px;
display: block;
}
&:disabled {
opacity: 0.3;
cursor: not-allowed;
}
}
.item {
padding: 0 5px;
}
.tabItem {
padding: 10px 0;
height: 120px;
cursor: pointer;
background: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
transition: all 0.2s ease-in-out;
position: relative;
overflow: hidden;
.tabItemTitle {
transition: all .3s ease-in-out;
}
&:hover {
.tabOverLay {
transform: translateY(0);
}
.tabItemTitle {
color: #fff;
}
}
.tabOverLay {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 100%;
background: var(--primary-color);
transform: translateY(100%);
transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1);
z-index: 1;
}
.tabItemImg,
.tabItemTitle {
position: relative;
z-index: 2;
}
}
.tabItemImg {
width: 82px;
height: 82px;
margin-bottom: 6px;
}
.tabItemTitle {
text-align: center;
font-size: var(--font-base);
line-height: 1.2;
}
.tabItem.active {
background-color: var(--primary-color);
color: #fff;
box-shadow: var(--card-shadow);
}
.productLayoutList {
width: 100%;
margin: 0 auto;
.productLayout {
display: grid;
gap: 16px;
margin-bottom: 30px;
width: 100%;
@include lg {
grid-template-columns: 460px minmax(0, 1fr);
gap: 16px;
margin-bottom: 40px;
align-items: stretch;
}
.featureBox {
width: 100%;
.featureCard {
background: var(--back-color);
position: relative;
transition: transform 0.3s ease, box-shadow 0.3s ease;
height: 100%;
display: flex;
flex-direction: column;
&:hover {
transform: translateY(-5px);
box-shadow: var(--card-shadow);
.featureImgCenter {
transform: scale(1.05);
}
}
.featureImg {
width: 192px;
aspect-ratio: 192 / 190;
margin: 0 auto;
.featureImgCenter {
width: 100%;
height: 100%;
transition: transform 0.3s ease;
}
}
.featureContent {
padding: 0 10px;
flex: 1;
display: flex;
flex-direction: column;
justify-content: space-between;
.productTitle {
font-size: var(--font-lg);
font-weight: 500;
text-align: center;
}
.productDescription {
color: var(--info-color);
@include line-clamp(2);
}
.more {
position: relative;
z-index: 2;
}
}
}
}
.productBox {
display: grid;
gap: 14px;
min-width: 0;
width: 100%;
@include md {
grid-template-columns: 1fr 1fr;
}
@include lg {
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 20px 16px;
}
.productItem {
background: var(--back-color);
display: flex;
flex-direction: row;
transition: transform 0.3s ease, box-shadow 0.3s ease;
&:hover:not(.placeholder) {
transform: translateY(-5px);
box-shadow: var(--card-shadow);
.productImgCenter {
transform: scale(1.05);
}
}
.productImg {
width: 122px;
aspect-ratio: 122 / 108;
.productImgCenter {
width: 122px;
height: 100px;
transition: transform 0.3s ease;
}
}
.productContent {
display: flex;
flex-direction: column;
justify-content: center;
gap: 10px;
padding: 10px 0;
.productTitle {
font-size: var(--font-lg);
font-weight: 500;
}
.productDescription {
color: var(--info-color);
@include line-clamp(2);
}
.more {
position: relative;
z-index: 2;
}
}
&.placeholder {
background-color: #fff;
}
}
}
}
}
.buttonBox {
display: inline-block;
color: var(--primary-color);
padding: 10px 25px;
padding-left: 0;
text-align: center;
border-radius: 25px;
cursor: pointer;
.centerBox {
display: flex;
align-items: center;
gap: 5px;
transition: all .2s ease-in-out;
svg {
width: 15px;
height: 15px;
}
}
}
.buttonBox:hover {
.centerBox {
transform: translateX(5px);
color: var(--secondary-color);
}
}
}
}