import { useMutation } from "@apollo/client";
import { Button, notification } from "antd";
import { gql } from "@apollo/client";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
export default function JobAdminDeleteIntake({ job }) {
  const { t } = useTranslation();
  const [loading, setLoading] = useState(false);
  const [deleteIntake] = useMutation(gql`
    mutation DELETE_INTAKE($jobId: uuid!) {
      update_jobs_by_pk(
        pk_columns: { id: $jobId }
        _set: { intakechecklist: null }
      ) {
        id
        intakechecklist
      }
    }
  `);

  const [DELETE_DELIVERY] = useMutation(gql`
    mutation DELETE_DELIVERY($jobId: uuid!) {
      update_jobs_by_pk(
        pk_columns: { id: $jobId }
        _set: { deliverchecklist: null }
      ) {
        id
        deliverchecklist
      }
    }
  `);

  const handleDelete = async (values) => {
    setLoading(true);
    const result = await deleteIntake({
      variables: { jobId: job.id },
    });

    if (!!!result.errors) {
      notification["success"]({ message: t("jobs.successes.save") });
    } else {
      notification["error"]({
        message: t("jobs.errors.saving", {
          error: JSON.stringify(result.errors),
        }),
      });
    }
    setLoading(false);
  };

  const handleDeleteDelivery = async (values) => {
    setLoading(true);
    const result = await DELETE_DELIVERY({
      variables: { jobId: job.id },
    });

    if (!!!result.errors) {
      notification["success"]({ message: t("jobs.successes.save") });
    } else {
      notification["error"]({
        message: t("jobs.errors.saving", {
          error: JSON.stringify(result.errors),
        }),
      });
    }
    setLoading(false);
  };

  return (
    <>
      <Button loading={loading} onClick={handleDelete}>
        {t("jobs.labels.deleteintake")}
      </Button>
      <Button loading={loading} onClick={handleDeleteDelivery}>
        {t("jobs.labels.deletedelivery")}
      </Button>
    </>
  );
}
