jQuery("#wc-orders-filter").on("submit", function (event) {
  if (
    jQuery("#bulk-action-selector-top").val() === "gcargo_print_bulk_shipments"
  ) {
    event.preventDefault();
    request(this);
  }
});

jQuery("#gcargo-shipments").on("submit", function (event) {
  if (jQuery("#bulk-action-selector-top").val() === "bulk_print") {
    event.preventDefault();
    request(this);
  }
});

function request(form) {
  const data = jQuery(form)
    .serializeArray()
    .filter((item) => item.name === "id[]")
    .map((item) => item.value);

  jQuery.ajax({
    url: `${window.gcargo.ajaxurl}?action=gcargo_bulk_print&_wpnonce=${window.gcargo.nonce}`,
    type: "POST",
    dataType: "json",
    contentType: "application/json",
    accept: "application/json",
    data: JSON.stringify(data),
    success: (response) => {
      window.open(response, "_blank");
    },
  });
}
