/* eslint-disable @typescript-eslint/no-explicit-any */
import React from "react";
import Modal from "@/components/elements/Modal";
import useAxios from "@/hooks/useAxios";
import { IOffers } from "@/types/types";
import RoundedButton from "@/components/elements/RoundedButton";
import OffersItem from "./OffersItem";
import {
  InfiniteData,
  QueryObserverResult,
  RefetchOptions,
} from "@tanstack/react-query";

type ConfirmModalProps = {
  isOpen: boolean;
  onClose: () => void;
  item: IOffers | null;
  refetch: (
    options?: RefetchOptions
  ) => Promise<QueryObserverResult<InfiniteData<any, unknown>, Error>>;
};

function ConfirmModal({ isOpen, onClose, item, refetch }: ConfirmModalProps) {
  const { request } = useAxios();
  const updateHandler = async (offerId: string | null, action: string) => {
    try {
      await request("POST", "/offers/update-status", {
        offerId,
        action,
      });
      refetch();
      onClose();
    } catch (error: unknown) {
      console.log(error);
    }
  };
  return (
    <Modal height="440px" isOpen={isOpen} onClose={onClose}>
      <div className="flex flex-col items-center gap-6 mt-5">
        {item && <OffersItem item={item} />}
        <div className="grid grid-cols-2 gap-4">
          <RoundedButton
            className="h-9 w-36 mt-4 text-[#0C8002] !border-[#0C8002]"
            onClick={() => {
              updateHandler(item?._id || "", "accept");
            }}
          >
            تایید درخواست
          </RoundedButton>
          <RoundedButton
            className="h-9 w-36 mt-4 text-[#FF0000] !border-[#FF0000]"
            onClick={() => {
              updateHandler(item?._id || "", "reject");
            }}
          >
            رد درخواست
          </RoundedButton>
        </div>
      </div>
    </Modal>
  );
}

export default ConfirmModal;
