import { useMutation } from "@apollo/client/react";
import { Dropdown } from "antd";
import { useTranslation } from "react-i18next";
import { UPDATE_JOB } from "../../graphql/jobs.queries";
import { logImEXEvent } from "../../firebase/firebase.utils";

export default function ProductionListColumnPaintPriority({ record }) {
  const { t } = useTranslation();

  const [updateAlert] = useMutation(UPDATE_JOB);

  const handleSetPaintPriority = (e) => {
    logImEXEvent("production_set_paint_priority");
    //  e.stopPropagation();
    const { key } = e;
    updateAlert({
      variables: {
        jobId: record.id,
        job: {
          production_vars: {
            ...record.production_vars,
            paintpriority: key === "clearPaintPriority" ? null : key
          }
        }
      }
    }).then(() => {
      if (record.refetch) record.refetch();
    });
  };

  const menu = {
    items: [
      {
        key: "clearPaintPriority",
        label: t("production.actions.paintpriority-clear")
      },
      {
        key: "set",
        label: t("production.actions.paintpriority-set"),
        children: new Array(15).fill().map((value, index) => ({
          key: index + 1,
          label: <div style={{ marginLeft: "2rem", marginRight: "2rem" }}>{index + 1}</div>
        }))
      }
    ],
    onClick: handleSetPaintPriority
  };

  return (
    <Dropdown menu={menu} trigger={["click"]}>
      <div style={{ width: "100%", height: "19px", cursor: "pointer" }}>{record.production_vars?.paintpriority}</div>
    </Dropdown>
  );
}
