import { useQueryClient } from "@tanstack/react-query";
import { usePopup } from "../../../stores/usePopup";
import { useUpdateStatus } from "../../../hooks/useUpdateStatus";
import { Button } from "antd";
import { STATUSES } from "../../../config";

export const ChangeStatusButton = ({
  selectedIds,
  selectedStatus,
  onOpenMakeWorkingModal,
  onSuccess,
}) => {
  const queryClient = useQueryClient();
  const { setPopup } = usePopup();
  const { mutate: updateStatus } = useUpdateStatus({
    onSuccess: () => {
      queryClient.invalidateQueries(["on-approval-points"]);
      setPopup(null);
      onSuccess();
    },
  });

  const handleClick = (e) => {
    e.stopPropagation();

    const params = new URLSearchParams({
      status: selectedStatus,
      "location_ids[]": selectedIds,
    });

    if (selectedStatus === STATUSES.working) {
      onOpenMakeWorkingModal();
    } else {
      updateStatus(params);
    }
  };

  return (
    <>
      <Button type="primary" onClick={handleClick}>
        Обновить статус
      </Button>
    </>
  );
};
