import React, { useEffect, useRef, useState } from "react";
import "./May_you_like_also.css";
import Product_card from "../../common-components/product_card/Product_card";
import { Swiper, SwiperSlide } from "swiper/react";
import { Autoplay, Pagination } from "swiper/modules";
import "swiper/css";
import "swiper/css/pagination";
import Order_summery_holder from "./order_summery_holder/Order_summery_holder";
import { Navigation } from "swiper/modules";
import { useDispatch, useSelector } from "react-redux";
import { getAllProducts } from "../../../features/products/productsSlice";

import Product_Info_Card from "../../common-components/product_info_card/Product_Info_Card";
import { variant_type } from "../../../utils/common";

function May_you_like_also({ data, instock }) {
  const dispatch = useDispatch();
  const productdata = useSelector((state) => state.product.allProducts);
  useEffect(() => {
    dispatch(
      getAllProducts({
        // name: searchTerm,
      }),
    );
  }, [dispatch]);

  console.log("May_you_like_also",data)

  return (
    <section className="May_you_like_also">
      <div className="container">
        <div className="row custom_row">
          <div
            className={
              !data || data?.data?.length === 0 ? "col-12" : "col-12"
            }
          >
            <div className="heading_holder">
              <h4 className="heading_text">You May Also Like</h4>
            </div>
            <Swiper
              modules={[Pagination, Navigation, Autoplay]}
              // navigation={true}
              spaceBetween={8}
              slidesPerView={2}
              // pagination={{ clickable: true }}
              autoplay={{
                delay: 3000, // 3 seconds
                disableOnInteraction: false,
                pauseOnMouseEnter: true,
              }}
              breakpoints={{
                0: { slidesPerView: 2 },
                576: { slidesPerView: 3 },
                768: { slidesPerView: 2 },
                992: { slidesPerView: 3 },
                1200: { slidesPerView: 3.5 },
                1400: { slidesPerView: 6 },
              }}
            >
              {/* {productdata?.data?.map((product, index) => ( */}
              {productdata?.data?.length > 0 ? (
                productdata?.data?.map((product, index) => {
                  let metal;
                  let size;
                  let triple;
                  if (
                    Number(product?.variant_type_id) ===
                    Number(variant_type.Single)
                  ) {
                    metal = product?.Product_Variants?.[0];
                  } else if (
                    Number(product?.variant_type_id) ===
                    Number(variant_type.Double)
                  ) {
                    metal = product?.Product_Variants?.[0];
                    size = metal?.Double_Variants?.[0];
                  } else if (
                    Number(product?.variant_type_id) ===
                    Number(variant_type.Triple)
                  ) {
                    metal = product?.Product_Variants?.[0];
                    size = metal?.Double_Variants?.[0];
                    triple = size?.Triple_Variants?.[0];
                  }

                  const activeVariant = triple || size || metal;

                  return (
                    <SwiperSlide key={index}>
                      <Product_Info_Card
                        product={product}
                        id={product.id}
                        imgSrc={process.env.NEXT_PUBLIC_IMG_URL + product.image}
                        vdoSrc={process.env.NEXT_PUBLIC_IMG_URL + product.video}
                        productName={product.name}
                        priceText={activeVariant?.price || 0}
                        mrp={activeVariant?.mrp || 0}
                        kgText={metal?.Metal?.name}
                        savingPrice={
                          parseFloat(activeVariant?.mrp || 0) -
                          parseFloat(activeVariant?.price || 0)
                        }
                      />
                    </SwiperSlide>
                  );
                })
              ) : (
                <div
                  style={{
                    textAlign: "center",
                    padding: "20px",
                    fontWeight: "bold",
                    width: "100%",
                    color: "#000",
                  }}
                >
                  Product Not Available
                </div>
              )}
              {/* ))} */}
            </Swiper>
          </div>

          {/* {data?.data?.length > 0 && (
            <div className="col-lg-4 col-md-5">
              <Order_summery_holder instock={instock} data={data} />
            </div>
          )} */}
        </div>
      </div>
    </section>
  );
}

export default May_you_like_also; 
