function collect(accessnumber, samplenumber, status) {
  // toggle checkbox
  const row = document.getElementById(`row-${accessnumber}-${samplenumber}`);
  const checkbox = row.querySelector('.coll-checkbox');
  checkbox.checked = status;

  $.ajax({
    url: `${BASE_URL}/api/specimen/collect/${accessnumber}`,
    type: "POST",
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    data: JSON.stringify({ samplenumber:samplenumber, status:status, userid:"<?=session('userid');?>" }), 
    success: function(response) {
      console.log("Success:", response);
    },
    error: function(xhr, status, error) {
      console.error("Error:", status, error);
      }
  });
}



function unreceive(accessnumber, samplenumber) {
  // toggle checkbox
  const row = document.getElementById(`row-${accessnumber}-${samplenumber}`);
  const checkbox = row.querySelector('.recv-checkbox');
  checkbox.checked = status;

  $.ajax({
    url: `${BASE_URL}/api/specimen/unreceive/${accessnumber}`,
    type: "POST",
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    data: JSON.stringify({ samplenumber:samplenumber }), 
    success: function(response) {
      console.log("Success:", response);
    },
    error: function(xhr, status, error) {
      console.error("Error:", status, error);
      }
  });
}

// Untuk Koment Value
function commentValue() {
  const input = prompt("Masukkan Komentar :");
}

function detailShow(userrole) {
  {
    const modal = document.getElementById('detailModal');
    const modalContent = document.getElementById('specimenModalContent');
    document.querySelectorAll('.pointercol').forEach(td => {
      td.addEventListener('click', async function () {
        const access = this.dataset.access;
        modalContent.innerHTML =
          "<p class='text-center text-muted'>Loading data...</p>";
        try {
          $.getJSON(`${BASE_URL}/api/specimen/${access}`, function(data) {
            $.get(`${BASE_URL}/${userrole}/modal_specimen`, function(tpl) {
              data.samples = data.samples.map(s => ({
                ...s,
                is_coll: s.colstatus == 1,
                is_recv: s.tubestatus == 4
              }));
              const html = Mustache.render(tpl, data);
              modalContent.innerHTML=html;
            });
          });
        } catch (err) {
          modalContent.innerHTML = "<p class='text-danger text-center'>Gagal memuat data.</p>";
          console.error(err);
        }
      });
    });
  }
}
