// Vehicle Verify — main.js
(function () {
  'use strict';

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

    // ── Sidebar overlay (mobile) ──────────────────────────────
    var sidebar  = document.getElementById('sidebar');
    var overlay  = document.getElementById('sidebarOverlay');
    var menuBtns = document.querySelectorAll('.mobile-menu-btn');

    function openSidebar() {
      if (!sidebar) return;
      sidebar.classList.add('open');
      if (overlay) { overlay.style.display = 'block'; requestAnimationFrame(function(){ overlay.classList.add('visible'); }); }
      document.body.style.overflow = 'hidden';
    }

    function closeSidebar() {
      if (!sidebar) return;
      sidebar.classList.remove('open');
      if (overlay) { overlay.classList.remove('visible'); setTimeout(function(){ overlay.style.display = 'none'; }, 280); }
      document.body.style.overflow = '';
    }

    menuBtns.forEach(function (btn) {
      btn.addEventListener('click', function (e) {
        e.stopPropagation();
        sidebar && sidebar.classList.contains('open') ? closeSidebar() : openSidebar();
      });
    });

    if (overlay) overlay.addEventListener('click', closeSidebar);

    // Close sidebar on nav link click (mobile)
    if (sidebar) {
      sidebar.querySelectorAll('a').forEach(function (a) {
        a.addEventListener('click', function () {
          if (window.innerWidth < 992) closeSidebar();
        });
      });
    }

    // Close on resize to desktop
    window.addEventListener('resize', function () {
      if (window.innerWidth >= 992) closeSidebar();
    });

    // ── Auto-uppercase plate inputs ───────────────────────────
    document.querySelectorAll('input[name="plate_number"], input[name="plate"]').forEach(function (el) {
      el.addEventListener('input', function () {
        var pos = this.selectionStart;
        this.value = this.value.toUpperCase();
        try { this.setSelectionRange(pos, pos); } catch(e){}
      });
    });

    // ── File input label update ───────────────────────────────
    document.querySelectorAll('input[type="file"]').forEach(function (el) {
      el.addEventListener('change', function () {
        var hint = this.nextElementSibling;
        if (hint && this.files[0]) {
          hint.textContent = '📎 ' + this.files[0].name;
        }
      });
    });

    // ── Auto-hide alerts after 5s ─────────────────────────────
    document.querySelectorAll('.alert').forEach(function (el) {
      setTimeout(function () {
        el.style.transition = 'opacity .5s, max-height .5s';
        el.style.opacity = '0';
        el.style.maxHeight = '0';
        el.style.overflow = 'hidden';
        setTimeout(function () { if(el.parentNode) el.parentNode.removeChild(el); }, 520);
      }, 5000);
    });

    // ── Active nav highlight fix ──────────────────────────────
    var current = window.location.pathname;
    document.querySelectorAll('.sidebar-nav a').forEach(function (a) {
      if (a.getAttribute('href') && current.endsWith(a.getAttribute('href').split('/').pop())) {
        a.classList.add('active');
      }
    });

    // ── Theme toggle smooth transition ────────────────────────
    document.querySelectorAll('.theme-toggle-btn').forEach(function(btn){
      btn.addEventListener('click', function(){
        document.documentElement.style.transition = 'background .3s, color .3s';
      });
    });

    // ── Confirm delete buttons ────────────────────────────────
    document.querySelectorAll('[data-confirm]').forEach(function(btn){
      btn.addEventListener('click', function(e){
        if (!confirm(this.dataset.confirm || 'هل أنت متأكد؟')) e.preventDefault();
      });
    });

  });
})();
