import { Button } from "antd";
import axios from "axios";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";

export default function JobSendPartPriceChangeComponent({ job, disabled }) {
  const { t } = useTranslation();
  const [loading, setLoading] = useState(false);
  const notification = useNotification();

  const handleClick = async () => {
    setLoading(true);
    try {
      const ppcData = await axios.post("/job/ppc", { jobid: job.id });
      await axios.post("http://localhost:1337/ppc/", ppcData.data);
    } catch (error) {
      notification.error({
        title: t("jobs.errors.partspricechange", {
          error: JSON.stringify(error)
        })
      });
    } finally {
      setLoading(false);
    }
  };

  return (
    <Button onClick={handleClick} loading={loading} disabled={disabled}>
      {t("jobs.actions.sendpartspricechange")}
    </Button>
  );
}
