import { useMutation } from "@apollo/client";
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 ProductionListColumnDetailPriority({ record }) {
  const { t } = useTranslation();

  const [updateAlert] = useMutation(UPDATE_JOB);

  const handleSetDetailPriority = (e) => {
    logImEXEvent("production_set_detail_priority");
    // e.stopPropagation();
    const { key } = e;
    updateAlert({
      variables: {
        jobId: record.id,
        job: {
          production_vars: {
            ...record.production_vars,
            detailpriority: key === "clearDetailPriority" ? null : key
          }
        }
      }
    }).then(() => {
      if (record.refetch) record.refetch();
    });
  };

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

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