(function(){
  function ready(fn){ if(document.readyState !== 'loading'){ fn(); } else { document.addEventListener('DOMContentLoaded', fn); } }
  ready(function(){
    var body = document.body;
    var sidebar = document.querySelector('.main-sidebar');
    if (sidebar && !document.querySelector('.mj-sidebar-backdrop')) {
      var backdrop = document.createElement('div');
      backdrop.className = 'mj-sidebar-backdrop';
      document.body.appendChild(backdrop);
      backdrop.addEventListener('click', function(){ body.classList.remove('mj-sidebar-open'); });
    }
    document.querySelectorAll('[data-mj-sidebar-toggle],[data-widget="pushmenu"]').forEach(function(btn){
      btn.addEventListener('click', function(ev){
        ev.preventDefault();
        if (window.matchMedia('(max-width: 991px)').matches) {
          body.classList.toggle('mj-sidebar-open');
        } else {
          body.classList.toggle('mj-sidebar-collapsed');
          try { localStorage.setItem('mj_admin_sidebar_collapsed', body.classList.contains('mj-sidebar-collapsed') ? '1' : '0'); } catch(e) {}
        }
      });
    });
    try { if (localStorage.getItem('mj_admin_sidebar_collapsed') === '1' && !window.matchMedia('(max-width: 991px)').matches) body.classList.add('mj-sidebar-collapsed'); } catch(e) {}
    document.querySelectorAll('.has-treeview > a.nav-link').forEach(function(link){
      link.addEventListener('click', function(ev){
        var href = (link.getAttribute('href') || '').trim();
        if (href === '' || href === '#') {
          ev.preventDefault();
          var parent = link.closest('.has-treeview');
          if (!parent) return;
          parent.classList.toggle('menu-open');
        }
      });
    });
    document.querySelectorAll('.nav-treeview .nav-link.active').forEach(function(active){
      var parent = active.closest('.has-treeview');
      if (parent) parent.classList.add('menu-open');
    });

    if (window.jQuery) {
      jQuery(function($){
        $('.mj-data-table').each(function(){
          if ($.fn.DataTable && !$.fn.DataTable.isDataTable(this)) {
            $(this).DataTable({responsive:true, pageLength:25, order:[], autoWidth:false});
          }
        });
        if ($.fn.tooltip) { $('[data-toggle="tooltip"]').tooltip(); }
      });
    }
  });
})();
async function loadStates(countryId, targetId){
  var target=document.getElementById(targetId); if(!target) return;
  target.innerHTML='<option value="">Loading...</option>';
  var base=(window.MJ_BASE_URL||'').replace(/\/$/,'');
  try { var r=await fetch(base+'/ajax/states.php?country_id='+encodeURIComponent(countryId)); target.innerHTML=await r.text(); }
  catch(e){ target.innerHTML='<option value="">Unable to load</option>'; }
}
async function loadCities(stateId, targetId){
  var target=document.getElementById(targetId); if(!target) return;
  target.innerHTML='<option value="">Loading...</option>';
  var base=(window.MJ_BASE_URL||'').replace(/\/$/,'');
  try { var r=await fetch(base+'/ajax/cities.php?state_id='+encodeURIComponent(stateId)); target.innerHTML=await r.text(); }
  catch(e){ target.innerHTML='<option value="">Unable to load</option>'; }
}
