$(".jservice").on("input", function () {
  let jservice = $(this).val();
  let modal = $(this).closest(".modal");
  let list = modal.find(".serviceList");
  console.log(jservice);
  if (jservice.length >= 2) {
    $.ajax({
      url: "asset/autocomplete_service.php",
      type: "POST",
      data: {
        jservice: jservice,
      },
      success: function (data) {
        list.html(data).show();
      },
    });
  } else {
    list.hide();
  }
});

$(document).on("click", ".service-item", function () {
  $(".jservice").val($(this).data("nama"));
  $(".serviceList").hide();

  let serviceId = $(this).data("id");
  let custId = $("#idcust").val();
  console.log("serviceId", serviceId);
  console.log("custId", custId);
  $.ajax({
    url: "asset/gethrgacus.php",
    type: "POST",
    data: {
      serviceId: serviceId,
      custId: custId,
    },
    success: function (data) {
      // console.log('Response dari server:', data);
      $("#hargae").text("Harga Sebelumnya : " + data);
    },
  });
  //opsi
  let serviceNama = $(this).data("nama");
  //   let isijservice = $("#jservice").val();
  const layananHydraulic = [
    "Service Hydraulic Hand Pallet Manual",
    "Service Hydraulic Hand Pallet Electric",
    "Service Hydraulic Hand Forklift Manual",
    "Service Hydraulic Hand Forklift Semi Electric",
    "Service Hydraulic Hand Forklift Electric",
    "Service Hydraulic Drum Porter",
    "Service Hydraulic Drum Handler Manual",
    "Service Hydraulic Drum Handler Semi Electric",
    "Service Hydraulic Drum Handler Electric",
    "Service Hydraulic Cold Storage Hand Pallet",
    "Service Hydraulic Cold Storage Hand Pallet Electric",
    "Service Hydraulic Cold Storage Hand Forklift Manual",
    "Service Hydraulic Cold Storage Hand Forklift Semi Electric",
    "Service Hydraulic Cold Storage Hand Forklift Electric",
    "Service Hydraulic Cold Storage Drum Porter",
    "Service Hydraulic Cold Storage Drum Handler Manual",
    "Service Hydraulic Cold Storage Drum Handler Semi Electric",
    "Service Hydraulic Cold Storage Drum Handler Electric",
  ];
  const jasaHydraulic = "Jasa Perbaikan Hydraulic";

  const isianket = `- Pembersihan body & hydraulic\n- Pembersihan roda depan & belakang`;

  const templates = {
    opsi1: `(Garansi 3 Bulan)
- pembersihan body & hydraulic
- pembersihan semua roda
- penggantian semua part dalam hydraulic
- penggantian oli full tank
- free pengecatan ulang full body`,

    opsi2: `(Dalam Masa Garansi)
- pembersihan body & hydraulic
- pembersihan semua roda
- penggantian semua part dalam hydraulic
- penggantian oli full tank
- free pengecatan ulang full body`,

    opsi3: `(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`,
  };

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

    if (templates[key]) {
      $(".kete").val(templates[key]);
    }
  });

  if (layananHydraulic.includes(serviceNama)) {
    $(".opsie").css("display", "block");
    $(".kete").css("display", "block");

    $(".pilihOpsi").val("opsi1").trigger("change");
    document
      .getElementById("pilihOpsi")
      .addEventListener("change", function () {
        const key = this.value;
        console.log(key);
        console.log(templates);
        console.log(templates[key]);
        if (templates[key]) {
          document.getElementById("kete").value = templates[key];
        }
      });
  } else {
    $(".opsie").css("display", "none");
    $(".kete").css("display", "none");
  }

  if (jasaHydraulic.includes(serviceNama)) {
    console.log("serviceNama:", serviceNama);
    console.log("isianket:", isianket);
    // $(".kete").css("display", "block");
    // document.getElementById("kete").value = isianket;
    let modal = $(this).closest(".modal");
    modal.find(".kete").val(isianket).show();
  }
});
