(()=>{const $=s=>document.querySelector(s),form=$('#pcat-form'),mode=$('#pcat-mode'),amount=$('#pcat-amount'),code=$('#pcat-code');if(!form)return;const clean=v=>(v||'').normalize('NFD').replace(/[\u0300-\u036f]/g,'').toUpperCase().replace(/[^A-Z0-9]+/g,'-').replace(/^-|-$/g,'').slice(0,24);function refresh(){amount.disabled=mode.value==='libre';amount.required=mode.value!=='libre';if(!$('#pcat-id').value){let p=$('#pcat-pole').selectedOptions[0]?.dataset.code||'POL',d=$('#pcat-discipline').selectedOptions[0]?.dataset.code||$('#pcat-category').value,u=$('#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'].forEach(s=>$(s).addEventListener('change',refresh));$('#pcat-course').addEventListener('change',e=>{let o=e.target.selectedOptions[0];if(o?.value){$('#pcat-pole').value=o.dataset.pole;$('#pcat-discipline').value=o.dataset.discipline;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',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);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();})();
