(()=>{document.querySelectorAll('[data-sign-toggle]').forEach(b=>b.addEventListener('click',()=>{const f=document.querySelector(`[data-sign-form="${b.dataset.signToggle}"]`);if(f)f.hidden=!f.hidden;}));document.querySelectorAll('.sign-form,.public-sign-form').forEach(form=>{const canvas=form.querySelector('canvas'),out=form.querySelector('[data-signature-output]'),ctx=canvas?.getContext('2d');if(!canvas||!ctx||!out)return;ctx.lineWidth=2.4;ctx.lineCap='round';ctx.strokeStyle='#102449';let draw=false;const point=e=>{const r=canvas.getBoundingClientRect(),p=e.touches?e.touches[0]:e;return[(p.clientX-r.left)*canvas.width/r.width,(p.clientY-r.top)*canvas.height/r.height]};const start=e=>{draw=true;ctx.beginPath();ctx.moveTo(...point(e));e.preventDefault()};const move=e=>{if(!draw)return;ctx.lineTo(...point(e));ctx.stroke();e.preventDefault()};const stop=()=>{draw=false;out.value=canvas.toDataURL('image/png')};canvas.addEventListener('pointerdown',start);canvas.addEventListener('pointermove',move);window.addEventListener('pointerup',stop);form.querySelector('[data-clear]')?.addEventListener('click',()=>{ctx.clearRect(0,0,canvas.width,canvas.height);out.value=''});form.addEventListener('submit',e=>{if(!out.value){e.preventDefault();alert('Veuillez signer le document.')}});});})();
