(()=>{const $=s=>document.querySelector(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);function structure(){return [...pole.options].find(o=>['STR','POL-STR'].includes(clean(o.dataset.code))||clean(o.textContent)==='STRUCTURE');}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='';$('#pcat-public').value='';}if(!editing){let 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||''):($('#pcat-public').selectedOptions[0]?.dataset.code||'');code.value=['TAR',clean(p).replace(/^POL-/,''),clean(d),clean(u)].filter(Boolean).join('-');}}['#pcat-category','#pcat-pole','#pcat-discipline','#pcat-public','#pcat-mode','#pcat-year','#pcat-label-mode'].forEach(s=>$(s)?.addEventListener('change',refresh));$('#pcat-course').addEventListener('change',e=>{let 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();}});document.querySelectorAll('.edit').forEach(b=>b.addEventListener('click',()=>{let x=JSON.parse(b.dataset.item);Object.entries({id:'id',category:'categorie',pole:'pole_id',discipline:'discipline_id',course:'atelier_id',public:'public_cible_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])=>{let e=$('#pcat-'+a.replace(/[A-Z]/g,m=>'-'+m.toLowerCase()));if(e)e.value=x[k]??''});$('#pcat-active').checked=+x.actif===1;$('#pcat-cancel').hidden=false;refresh();form.scrollIntoView({behavior:'smooth'});}));$('#pcat-cancel').addEventListener('click',()=>location.reload());refresh();})();
