/* =============================================
   POS SYSTEM — app.js
   ============================================= */

// ── Modal global ──
function openModal()  { document.getElementById('modal').classList.add('open'); }
function closeModal() { document.getElementById('modal').classList.remove('open'); }

document.addEventListener('click', function(e) {
  if (e.target.classList.contains('modal-overlay')) {
    e.target.classList.remove('open');
  }
});

document.addEventListener('keydown', function(e) {
  if (e.key === 'Escape') {
    document.querySelectorAll('.modal-overlay.open').forEach(m => m.classList.remove('open'));
    closeSidebar();
  }
});

// ── Sidebar (desktop & mobile) ──
function openSidebar() {
  document.getElementById('sidebar').classList.add('open');
  const overlay = document.getElementById('sidebarOverlay');
  if (overlay) overlay.classList.add('open');
  document.body.style.overflow = 'hidden';
}

function closeSidebar() {
  document.getElementById('sidebar').classList.remove('open');
  const overlay = document.getElementById('sidebarOverlay');
  if (overlay) overlay.classList.remove('open');
  document.body.style.overflow = '';
}

// Hamburger di topbar
const hamburger = document.getElementById('hamburgerBtn');
if (hamburger) {
  hamburger.addEventListener('click', function() {
    const sidebar = document.getElementById('sidebar');
    if (sidebar.classList.contains('open')) closeSidebar();
    else openSidebar();
  });
}

// ── Auto-hilangkan alert setelah 4 detik ──
document.querySelectorAll('.alert').forEach(el => {
  setTimeout(() => {
    el.style.transition = 'opacity .5s';
    el.style.opacity    = '0';
    setTimeout(() => el.remove(), 500);
  }, 4000);
});

// ── Deteksi mobile ──
function isMobile() {
  return window.innerWidth <= 768;
}

// Tutup sidebar otomatis saat navigasi di mobile
document.querySelectorAll('.nav-link').forEach(link => {
  link.addEventListener('click', function() {
    if (isMobile()) closeSidebar();
  });
});
