import { useApiRequest } from "../../utils/useApiRequest";
import { useModalStore } from "../../context/zustand-store/appStore";
import { Grid } from "../../components/Grid/Grid";
import Button from "../../components/Button/Button";
import { motion } from "framer-motion";
import SVGImage from "../../components/SvgImage/SvgImage";
import editIcon from "../../assets/images/svg/edit.svg?react";
import { AddBroker } from "./AddBroker";
import { NoData } from "../../components/NoData/NoData";
import { PageTitle } from "../../components/PageTitle/PageTitle";
import { checkAccess } from "../../utils/checkAccess";
import { BooleanQuestion } from "../../components/BooleanQuestion/BooleanQuestion";
import trashIcon from "../../assets/images/svg/trash.svg?react";

export const Brokers = () => {
  const { openModal } = useModalStore();

  const { data: brokers, refetch } = useApiRequest({
    api: "/product/web/api/v1/broker/",
    method: "get",
    params: { page: 1, page_size: 1000 },
    queryKey: ["brokers"],
  });

  return (
    <Grid container column className="">
      <Grid>
        <Button
          size="small"
          variant="submit"
          page="pricing"
          access="Submit-Broker"
          onClick={() => {
            openModal({
              title: "ایجاد کارگزار",
              content: <AddBroker getData={refetch} />,
            });
          }}
        >
          ایجاد کارگزار
        </Button>
      </Grid>

      <Grid className="mt-4">
        <PageTitle title="کارگزاران" />
      </Grid>

      {!brokers?.results?.length ? (
        <NoData title="کارگزاری موجود نیست!" />
      ) : (
        <motion.div className="grid sm:grid-cols-1 lg:grid-cols-2 2xl:grid-cols-3 gap-2 justify-items-center mt-4 items-center">
          {brokers?.results?.map((item: any, i: number) => (
            <motion.div
              key={i}
              className="bg-white h-24 dark:bg-dark-600 rounded-lg border border-gray1-200 dark:border-dark-300 overflow-hidden flex justify-between items-center p-2 w-full"
              initial="hidden"
              animate="show"
            >
              <div className="w-1/6 rounded-full flex justify-start">
                <div className="bg-white1-400 w-8 p-1 dark:bg-gray-800 rounded-lg flex justify-center text-gray2-300 dark:text-dark-300">
                  {i + 1}
                </div>
              </div>

              <div className="p-3 flex flex-col text-sm text-gray-500 dark:text-dark-200 w-2/6">
                <p>{item?.name || "بدون نام"}</p>
              </div>

              <div className="flex flex-col justify-center text-[8px] sm:text-[8px] lg:text-[10px] 2xl:text-xs w-2/6 gap-1">
                <p
                  className={`w-full ${
                    item?.product
                      ? "bg-primary-500 text-white"
                      : "bg-white1-400 text-gray-800"
                  } rounded-lg text-center py-1`}
                >
                  {item?.product ? item?.product?.name : "عمومی"}
                </p>
                <p
                  className={`w-full text-white bg-gray-300 rounded-lg text-center py-1`}
                >
                  {item?.organization_type
                    ? item?.organization_type?.name
                    : "بدون سازمان"}
                </p>
                <p
                  className={`w-full text-gray-700 bg-primary-50 rounded-lg text-center py-1`}
                >
                  {item?.required ? "تعرفه الزامی" : "تعرفه اختیاری"}
                </p>
              </div>

              <div className="flex justify-between items-end flex-col gap-4 w-1/6">
                {checkAccess({ page: "pricing", access: "Edit-Broker" }) && (
                  <SVGImage
                    onClick={() => {
                      openModal({
                        title: "ویرایش کارگزار",
                        content: <AddBroker getData={refetch} item={item} />,
                      });
                    }}
                    src={editIcon}
                    className={`cursor-pointer w-5 text-primary-600 dark:text-primary-100`}
                  />
                )}
                {checkAccess({ page: "pricing", access: "Edit-Broker" }) && (
                  <SVGImage
                    onClick={() => {
                      openModal({
                        title: "از حذف کارگزار مطمئنید؟",
                        content: (
                          <BooleanQuestion
                            isAlert
                            getData={refetch}
                            api={`product/web/api/v1/broker/${item?.id}/`}
                            method="delete"
                          />
                        ),
                      });
                    }}
                    src={trashIcon}
                    className={`cursor-pointer w-5  text-red-500 dark:text-red-400`}
                  />
                )}
              </div>
            </motion.div>
          ))}
        </motion.div>
      )}
    </Grid>
  );
};
