(()=>{const dialog=document.querySelector('#pp-dialog'),calendar=document.querySelector('#pp-calendar'),daySelect=document.querySelector('#pp-day-select'),discipline=document.querySelector('#pp-discipline'),type=document.querySelector('#pp-type'),start=document.querySelector('#pp-start'),end=document.querySelector('#pp-end'),duration=document.querySelector('#pp-duration'),preview=document.querySelector('#pp-preview'),capacity=document.querySelector('#pp-capacity-wrap');let current='lundi';const minutes=v=>{const p=v.split(':').map(Number);return p[0]*60+p[1]},clock=v=>String(Math.floor(v/60)).padStart(2,'0')+':'+String(v%60).padStart(2,'0');function refresh(){const option=discipline.selectedOptions[0];const size=+(type.value==='individuel'?option?.dataset.individual:option?.dataset.collective)||30;duration.textContent='Durée prévue : '+size+' minutes par cours';capacity.hidden=type.value==='individuel';const from=minutes(start.value),to=minutes(end.value);if(to<=from){preview.textContent='Choisissez une heure de fin postérieure.';return}if(type.value==='collectif'){preview.textContent='Un bloc collectif de '+start.value+' à '+end.value+'.';return}const count=(to-from)/size;preview.textContent=Number.isInteger(count)?count+' créneau(x) généré(s) : '+Array.from({length:count},(_,i)=>clock(from+i*size)+'–'+clock(from+(i+1)*size)).join(' · '):'La plage doit être divisible par '+size+' minutes.'}function selectDay(day){current=day;calendar.classList.remove('week');document.querySelectorAll('[data-day]').forEach(x=>x.classList.toggle('active',x.dataset.day===day));document.querySelectorAll('[data-day-column]').forEach(x=>x.classList.toggle('active',x.dataset.dayColumn===day));daySelect.value=day}document.querySelectorAll('.pp-days button').forEach(b=>b.addEventListener('click',()=>selectDay(b.dataset.day)));document.querySelector('#pp-week').addEventListener('click',()=>calendar.classList.toggle('week'));document.querySelector('#pp-new').addEventListener('click',()=>{daySelect.value=current;refresh();dialog.showModal()});document.querySelector('#pp-close').addEventListener('click',()=>dialog.close());document.querySelectorAll('.pp-grid').forEach(grid=>grid.addEventListener('click',e=>{if(e.target.closest('.pp-slot'))return;const rect=grid.getBoundingClientRect(),m=Math.max(0,Math.min(840,Math.round((e.clientY-rect.top)/15)*15));current=grid.dataset.day;daySelect.value=current;start.value=clock(8*60+m);end.value=clock(Math.min(22*60,8*60+m+120));refresh();dialog.showModal()}));[discipline,type,start,end].forEach(x=>x.addEventListener('change',refresh));refresh()})();
