$(".barang").on("input", function () {
  // console.log('KEYUP:', unit);
  let barang = $(this).val();
  //   console.log("KEYUP:", barang);

  if (barang.length >= 2) {
    $.ajax({
      url: "asset/autocomplete_barang.php",
      type: "POST",
      data: {
        barang: barang,
      },
      success: function (data) {
        // console.log("RESPONSE:", data);
        $(".barangList").html(data).show();
      },
    });
  } else {
    $(".barangList").hide();
  }
});

$(document).on("click", ".barang-item", function () {
  $(".barang").val($(this).data("nama"));
  $(".barangList").hide();

  let barangId = $(this).data("id");
  let custId = $("#idcustomer").val();
  console.log("barangId", barangId);
  console.log("custId", custId);
  $.ajax({
    url: "asset/gethrgbrg.php",
    type: "POST",
    data: {
      barangId: barangId,
      custId: custId,
    },
    success: function (data) {
      console.log('Response dari server:', data);
      $("#hargab").text("Harga Sebelumnya : " + data);
    },
  });

  let barangNama = $(this).data("nama");
  console.log(barangNama);
  const sparepartHydraulic = [
    "Sparepart Hydraulic Hand Pallet",
    "Sparepart Hydraulic Hand Forklift Manual",
    "Sparepart Hydraulic Hand Forklift Semi Electric",
    "Sparepart Hydraulic Hand Forklift Electric",
    "Sparepart Hydraulic Drum Porter",
    "Sparepart Hydraulic Drum Handler Manual",
    "Sparepart Hydraulic Durm Handler Semi Electric",
    "Sparepart Hydraulic Drum Handler Electric",
    "Sparepart Hydraulic Cold Storage Hand Pallet",
    "Sparepart Hydraulic Cold Storage Hand Forklift Manual",
    "Sparepart Hydraulic Cold Storage Hand Forklift Semi Electric",
    "Sparepart Hydraulic Cold Storage Hand Forklift Electric",
    "Sparepart Hydraulic Cold Storage Drum Porter",
    "Sparepart Hydraulic Cold Storage Drum Handler Manual",
    "Sparepart Hydraulic Cold Storage Durm Handler Semi Electric",
    "Sparepart Hydraulic Cold Storage Drum Handler Electric",
  ];

  const templatesb = {
    opsib1: `(garansi 3 bulan)`,
    opsib2: `(garansi 3 bulan)
   - Dust Ring as hydraulic
   - Sealing Ring as hydraulic
   - O-ring as hydraulic
   - Dust Ring as pump hydraulic
   - Sealing Ring as pump hydraulic
   - O-ring as Bypass
   - Hydraulic Oil`,
  };

  $(".pilihOpsib").on("change", function () {
    const key = this.value.trim();

    if (templatesb[key]) {
      $(".keteb").val(templatesb[key]);
    }
  });

  if (sparepartHydraulic.includes(barangNama)) {
    $(".opsib").css("display", "block");
    $(".keteb").css("display", "block");

    $(".pilihOpsib").val("opsib1").trigger("change");
    document
      .getElementById("pilihOpsib")
      .addEventListener("change", function () {
        const key = this.value;
        console.log(key);
        console.log(templatesb);
        console.log(templatesb[key]);
        if (templatesb[key]) {
          document.getElementById("keteb").value = templatesb[key];
        }
      });
  } else {
    $(".opsib").css("display", "none");
    $(".keteb").css("display", "none");
  }

});
$(".modal-barang").on("hidden.bs.modal", function () {
  $(".opsib").hidden();
  $("#keteb").hidden();
});
