(()=>{
 const $=s=>document.querySelector(s),$$=s=>[...document.querySelectorAll(s)],form=$('#pcat-form');if(!form)return;
 const mode=$('#pcat-mode'),amount=$('#pcat-amount'),code=$('#pcat-code'),category=$('#pcat-category'),pole=$('#pcat-pole'),year=$('#pcat-year'),labelMode=$('#pcat-label-mode'),label=$('#pcat-label');
 const clean=v=>(v||'').normalize('NFD').replace(/[\u0300-\u036f]/g,'').toUpperCase().replace(/[^A-Z0-9]+/g,'-').replace(/^-|-$/g,'').slice(0,24);
 const publics=()=>$$('.public-option input:checked');
 function structure(){return [...pole.options].find(o=>['STR','POL-STR'].includes(clean(o.dataset.code))||clean(o.textContent)==='STRUCTURE');}
 function publicSummary(){const selected=publics(),out=$('#pcat-public-summary');$$('.public-option').forEach(x=>x.classList.toggle('selected',x.querySelector('input').checked));out.textContent=selected.length?selected.map(x=>x.closest('label').querySelector('b').textContent).join(' + '):'Tous les publics / non applicable';}
 function refresh(){
  const editing=!!$('#pcat-id').value,adhesion=category.value==='adhesion',fromCourse=labelMode.value==='inscription';
  amount.disabled=mode.value==='libre';amount.required=mode.value!=='libre';year.required=adhesion;$('#pcat-year-field').hidden=!adhesion;label.required=labelMode.value!=='libre';label.disabled=labelMode.value==='libre';
  $('#pcat-label-help').textContent=fromCourse?'Le libellé final sera construit depuis l’inscription et le cours.':labelMode.value==='libre'?'Le libellé sera saisi directement dans la facture.':'Ce libellé sera proposé et pourra être modifié dans la facture.';
  if(adhesion&&!editing){const s=structure();if(s)pole.value=s.value;const active=[...year.options].find(o=>o.dataset.active==='1');if(active&&!year.value)year.value=active.value;labelMode.value='propose';label.disabled=false;label.required=true;label.value='Adhésion '+(year.selectedOptions[0]?.dataset.label||'annuelle');$('#pcat-unit').value='annuel';$('#pcat-discipline').value='';$('#pcat-course').value='';}
  if(!editing){const p=pole.selectedOptions[0]?.dataset.code||'POL',d=adhesion?'ADH':($('#pcat-discipline').selectedOptions[0]?.dataset.code||category.value),u=adhesion?(year.selectedOptions[0]?.dataset.label||''):publics().map(x=>x.dataset.code).join('-');code.value=['TAR',clean(p).replace(/^POL-/,''),clean(d),clean(u)].filter(Boolean).join('-');}
  publicSummary();
 }
 ['#pcat-category','#pcat-pole','#pcat-discipline','#pcat-mode','#pcat-year','#pcat-label-mode'].forEach(s=>$(s)?.addEventListener('change',refresh));
 $$('.public-option input').forEach(x=>x.addEventListener('change',refresh));
 $('#pcat-course').addEventListener('change',e=>{const o=e.target.selectedOptions[0];if(o?.value){pole.value=o.dataset.pole;$('#pcat-discipline').value=o.dataset.discipline;if(!$('#pcat-id').value){labelMode.value='inscription';label.value=o.dataset.label||'';}refresh();}});
 $$('.edit').forEach(b=>b.addEventListener('click',()=>{const x=JSON.parse(b.dataset.item);Object.entries({id:'id',category:'categorie',pole:'pole_id',discipline:'discipline_id',course:'atelier_id',year:'annee_scolaire_id',labelMode:'mode_libelle',label:'libelle',mode:'mode_tarif',amount:'montant',unit:'unite',code:'code',account:'compte_produit',start:'date_debut',end:'date_fin'}).forEach(([a,k])=>{const e=$('#pcat-'+a.replace(/[A-Z]/g,m=>'-'+m.toLowerCase()));if(e)e.value=x[k]??''});const selected=String(x.public_ids||x.public_cible_id||'').split(',');$$('.public-option input').forEach(e=>e.checked=selected.includes(e.value));$('#pcat-active').checked=+x.actif===1;$('#pcat-cancel').hidden=false;refresh();form.scrollIntoView({behavior:'smooth'});}));
 $('#pcat-cancel').addEventListener('click',()=>location.reload());refresh();
})();
