/**
 * Fichier  : assets/js/ui-v2.js
 * Auteur   : KREMER Régis
 * Version  : 2.2.0
 * Date     : 2026-02-21
 * Description : JS — Composants UI v2 : modals, toasts, dropdowns
 *
 * 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
 */
// ================================================================
// ui-v2.js — UI layer (no business logic)
// - Density toggle (comfortable/compact)
// - View switches (grid/list/compact) — CSS driven
// - Small helpers (focus page search)
// ================================================================
(function(){
  const LS = {
    density: 'sm_density',
    view: (page)=> `sm_view_${page}`
  };

  function setDensity(mode){
    const body = document.body;
    if(mode === 'compact') body.classList.add('sm-compact');
    else body.classList.remove('sm-compact');
    localStorage.setItem(LS.density, mode);
    // reflect UI
    document.querySelectorAll('[data-density-btn]').forEach(b=>{
      b.classList.toggle('active', b.getAttribute('data-density-btn')===mode);
    });
  }

  function initDensity(){
    const saved = localStorage.getItem(LS.density) || 'comfortable';
    setDensity(saved);
    document.querySelectorAll('[data-density-btn]').forEach(b=>{
      b.addEventListener('click', ()=> setDensity(b.getAttribute('data-density-btn')));
    });
  }

  function setView(page, view){
    const target = document.querySelector('[data-view-target]');
    if(!target) return;
    target.classList.remove('view-grid','view-list','view-compact');
    target.classList.add(`view-${view}`);
    localStorage.setItem(LS.view(page), view);
    document.querySelectorAll('[data-view-btn]').forEach(b=>{
      b.classList.toggle('active', b.getAttribute('data-view-btn')===view);
    });
  }

  function initView(){
    const page = window.SM_PAGE || 'default';
    const target = document.querySelector('[data-view-target]');
    const btns = document.querySelectorAll('[data-view-btn]');
    if(!target || !btns.length) return;

    // ensure default class
    if(!target.classList.contains('view-grid') && !target.classList.contains('view-list') && !target.classList.contains('view-compact')){
      target.classList.add('view-grid');
    }

    const saved = localStorage.getItem(LS.view(page));
    if(saved) setView(page, saved);

    btns.forEach(b=>{
      b.addEventListener('click', ()=> setView(page, b.getAttribute('data-view-btn')));
    });
  }

  function initFocusSearch(){
    const input = document.getElementById('smGlobalSearch');
    if(!input) return;

    input.addEventListener('keydown', (e)=>{
      if(e.key === 'Enter'){
        // Try to forward to page search if exists
        const pageSearch = document.querySelector('#prodSearch, #mvSearch, input.inp-search, input[type="search"], input[placeholder*="Rechercher"]');
        if(pageSearch){
          pageSearch.focus();
          pageSearch.value = input.value;
          pageSearch.dispatchEvent(new Event('input', {bubbles:true}));
        }
      }
    });
  }

  document.addEventListener('DOMContentLoaded', ()=>{
    initDensity();
    initView();
    initFocusSearch();
  });
})();
