"use client";
import CardCart from "@comp/Cards/CardCart/page";
import NavBarDownCart from "@comp/Carts/component/NavBarDownCart/page";
import NavbarTransparent from "@comp/Navbar/NavbarTransparent";
import Navbar from "@comp/Navbar/page";
import AppContext from "@ctx/AppContext";
import { useRouter } from "next/navigation";
import Chapar from "plugins/Chapar";
import PersianNumber from "plugins/PersianNumber";
import { useContext, useEffect } from "react";
import { toast } from "react-toastify";

const CartData = () => {
  const CTX = useContext(AppContext);
  const router = useRouter();
  const cart = CTX.state.cart;

  const calculateTotalCost = cart.reduce(
    (total, item) => total + parseInt(item.cost) * item.count,
    0
  );

  const calculateTotalCostWithDiscount = cart.reduce(
    (total, item) => total + parseInt(item.costWithDiscount) * item.count,
    0
  );

  const handleGoCheckOut = async () => {
    CTX.GoCheckOut();
  };

  useEffect(() => {
    CTX.setBottomSheetCartOpen(false);
  }, []);
  return (
    <>
      <div className=" pb-20">
        <NavbarTransparent theme={1} />

        <div className="xs:w-full lg:w-4/12 mx-auto ">
          <div className="text-right flex rtl justify-between bg-primary-600 rounded-2xl m-3 px-4 ">
            <p className="mb-0 text-sm font-semibold py-4 text-white">
              محصولات انتخاب شده
            </p>

            <div className="bg-primary-700 w-fit h-fit relative m-3 p-1 rounded-lg">
              <p className="mb-0 text-[11px] text-white rtl">
                <PersianNumber number={cart?.length} style={"mx-1"} />
                محصول
              </p>
            </div>
          </div>

          <div className="">
            {cart?.map((e, index) => (
              <CardCart key={index} data={e} />
            ))}

            <div className=" bg-primary-600 rounded-2xl pb-2 m-2 ">
              <div className="text-right flex rtl justify-between my-3 px-4 ">
                <p className="mb-0 text-sm font-medium py-4 text-white">
                  حساب نهایی
                </p>
              </div>

              <div>
                <div className="flex justify-between rtl px-4">
                  <p className="mb-0 text-[12px] text-gray-200">قیمت </p>

                  <div className="flex justify-center">
                    <p className="mb-0 ">
                      <PersianNumber
                        number={(calculateTotalCost / 10)?.toLocaleString()}
                        style={"!text-[14px] !font-bold text-white "}
                      />
                    </p>
                    <small className="text-gray-100 text-[12px] mt-1 mx-1">
                      {" "}
                      تومان
                    </small>
                  </div>
                </div>

                <div className="flex justify-between rtl my-2 px-4">
                  <p className="mb-0 text-[12px] text-gray-200">تخفیف محصول</p>

                  <div className="flex justify-center ">
                    <p className="mb-0 ">
                      <PersianNumber
                        number={(
                          (calculateTotalCost -
                            calculateTotalCostWithDiscount) /
                          10
                        )?.toLocaleString()}
                        style={"!text-[14px] !font-bold text-white "}
                      />
                    </p>
                    <small className="text-gray-100 text-[12px] mt-1 mx-1">
                      {" "}
                      تومان
                    </small>
                  </div>
                </div>

                <div className="flex justify-between bg-primary-700 p-3 rtl m-2 rounded-xl">
                  <p className="mb-0 text-sm text-gray-200 font-bold">
                    قابل پرداخت
                  </p>

                  <div className="flex justify-center">
                    <p className="mb-0 ">
                      <PersianNumber
                        number={
                          calculateTotalCostWithDiscount !== 0
                            ? (
                                calculateTotalCostWithDiscount / 10
                              )?.toLocaleString()
                            : 0
                        }
                        style={"!text-[14px] !font-bold text-white"}
                      />
                    </p>
                    <small className="text-gray-100 text-[12px] mt-1 mx-1">
                      {" "}
                      تومان
                    </small>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <NavBarDownCart
          calculateTotalCost={calculateTotalCostWithDiscount / 10}
          event={() => handleGoCheckOut()}
          permissionGoPay={!!cart.length > 0}
        />
      </div>
    </>
  );
};

export default CartData;
