/* =========================================================
   A&B HUB — Core interactions (vanilla JS, no backend)
   ========================================================= */
(function(){
  "use strict";
  
  
    /* ---------- Move Bootstrap modals to <body> ----------
     Prevents Bootstrap modal/backdrop issues when the layout
     container has CSS animations or transforms.
     Safe to run multiple times.
  ------------------------------------------------------ */
  document.querySelectorAll('.modal').forEach(function (modal) {
    if (modal.parentElement !== document.body) {
      document.body.appendChild(modal);
    }
  });
  

  var shell = document.querySelector('.ab-shell');

  /* ---------- Sidebar collapse (desktop) ---------- */
  var collapseBtn = document.getElementById('abCollapseBtn');
  if (collapseBtn && shell){
    var savedState = localStorage.getItem('ab-sidebar-collapsed');
    if (savedState === '1') shell.classList.add('collapsed');
    collapseBtn.addEventListener('click', function(){
      shell.classList.toggle('collapsed');
      localStorage.setItem('ab-sidebar-collapsed', shell.classList.contains('collapsed') ? '1' : '0');
    });
  }

  /* ---------- Mobile sidebar open/close ---------- */
  var mobileBtn = document.getElementById('abMobileMenuBtn');
  var overlay = document.getElementById('abSidebarOverlay');
  function closeMobile(){ shell && shell.classList.remove('mobile-open'); }
  if (mobileBtn && shell){
    mobileBtn.addEventListener('click', function(){ shell.classList.toggle('mobile-open'); });
  }
  if (overlay){ overlay.addEventListener('click', closeMobile); }

  /* ---------- Collapsible nav groups (sidebar accordions) ---------- */
  document.querySelectorAll('[data-ab-toggle="collapse"]').forEach(function(el){
    el.addEventListener('click', function(e){
      e.preventDefault();
      var targetSel = el.getAttribute('data-ab-target');
      var target = document.querySelector(targetSel);
      if (!target) return;
      var isOpen = target.classList.contains('show');
      target.classList.toggle('show');
      el.setAttribute('aria-expanded', String(!isOpen));
    });
  });

  /* ---------- Brand context switch (Homecare / Cleaner) ---------- */
  var ctxButtons = document.querySelectorAll('[data-ab-context]');
  var savedCtx = localStorage.getItem('ab-context') || 'homecare';
  function applyContext(ctx){
    document.body.classList.toggle('ctx-cleaner', ctx === 'cleaner');
    ctxButtons.forEach(function(b){ b.classList.toggle('active', b.getAttribute('data-ab-context') === ctx); });
    localStorage.setItem('ab-context', ctx);
  }
  if (ctxButtons.length){
    applyContext(savedCtx);
    ctxButtons.forEach(function(b){
      b.addEventListener('click', function(){ applyContext(b.getAttribute('data-ab-context')); });
    });
  }

  /* ---------- Generic table search / filter / pagination (client-side demo) ---------- */
  document.querySelectorAll('[data-ab-table]').forEach(function(wrapper){
    var input = wrapper.querySelector('[data-ab-table-search]');
    var rows = Array.prototype.slice.call(wrapper.querySelectorAll('tbody tr'));
    if (input){
      input.addEventListener('input', function(){
        var q = input.value.trim().toLowerCase();
        rows.forEach(function(r){
          var match = r.textContent.toLowerCase().indexOf(q) !== -1;
          r.style.display = match ? '' : 'none';
        });
      });
    }
    wrapper.querySelectorAll('[data-ab-filter-chip]').forEach(function(chip){
      chip.addEventListener('click', function(){
        wrapper.querySelectorAll('[data-ab-filter-chip]').forEach(function(c){ c.classList.remove('active'); });
        chip.classList.add('active');
        var val = chip.getAttribute('data-ab-filter-chip');
        rows.forEach(function(r){
          if (val === 'all'){ r.style.display = ''; return; }
          r.style.display = (r.getAttribute('data-status') === val) ? '' : 'none';
        });
      });
    });
  });

  /* ---------- Row checkbox "select all" ---------- */
  document.querySelectorAll('[data-ab-check-all]').forEach(function(master){
    master.addEventListener('change', function(){
      var table = master.closest('table');
      if (!table) return;
      table.querySelectorAll('tbody [data-ab-row-check]').forEach(function(cb){ cb.checked = master.checked; });
    });
  });

  /* ---------- Kanban drag & drop (vanilla) ---------- */
  document.querySelectorAll('.ab-kanban-card[draggable="true"]').forEach(function(card){
    card.addEventListener('dragstart', function(){ card.classList.add('dragging'); });
    card.addEventListener('dragend', function(){ card.classList.remove('dragging'); });
  });
  document.querySelectorAll('.ab-kanban-col-body').forEach(function(col){
    col.addEventListener('dragover', function(e){
      e.preventDefault();
      col.closest('.ab-kanban-col').classList.add('drag-over');
      var dragging = document.querySelector('.ab-kanban-card.dragging');
      if (dragging) col.appendChild(dragging);
    });
    col.addEventListener('dragleave', function(){ col.closest('.ab-kanban-col').classList.remove('drag-over'); });
    col.addEventListener('drop', function(){ col.closest('.ab-kanban-col').classList.remove('drag-over'); });
  });

  /* ---------- Toast helper ---------- */
  window.abToast = function(message, variant){
    variant = variant || 'primary';
    var container = document.getElementById('abToastContainer');
    if (!container) return;
    var el = document.createElement('div');
    el.className = 'toast align-items-center border-0 mb-2';
    el.style.background = '#fff';
    el.setAttribute('role','alert');
    el.innerHTML = '<div class="d-flex"><div class="toast-body small fw-semibold">'+message+'</div>' +
      '<button type="button" class="btn-close me-2 m-auto" data-bs-dismiss="toast"></button></div>';
    container.appendChild(el);
    var t = new bootstrap.Toast(el, { delay: 3500 });
    t.show();
    el.addEventListener('hidden.bs.toast', function(){ el.remove(); });
  };

  /* ---------- Enable all tooltips ---------- */
  document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(function(el){
    new bootstrap.Tooltip(el);
  });

})();
