<?php
if(@$_SESSION['user_droits'][2]==2) {
	if(isset($_POST['validhor'.$tabevent_info['uid']])) {
		if(substr($tabevent_info['dtstart'],9,2).':'.substr($tabevent_info['dtstart'],11,2)!=$_POST['debut'.$tabevent_info['uid']]||substr($tabevent_info['dtend'],9,2).':'.substr($tabevent_info['dtend'],11,2)!=$_POST['fin'.$tabevent_info['uid']]) {
			$horcheck = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE hor_id = '".$tabevent_info['hor_id']."'"));
			if($_POST['debut'.$tabevent_info['uid']]>=substr($horcheck['fin'],0,5)||$_POST['fin'.$tabevent_info['uid']]<=substr($horcheck['debut'],0,5)||$_POST['fin'.$tabevent_info['uid']]<=$_POST['debut'.$tabevent_info['uid']]) {
				echo '<p class="signal">'.EDT_BRIK2.'</p>';
			}
			else {
				$dtstart = substr($tabevent_info['dtstart'],0,9).substr($_POST['debut'.$tabevent_info['uid']],0,2).substr($_POST['debut'.$tabevent_info['uid']],3,2).substr($tabevent_info['dtstart'],13,2);
				$dtend = substr($tabevent_info['dtend'],0,9).substr($_POST['fin'.$tabevent_info['uid']],0,2).substr($_POST['fin'.$tabevent_info['uid']],3,2).substr($tabevent_info['dtend'],13,2);
				if($_POST['debut'.$tabevent_info['uid']]==substr($horcheck['debut'],0,5)&&$_POST['fin'.$tabevent_info['uid']]==substr($horcheck['fin'],0,5)) { $spe = 0; }
				else { $spe = 1; }
				$deb = $_POST['debut'.$tabevent_info['uid']];
				$fin = $_POST['fin'.$tabevent_info['uid']];
				$dteStart = new DateTime($_POST['debut'.$tabevent_info['uid']]);
				$dteEnd = new DateTime($_POST['fin'.$tabevent_info['uid']]);
				include('edt_brikopt_duree.php');
				mysqli_query($bdd,"UPDATE ".$prefix."_edtevent SET specifique = '".$spe."', dtstart = '".$dtstart."', dtend = '".$dtend."', duration = '".$duree1."' WHERE uid = '".$tabevent_info['uid']."'");
				// Test d'événements en chevauchement (avec suppressions ou corrections) :
				$checkevent = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent edt INNER JOIN ".$prefix."_espaces_event eve ON edt.uid = eve.uid WHERE eve.espace_id = '".$tabevent_info['espace_id']."' AND (edt.dtstart < '".$dtend."' AND edt.dtstart > '".$dtstart."') AND edt.uid <> '".$tabevent_info['uid']."'");
				if(mysqli_num_rows($checkevent)==1) {
					$info = mysqli_fetch_assoc($checkevent);
					if($info['dtend']<=$dtend) {
						mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$info['uid']."'");
						mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$info['uid']."'");
						mysqli_query($bdd,"DELETE FROM ".$prefix."_edtevent WHERE uid = '".$info['uid']."'");
					}
					else {
						$dteStart = new DateTime($dtend);
						$dteEnd = new DateTime($info['dtend']);
						include('edt_brikopt_duree.php');
						mysqli_query($bdd,"UPDATE ".$prefix."_edtevent SET specifique = '".$spe."', dtstart = '".$dtend."', dtend = '".$info['dtend']."', duration = '".$duree1."' WHERE uid = '".$info['uid']."'");
					}
				}
				$checkevent = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent edt INNER JOIN ".$prefix."_espaces_event eve ON edt.uid = eve.uid WHERE eve.espace_id = '".$tabevent_info['espace_id']."' AND (edt.dtend > '".$dtstart."' AND edt.dtend < '".$dtend."') AND edt.uid <> '".$tabevent_info['uid']."'");
				if(mysqli_num_rows($checkevent)==1) {
					$info = mysqli_fetch_assoc($checkevent);
					if($info['dtstart']>=$dtstart) {
						mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$info['uid']."'");
						mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$info['uid']."'");
						mysqli_query($bdd,"DELETE FROM ".$prefix."_edtevent WHERE uid = '".$info['uid']."'");
					}
					else {
						$dteStart = new DateTime($info['dtstart']);
						$dteEnd = new DateTime($dtstart);
						include('edt_brikopt_duree.php');
						mysqli_query($bdd,"UPDATE ".$prefix."_edtevent SET specifique = '".$spe."', dtstart = '".$info['dtstart']."', dtend = '".$dtstart."', duration = '".$duree1."' WHERE uid = '".$info['uid']."'");
					}
				}
				echo '<script language="Javascript">
				<!--
				document.location.replace("edt.php");
				// -->
				</script>';
			}
		}
		else {
			echo '<p class="signal">'.EDT_BRIK3.'</p>';
		}
	}
	$edt_cell .= '<div name="noprint" class="noprint" style="position:relative;" data-html2canvas-ignore><a onmousedown="toggleDiv(\'param'.$tabevent_info['uid'].'\');"><img class="param_brik" src="./img/div/param.png" /></a><div name="noprint" id="param'.$tabevent_info['uid'].'" class="parambrik_edt" style="display:none;" data-html2canvas-ignore>'.EDT_BRIK4.'<br><input type="time" class="inptxt time" name="debut'.$tabevent_info['uid'].'" value="'.substr($tabevent_info['dtstart'],9,2).':'.substr($tabevent_info['dtstart'],11,2).'" /> à <input type="time" class="inptxt time" name="fin'.$tabevent_info['uid'].'" value="'.substr($tabevent_info['dtend'],9,2).':'.substr($tabevent_info['dtend'],11,2).'" /><p class="right"><input type="submit" name="validhor'.$tabevent_info['uid'].'" class="boutontab" value="'.VALID.'"></p> 
	<br>'.EDT_BRIK5.'<br>';
	$edt_cell .= '<input name="checkAll'.$tabevent_info['uid'].'" id="checkAll'.$tabevent_info['uid'].'" class="bud" type="checkbox" onclick="full()" /><span name="noprint"><label for="checkAll'.$tabevent_info['uid'].'">'.CHECKORNOT.'</label></span><br>';
	$edt_cell .= '<script type=\'text/javascript\'>
	$(\'#checkAll'.$tabevent_info['uid'].'\').click(function() {

	var elevs'.$tabevent_info['uid'].' = $("#elev'.$tabevent_info['uid'].'").find(\':checkbox\'); 
	   if(this.checked){ 
		  elevs'.$tabevent_info['uid'].'.prop(\'checked\', true); 
	   } else{ 
		  elevs'.$tabevent_info['uid'].'.prop(\'checked\', false);
	   }          
	});
	</script>';
	$edt_cell .= '<ul id="elev'.$tabevent_info['uid'].'">';
	//boucle pour chaque jour proposé en duplication :
	$jourDeb  = DateTime::createFromFormat('Y-m-d', $jouropentest);
	$jourFin = DateTime::createFromFormat('Y-m-d', $lastday);
	$in=0;
	foreach(new DatePeriod($jourDeb, new DateInterval('P7D') /* pas de 7 jours */, $jourFin) as $dtdup) {
		$datetest = $dtdup->format('Y-m-d');
		$dateweek = $dtdup->format('oW');
		$opencheck = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT statut FROM ".$prefix."_calendrier WHERE date = '".$datetest."'"));
		if(@$opencheck['statut']==1&&($datetest!=$jouropentest)) {
			$edt_cell .= '<input type="checkbox" id="dup'.$tabevent_info['uid'].$in.'" value="'.$datetest.'" /><label for="dup'.$tabevent_info['uid'].$in.'"> '.$datetest.'</label>';
			$edt_cell .= '<input type="hidden" id="dupw'.$tabevent_info['uid'].$in.'" value="'.$dateweek.'" /><br>';
			$in++;
		}
	}
	$edt_cell .= '</ul><input type="hidden" id="dupnb'.$tabevent_info['uid'].'" value="'.$in.'" />';
	$edt_cell .= '<p class="right"><element onclick="dupli'.$tabevent_info['uid'].'(this);" id="dupli'.$tabevent_info['uid'].'" class="boutontab" value="'.VALID.'">'.VALID.'</element></p></div></div>';
	$edt_cell .= '<script type="text/javascript">
		function dupli'.$tabevent_info['uid'].'(param) {
		var param = "dupli";
		var jour1 = "'.$jouropentest.'";
		var jour2 = "'.$lastday.'";
		var dtstart = "'.$tabevent_info['dtstart'].'";
		var dtend = "'.$tabevent_info['dtend'].'";
		var hor_id = "'.$tabevent_info['hor_id'].'";
		var hor_name = "'.$tabevent_info['hor_name'].'";
		var brik_id = "'.$tabevent_info['brik_id'].'";
		var duration = "'.$tabevent_info['duration'].'";
		var seq_id = "'.$tabevent_info['seq_id'].'";
		var specifique = "'.$tabevent_info['specifique'].'";
		var usercode = "'.$usercode.'";
		var espcode = "'.$espid.'";
		var couleur = "red 2px solid";
		var couleurw = "black 1px solid";
		var color = "red";
		var colorw = "black";';
		$in=0;
		foreach(new DatePeriod($jourDeb, new DateInterval('P7D') /* pas de 7 jours */, $jourFin) as $dtdup) {
			$datetest = $dtdup->format('Y-m-d');
			$datetest_var = $dtdup->format('Ymd');
			$dateweek = $dtdup->format('oW');
			$opencheck = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT statut FROM ".$prefix."_calendrier WHERE date = '".$datetest."'"));
			if(@$opencheck['statut']==1&&($datetest!=$jouropentest)) {
				$edt_cell .= ' var c'.$datetest_var.' = document.getElementById("dup'.$tabevent_info['uid'].$in.'").checked;
				if(c'.$datetest_var.'===false) {
					var t'.$datetest_var.' = "0";
				}
				else {
					var t'.$datetest_var.' = "'.$datetest.'";
				}';
				$edt_cell .= ' var t'.$dateweek.' = "'.$dateweek.'";';
				$in++;
			}		
		}
		$edt_cell .= ' var nombre = document.getElementById("dupnb'.$tabevent_info['uid'].'").value;';
		$edt_cell .= ' document.getElementById("dupli'.$tabevent_info['uid'].'").style.border = couleur; 		document.getElementById("dupli'.$tabevent_info['uid'].'").style.color = color;';
		$edt_cell .= '$.ajax({
		    type: "POST",
		    data: { param: param, jour1: jour1, jour2: jour2, dtstart: dtstart, dtend: dtend, hor_id: hor_id, hor_name: hor_name, brik_id: brik_id, duration: duration, seq_id: seq_id, specifique: specifique, usercode: usercode, espcode: espcode, nombre: nombre';
		    foreach(new DatePeriod($jourDeb, new DateInterval('P7D') /* pas de 7 jours */, $jourFin) as $dtdup) {
				$datetest = $dtdup->format('Y-m-d');
				$datetest_var = $dtdup->format('Ymd');
				$dateweek = $dtdup->format('oW');
				$opencheck = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT statut FROM ".$prefix."_calendrier WHERE date = '".$datetest."'"));
				if(@$opencheck['statut']==1&&($datetest!=$jouropentest)) {
					$edt_cell .= ', t'.$datetest_var.': t'.$datetest_var.', t'.$dateweek.': t'.$dateweek.' ';
				}
			}
		    $edt_cell .= ' },
		    url: "js/idl/edt_brikopt.php",
		   });';
		$edt_cell .= ' setTimeout(() => {  
			document.getElementById("dupli'.$tabevent_info['uid'].'").style.color = colorw; 
			document.getElementById("dupli'.$tabevent_info['uid'].'").style.border = couleurw;
			}, 2000); ';
		$edt_cell .= ' }
	</script>';

}
$edt_cell .= '</div>';
?>