<?php
echo '<form method="POST" action="" name="msgform'.$note.'">
<input type="hidden" name="agenda_id" value="'.$note.'" />
<table class="agenda_form">
<tr><td>';
if($note!='0') { 	echo NID.' '.$blocnote['agenda_id'].' - '; }
echo TITLE.DOTS.'</td><td> <input type="text" class="inptxt" max="200" name="titre'.$note.'" value="';
if($note!='0') { 	echo $blocnote['titre']; }
echo '" /></td></tr>
<tr><td>'.FREQC_JOUR4.'</td><td><input type="date" name="date1'.$note.'" class="inptxt" value="';
if($note!='0') { 	echo $blocnote['date1']; }
echo '" /></td></tr><tr><td>'.PARAM_CAL2.'</td><td><input type="date" name="date2'.$note.'" class="inptxt" value="';
if($note!='0') { 	echo $blocnote['date2']; }
echo '" /></td></tr>';
if($note!='0') {
	echo '<tr><td colspan="2">'.CREATOR.DOTS.' ';
	$creat = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT id, login, nom, prenom FROM ".$prefix."_user WHERE id = '".$blocnote['creator']."' LIMIT 1"));
	echo $creat['login']; 
}
echo '<tr><td colspan="2">'.PARANAV2.DOTS;
$usrcs = mysqli_query($bdd,"SELECT id, login, nom, prenom FROM ".$prefix."_user ORDER BY login ASC");
foreach($usrcs as $usrc) {
	echo '<input type="checkbox" name="'.$note.'usr'.$usrc['id'].'" id="'.$note.'usr'.$usrc['id'].'" ';
	if($note == '0') {
		if($usrc['id']==$_SESSION['user_id']) { echo 'checked '; }
	}
	else {
		if(in_array($usrc['id'],$uses)) { echo 'checked '; }
	}
	echo ' /> <label for="'.$note.'usr'.$usrc['id'].'"> '.$usrc['login'].'</label> ';
}
echo '</td></tr>';
if($espopt['valeur']=='1') {
	echo '<tr><td colspan="2">'.PARANAV8.DOTS;
	$espcs = mysqli_query($bdd,"SELECT espace_id, espace_nom FROM ".$prefix."_espaces ORDER BY espace_id ASC");
	$espfirst = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first'"));
	echo '<input type="checkbox" name="'.$note.'esp0" id="'.$note.'esp0"';
	if($note=='0') { echo ' checked '; }
	else {
		if(in_array('0',$esps)) { echo 'checked '; }
	}
	echo ' /> <label for="'.$note.'esp0"> '.$espfirst['valeur'].'</label> ';
	foreach($espcs as $espc) {
		echo '<input type="checkbox" name="'.$note.'esp'.$espc['espace_id'].'" id="'.$note.'usr'.$espc['espace_id'].'" ';
		if($note != '0') {
			if(in_array($espc['espace_id'],$esps)) { echo 'checked '; }
		}
		echo '/> <label for="'.$note.'usr'.$espc['espace_id'].'"> '.$espc['espace_nom'].'</label> ';
	}
	echo '</td></tr>';
}
echo '<tr><td>'.TEXT.DOTS.'</td><td>
<textarea name="texte'.$note.'" id="texte'.$note.'" rows="10" cols="60" class="tinymce">';
if($note!='0') { 	echo $blocnote['texte']; }
echo '</textarea></td></tr>
<tr><td>'.NOTES.DOTS.'</td><td>
<textarea name="notes'.$note.'" id="notes'.$note.'" rows="10" cols="60" class="tinymce">';
if($note!='0') { 	echo $blocnote['notes']; }
echo '</textarea></td></tr>
<tr><td>'.OPTIONS.DOTS.'</td>
<td><input type="checkbox" name="afaire'.$note.'" id="afaire'.$note.'" ';
if($note!='0') { 	if($opts[0]=='1') { echo ' checked '; } }
echo '/> <label for="afaire'.$note.'"> '.EDTC_AGE6.'</label>
<br><input type="checkbox" name="fait'.$note.'" id="fait'.$note.'" ';
if($note!='0') { 	if($opts[1]=='1') { echo ' checked '; } }
echo '/> <label for="fait'.$note.'"> '.EDTC_AGE7.'</label>
<br><input type="checkbox" name="edtday'.$note.'" id="edtday'.$note.'" ';
if($note!='0') { 	if($opts[2]=='1') { echo ' checked '; } }
echo '/> <label for="edtday'.$note.'"> '.EDTC_AGE8.'</label>
<br><input type="checkbox" name="edtaff'.$note.'" id="edtaff'.$note.'" ';
if($note!='0') { 	if($opts[3]=='1') { echo ' checked '; } }
echo '/> <label for="edtaff'.$note.'"> '.EDTC_AGE9.'</label>
<br><input type="checkbox" name="edtpageresa'.$note.'" id="edtpageresa'.$note.'" ';
if($note!='0') { 	if($opts[4]=='1') { echo ' checked '; } }
echo '/> <label for="edtpageresa'.$note.'"> '.EDTC_AGE10.'</label>
<br><input type="checkbox" name="modifiable" id="modifiable'.$note.'" ';
if($note!='0') { 	if($opts[5]=='1') { echo ' checked '; } }
echo '/> <label for="modifiable'.$note.'"> '.EDTC_AGE11.'</label></td></tr>';

echo '<tr><td colspan="2" class="right">&nbsp; <input class="bouton boutparam" type="submit" id="supprBtn'.$note.'" name="supprevent" value="'.SUPPR.'" /> <input class="bouton boutparam" type="submit" id="submitBtn'.$note.'" name="validevent" value="'.REGISTER.'" disabled /><p id="formMessage'.$note.'" class="signal right" style="display: none;">'.EDTC_AGE12.'</p></td></tr></table></form><br>';
?>
<script>
	document.addEventListener('DOMContentLoaded', function() {
	    const form = document.forms['msgform<?php echo $note; ?>']; // Formulaire dynamique
	    const titre = document.querySelector('input[name="titre<?php echo $note; ?>"]');
	    const date1 = document.querySelector('input[name="date1<?php echo $note; ?>"]');
	    const date2 = document.querySelector('input[name="date2<?php echo $note; ?>"]');
	    const submitBtn = document.getElementById('submitBtn<?php echo $note; ?>');
	    const formMessage = document.getElementById('formMessage<?php echo $note; ?>');

	    function isValidDate(dateString) {
	        const date = new Date(dateString);
	        return !isNaN(date.getTime()) && /^\d{4}-\d{2}-\d{2}$/.test(dateString);
	    }

	    function checkForm() {
	        let errors = [];

	        const titreValide = titre.value.trim() !== '';
	        const date1Valide = isValidDate(date1.value);

	        if (!date1Valide||!titreValide) {
	            errors.push("<?php echo addslashes(EDTC_AGE12); ?>");
	        }

	        if (date2.value.trim() !== '') {
	            const date2Valide = isValidDate(date2.value);
	            if (!date2Valide) {
	                errors.push("<?php echo addslashes(EDTC_AGE13); ?>");
	            } else if (date1Valide && new Date(date2.value) <= new Date(date1.value)) {
	                errors.push("<?php echo addslashes(EDTC_AGE13); ?>");
	            }
	        }

	        if (errors.length === 0) {
	            submitBtn.disabled = false;
	            formMessage.style.display = 'none';
	            formMessage.innerHTML = '';
	        } else {
	            submitBtn.disabled = true;
	            formMessage.style.display = 'block';
	            formMessage.innerHTML = errors.join('<br>');
	        }
	    }

	    // Surveiller les changements
	    titre.addEventListener('input', checkForm);
	    date1.addEventListener('input', checkForm);
	    date2.addEventListener('input', checkForm);

	    // Vérification initiale
	    checkForm();

	    // Intercepter la soumission du formulaire
	    form.addEventListener('submit', function(e) {
	        checkForm();
	        if (submitBtn.disabled) {
	            e.preventDefault(); // Bloquer l'envoi si bouton désactivé
	        }
	    });
	});
</script>