(()=>{const $=s=>document.querySelector(s),form=$('#reduction-form');if(!form)return;const condition=$('#red-condition'),type=$('#red-type'),value=$('#red-value'),free=$('#red-free'),code=$('#red-code'),label=$('#red-label'),threshold=$('#red-threshold'),id=$('#red-id');const initial={start:form.date_debut.value,end:form.date_fin.value};const clean=v=>(v||'').normalize('NFD').replace(/[\u0300-\u036f]/g,'').toUpperCase().replace(/[^A-Z0-9]+/g,'-').replace(/^-|-$/g,'').slice(0,40);function refresh(){const exceptional=condition.value==='exceptionnelle';$('#red-free-field').hidden=!exceptional;if(!exceptional)free.checked=false;value.disabled=free.checked;value.required=!free.checked;$('#red-value-field').style.opacity=free.checked?'.45':'1';$('#red-unit').textContent=type.value==='pourcentage'?'%':'€';if(!code.dataset.manual)code.value=exceptional&&free.checked?'EXCEPTIONNELLE-LIBRE':clean(label.value||condition.selectedOptions[0].textContent);}function reset(){form.reset();id.value='';form.date_debut.value=initial.start;form.date_fin.value=initial.end;$('#red-active').checked=true;$('#red-cumulable').checked=true;$('#red-mode').textContent='NOUVELLE RÈGLE';$('#red-title').textContent='Configurer la réduction';$('#red-save').textContent='Enregistrer la réduction';$('#red-cancel').hidden=true;delete code.dataset.manual;refresh();}function edit(d){id.value=d.id;condition.value=d.condition_type;label.value=d.libelle;type.value=d.type_reduction;value.value=d.valeur;free.checked=!!d.free;threshold.value=d.seuil||'';form.priorite.value=d.priorite;form.date_debut.value=d.date_debut||'';form.date_fin.value=d.date_fin||'';form.commentaire.value=d.commentaire||'';code.value=d.code;code.dataset.manual='1';$('#red-cumulable').checked=!!d.cumulable;$('#red-active').checked=!!d.actif;$('#red-mode').textContent='MODIFICATION';$('#red-title').textContent='Modifier la réduction';$('#red-save').textContent='Enregistrer les modifications';$('#red-cancel').hidden=false;refresh();form.scrollIntoView({behavior:'smooth',block:'start'});}condition.addEventListener('change',refresh);type.addEventListener('change',refresh);free.addEventListener('change',refresh);label.addEventListener('input',refresh);code.addEventListener('input',()=>code.dataset.manual='1');$('#red-cancel').addEventListener('click',reset);document.querySelectorAll('.edit-rule').forEach(b=>b.addEventListener('click',()=>edit(JSON.parse(b.dataset.rule))));const templates={second:{condition:'plusieurs_membres',label:'Réduction troisième enfant',type:'montant_fixe',threshold:3,value:20},multi:{condition:'multi_activites',label:'Réduction seconde activité',type:'montant_fixe',threshold:2,value:10},territory:{condition:'autre',label:'Autre réduction',type:'montant_fixe',threshold:'',value:''},exceptional:{condition:'exceptionnelle',label:'Réduction exceptionnelle à définir',type:'montant_fixe',threshold:'',free:true,value:0}};document.querySelectorAll('[data-template]').forEach(b=>b.addEventListener('click',()=>{reset();const t=templates[b.dataset.template];condition.value=t.condition;label.value=t.label;type.value=t.type;threshold.value=t.threshold;value.value=t.value;free.checked=!!t.free;delete code.dataset.manual;refresh();form.scrollIntoView({behavior:'smooth'});}));$('#red-search')?.addEventListener('input',e=>document.querySelectorAll('.rule-list article').forEach(a=>a.hidden=!a.dataset.search.includes(e.target.value.toLowerCase())));refresh();})();
