/**
 * Fichier  : assets/js/wow-ui.js
 * Auteur   : KREMER Régis
 * Version  : 2.2.0
 * Date     : 2026-02-21
 * Description : JS — Composants WOW UI : boutons, cards, badges
 *
 * Changelog :
 *   v2.1.0 (2026-02-21) — Version initiale auditée et sécurisée
 *   v2.1.1 (2026-02-21) — Corrections sécurité, utils.js centralisé
 *   v2.2.0 (2026-02-21) — Permissions magasins, bons de commande, rapport d'écart,
 *                      import CSV, historique prix, bon de livraison multi-lignes
 */
// WOW UI V3 — view switches + density helpers (non-breaking)
(function(){
  const setActive=(root,btn)=>{
    root.querySelectorAll('.vs-btn').forEach(b=>b.classList.toggle('active', b===btn));
  };

  // Products view: grid/list/compact (CSS only, toggles data attr)
  const pv = document.getElementById('prodViewSwitch');
  if(pv){
    const key='sm_view_products';
    const body=document.body;
    const apply=(v)=>{
      body.setAttribute('data-products-view', v);
      localStorage.setItem(key,v);
    };
    const saved=localStorage.getItem(key);
    if(saved) apply(saved);
    pv.addEventListener('click',(e)=>{
      const btn=e.target.closest('.vs-btn');
      if(!btn) return;
      const v=btn.getAttribute('data-view');
      setActive(pv,btn);
      apply(v);
    });
    // sync active after load
    const cur=body.getAttribute('data-products-view')||'grid';
    const b=pv.querySelector(`.vs-btn[data-view="${cur}"]`);
    if(b) setActive(pv,b);
  }

  // Movements view: detailed/compact
  const mv = document.getElementById('mvViewSwitch');
  if(mv){
    const key='sm_view_movements';
    const body=document.body;
    const apply=(v)=>{
      body.setAttribute('data-movements-view', v);
      localStorage.setItem(key,v);
    };
    const saved=localStorage.getItem(key);
    if(saved) apply(saved);
    mv.addEventListener('click',(e)=>{
      const btn=e.target.closest('.vs-btn');
      if(!btn) return;
      const v=btn.getAttribute('data-view');
      setActive(mv,btn);
      apply(v);
    });
    const cur=body.getAttribute('data-movements-view')||'detailed';
    const b=mv.querySelector(`.vs-btn[data-view="${cur}"]`);
    if(b) setActive(mv,b);
  }
})();
