/* assets/js/app.js — SiPendas */

document.addEventListener('DOMContentLoaded', function () {

  // ---- Sidebar Toggle ----
  const toggleBtn = document.getElementById('sidebarToggle');
  const nav       = document.getElementById('layoutSidenav_nav');
  if (toggleBtn && nav) {
    toggleBtn.addEventListener('click', function () {
      nav.classList.toggle('sb-sidenav-toggled');
    });
    // Close on outside click (mobile)
    document.addEventListener('click', function (e) {
      if (window.innerWidth < 992 &&
          !nav.contains(e.target) &&
          !toggleBtn.contains(e.target)) {
        nav.classList.remove('sb-sidenav-toggled');
      }
    });
  }

  // ---- Auto-dismiss alerts ----
  document.querySelectorAll('.alert-dismissible').forEach(function(el) {
    setTimeout(function() {
      const bsAlert = bootstrap.Alert.getOrCreateInstance(el);
      if (bsAlert) bsAlert.close();
    }, 5000);
  });

  // ---- Confirm delete ----
  document.querySelectorAll('[data-confirm]').forEach(function(el) {
    el.addEventListener('click', function(e) {
      const msg = el.getAttribute('data-confirm') || 'Yakin ingin menghapus data ini?';
      if (!confirm(msg)) e.preventDefault();
    });
  });

  // ---- Upload zone drag & drop ----
  document.querySelectorAll('.upload-zone').forEach(function(zone) {
    const input = zone.querySelector('input[type=file]');
    zone.addEventListener('click', function() { if(input) input.click(); });
    zone.addEventListener('dragover', function(e) { e.preventDefault(); zone.classList.add('dragover'); });
    zone.addEventListener('dragleave', function() { zone.classList.remove('dragover'); });
    zone.addEventListener('drop', function(e) {
      e.preventDefault();
      zone.classList.remove('dragover');
      if (input && e.dataTransfer.files.length) {
        const dt = new DataTransfer();
        Array.from(e.dataTransfer.files).forEach(f => dt.items.add(f));
        input.files = dt.files;
        input.dispatchEvent(new Event('change'));
      }
    });
  });

  // ---- Preview foto sebelum upload ----
  document.querySelectorAll('.input-foto-preview').forEach(function(input) {
    input.addEventListener('change', function() {
      const preview = document.getElementById(input.getAttribute('data-preview'));
      if (!preview) return;
      const file = input.files[0];
      if (file && file.type.startsWith('image/')) {
        const reader = new FileReader();
        reader.onload = e => { preview.src = e.target.result; preview.classList.remove('d-none'); };
        reader.readAsDataURL(file);
      }
    });
  });

  // ---- Tooltip init ----
  document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(function(el) {
    new bootstrap.Tooltip(el, { trigger: 'hover' });
  });

  // ---- Table search filter ----
  const searchInput = document.getElementById('tableSearch');
  if (searchInput) {
    searchInput.addEventListener('input', function() {
      const val = this.value.toLowerCase();
      document.querySelectorAll('.table-searchable tbody tr').forEach(function(row) {
        row.style.display = row.textContent.toLowerCase().includes(val) ? '' : 'none';
      });
    });
  }
});
