<?php 
$tinymce[$i]=0;
if($txt_type[$i]=="seq_nom"||$txt_type[$i]=="auteur"||$txt_type[$i]=="url") {
	echo '<input type="text" class="inptxt" style="margin-left:80px;" size="38" onkeyup="selection(this,\'t_'.$txt_type[$i].'\');" name="'.$txt_type[$i].'" id="t_'.$txt_type[$i].'"';
	if($txt_type[$i]=="seq_nom") { echo ' maxlength="100" '; }
	else if($txt_type[$i]=="auteur") { echo ' maxlength="300" '; }
	else if($txt_type[$i]=="url") { echo ' maxlength="500" '; }
	echo ' value="'; if($seq_info[$txt_type[$i]]!='') { echo $seq_info[$txt_type[$i]]; } echo '" />';
}
else if($txt_type[$i]=="date_crea"||$txt_type[$i]=="date_maj") {
	echo '<input type="date" class="inptxt" style="margin-left:200px;" onkeyup="selection(this,\'t_'.$txt_type[$i].'\');" name="'.$txt_type[$i].'" id="t_'.$txt_type[$i].'" value="'; if($txt_type[$i]=="date_maj") { echo date('Y-m-d'); } else if($seq_info[$txt_type[$i]]!='') { echo $seq_info[$txt_type[$i]]; }  echo '" />';
}
else if($txt_type[$i]=="niveau") {
	echo '<div class="box"><select name=\'niveau\' id=\'niveaugo\' onchange=\'go()\'>';
		echo '<option value="-1">'.NONE.'</option>';
		include('db/proc/freq_stats_nivcolors.php');
		for($no=0;$no<count($tabDebniveau);$no++) {
			echo '<option value="'.$tabDebniveau[$no].'"';
			if($seq_info['niveau']==$tabDebniveau[$no]) { echo ' selected '; }
			echo '>'.$tabNiveau[$no].'</option>';
		}
		echo '</select></div>';
}
else if($txt_type[$i]=="progression") {
	echo '<input type="number" name="progression" min="1" max="100" id="t_'.$txt_type[$i].'" class="number" value="';
			if($seq_info['progression']!='1000') { echo $seq_info['progression']; }
			echo '" /><div id=\'progress\'><br>';
		$liste_progress = mysqli_query($bdd,"SELECT progression, seq_nom, seq_id FROM ".$prefix."_sequences WHERE niveau = '".$seq_info['niveau']."' AND progression <> '1000' ORDER BY progression");
		$liste_nb = mysqli_num_rows($liste_progress);
		if($liste_nb>0) {
			echo EDIT_NIV.'<br>';
			while($liste = mysqli_fetch_assoc($liste_progress)) {
				echo $liste['progression'].'. <a href="edit.php?page=consult&seq_id='.$liste['seq_id'].'">'.$liste['seq_nom'].'</a><br>';
			}
		}
		echo '</div>';
}
else if($txt_type[$i]=="SAVOIRS") {
	echo'<br><hr class="hr2n"> <h4>'.EDIT_SAVID1.'</h4>';
	if (
	    $seq_info['niveau'] != 'a' &&
	    (str_starts_with($seq_info['niveau'], '1') ||
	     str_starts_with($seq_info['niveau'], '2') ||
	     str_starts_with($seq_info['niveau'], '3') ||
	     str_starts_with($seq_info['niveau'], '4') ||
	     str_starts_with($seq_info['niveau'], '5') ||
	     str_starts_with($seq_info['niveau'], '6') ||
	     str_starts_with($seq_info['niveau'], 't'))
	) {
		$seqsav_select = 
		"SELECT seqsav.s_id, sav.nom_savoir savoir, SUBSTR(seqsav.s_id,7,3) sidsub FROM ".$prefix."_seqsav seqsav 
		INNER JOIN ".$prefix."_savoirs".$seq_info['niveau']." sav
		ON seqsav.s_id = sav.s".$seq_info['niveau']."_id
		WHERE seqsav.seq_id = '".$seq_info['seq_id']."' AND seqsav.s_id LIKE 'APD%'
		ORDER BY seqsav.s_id";
		$seqsav = mysqli_query($bdd,$seqsav_select);
		$seqsav_nb = mysqli_num_rows($seqsav);
		if($seqsav_nb>0) {
			echo '<ul>';
			while($liste_sav = mysqli_fetch_assoc($seqsav)) {
				echo '<li> '.$liste_sav['sidsub'].' : '.$liste_sav['savoir'].'</li>';
			}
			echo '</ul>';
		}
	}
	echo '<div class="right"><input class="bouton boutseq bsright" type="button" name="savapd_'.$txt_type[$i].'" id="savapd'.$txt_type[$i].'" value="'.VALID.'" onclick="valapd'.$txt_type[$i].'(this,\'APD\',\''.$seq_info['seq_id'].'\'); validcolor(\'savapd'.$txt_type[$i].'\');" /><br></div>';
	echo '<div id=\'savoir1\' style=\'display:inline\'>';
	include('edit_select_sav_def.php');
	echo'</div>';
	
	if($nb_sav_emi>0) {
		echo'<br>&nbsp; <br>&nbsp; <br><hr class="hr2n"> <h4>'.EDIT_SAVID2.'</h4>';
		if (
		    $seq_info['niveau'] != 'a' &&
		    (str_starts_with($seq_info['niveau'], '1') ||
		     str_starts_with($seq_info['niveau'], '2') ||
		     str_starts_with($seq_info['niveau'], '3') ||
		     str_starts_with($seq_info['niveau'], '4') ||
		     str_starts_with($seq_info['niveau'], '5') ||
		     str_starts_with($seq_info['niveau'], '6') ||
		     str_starts_with($seq_info['niveau'], 't'))
		) {
			$seqemi_select = 
			"SELECT seqsav.s_id, sav.nom_savoir savoir, SUBSTR(seqsav.s_id,7,3) sidsub FROM ".$prefix."_seqsav seqsav 
			INNER JOIN ".$prefix."_savoirs".$seq_info['niveau']." sav
			ON seqsav.s_id = sav.s".$seq_info['niveau']."_id
			WHERE seqsav.seq_id = '".$seq_info['seq_id']."' AND seqsav.s_id LIKE 'EMI%'
			ORDER BY seqsav.s_id";
			$seqemi = mysqli_query($bdd,$seqemi_select);
			$seqemi_nb = mysqli_num_rows($seqemi);
			if($seqemi_nb>0) {
				echo '<ul>';
				while($liste_emi = mysqli_fetch_assoc($seqemi)) {
					echo '<li> '.$liste_emi['sidsub'].' : '.$liste_emi['savoir'].'</li>';
				}
				echo '</ul>';
			}
		}
		echo '<div class="right"><input class="bouton boutseq bsright" type="button" name="savemi_'.$txt_type[$i].'" id="savemi'.$txt_type[$i].'" value="'.VALID.'" onclick="valemi'.$txt_type[$i].'(this,\'EMI\',\''.$seq_info['seq_id'].'\'); validcolor(\'savemi'.$txt_type[$i].'\');" /><br></div><br>';
		echo '<div id=\'emi1\' style=\'display:inline\'>';
		include('edit_select_emi_def.php');
		echo '</div><br>';
	
	}
	if($nb_sav_per>0) {
		echo'<br><hr class="hr2n"> <h4>'.EDIT_SAVID3.'</h4>';
		$ccr = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'eval_perso'");
		$ccr_info = mysqli_fetch_assoc($ccr);
		if($ccr_info['valeur']=="niveau") {
			if (
			    $seq_info['niveau'] != 'a' &&
			    (str_starts_with($seq_info['niveau'], '1') ||
			     str_starts_with($seq_info['niveau'], '2') ||
			     str_starts_with($seq_info['niveau'], '3') ||
			     str_starts_with($seq_info['niveau'], '4') ||
			     str_starts_with($seq_info['niveau'], '5') ||
			     str_starts_with($seq_info['niveau'], '6') ||
			     str_starts_with($seq_info['niveau'], 't'))
			) {
				$seqper_select = 
				"SELECT seqsav.s_id, sav.nom_savoir savoir, SUBSTR(seqsav.s_id,-3) sidsub FROM ".$prefix."_seqsav seqsav 
				INNER JOIN ".$prefix."_savoirs".$seq_info['niveau']." sav
				ON seqsav.s_id = sav.s".$seq_info['niveau']."_id
				WHERE seqsav.seq_id = '".$seq_info['seq_id']."' AND seqsav.s_id LIKE 'PER%'
				ORDER BY seqsav.s_id";
				$seqper = mysqli_query($bdd,$seqper_select);
				$seqper_nb = mysqli_num_rows($seqper);
				if($seqper_nb>0) {
					echo '<ul>';
					while($liste_per = mysqli_fetch_assoc($seqper)) {
						echo '<li> '.$liste_per['sidsub'].' : '.$liste_per['savoir'].'</li>';
					}
					echo '</ul>';
				}
			}
		}
		else if($ccr_info['valeur']=="global") {
			$tabDebniveau = array('6', '5', '4', '3', '2', '1', 't');
			foreach($tabDebniveau as $divdeb) {
				$seqper_select = 
				"SELECT seqsav.s_id, sav.nom_savoir savoir, SUBSTR(seqsav.s_id,4,1) niv, SUBSTR(seqsav.s_id,-3) sidsub FROM ".$prefix."_seqsav seqsav 
				INNER JOIN ".$prefix."_savoirs".$divdeb." sav
				ON seqsav.s_id = sav.s".$divdeb."_id
				WHERE seqsav.seq_id = '".$seq_info['seq_id']."' AND seqsav.s_id LIKE 'PER%'
				ORDER BY seqsav.s_id";
				$seqper = mysqli_query($bdd,$seqper_select);
				$seqper_nb = mysqli_num_rows($seqper);
				if($seqper_nb>0) {
					echo '<ul>';
					foreach($seqper as $liste_per) {
						echo '<li> '.$liste_per['niv'].'e - '.$liste_per['sidsub'].' : '.$liste_per['savoir'].'</li>';
					}
					echo '</ul>';
				}
			}
		}
		echo '<div class="right"><input class="bouton boutseq bsright" type="button" name="savper_'.$txt_type[$i].'" id="savper'.$txt_type[$i].'" value="'.VALID.'" onclick="valper'.$txt_type[$i].'(this,\'PER\',\''.$seq_info['seq_id'].'\'); validcolor(\'savper'.$txt_type[$i].'\');" /><br></div><br>';
		echo '<div id=\'per1\' style=\'display:inline\'>';
		include('edit_select_per_def.php');
		echo '</div><br>';
	}
	echo '<script type="text/javascript">
	function valapd'.$txt_type[$i].'(savs,item,seq_id) { 
		var items = new Array();
		$(\'#selectdef :selected\').each(function(i, selected) {
		  items[i] = $(selected).val();
		});
		var savs = JSON.stringify(items);

	  	$.ajax({
	    type: "POST",
	    data: { savs: savs, item: item, seq_id: seq_id },
	    url: "js/idl/seq_itemsav_modif.php",
	   });
	}
	function valemi'.$txt_type[$i].'(savs,item,seq_id) { 
		var items = new Array();
		$(\'#savemis :selected\').each(function(i, selected) {
		  items[i] = $(selected).val();
		});
		var savs = JSON.stringify(items);

	  	$.ajax({
	    type: "POST",
	    data: { savs: savs, item: item, seq_id: seq_id },
	    url: "js/idl/seq_itemsav_modif.php",
	   });
	}
	function valper'.$txt_type[$i].'(savs,item,seq_id) { 
		var items = new Array();
		$(\'#savpers :selected\').each(function(i, selected) {
		  items[i] = $(selected).val();
		});
		var savs = JSON.stringify(items);

	  	$.ajax({
	    type: "POST",
	    data: { savs: savs, item: item, seq_id: seq_id },
	    url: "js/idl/seq_itemsav_modif.php",
	   });
	}
	</script>';
}
else if($txt_type[$i]=="documents") {
	echo '<form method="POST" enctype="multipart/form-data" action="edit.php?page=consult&seq_id='.$seq_info['seq_id'].'&modif#docs" class="padd4">';
	$seqdocs = mysqli_query($bdd,"SELECT * FROM ".$prefix."_seqdocs WHERE seq_id = '".$seq_info['seq_id']."' ORDER BY docname ASC");
	$seqdocs_nb = mysqli_num_rows($seqdocs);
	if($seqdocs_nb>0) {
		echo '<b><i>'.EDIT_DOCS1.'</i></b>';
		$h=1;
		foreach($seqdocs as $liste_docs) {
			echo '<p>'.FILE.' ('.$h.') : <input type="text" class="inptxt" name="'.$h.'" size="40em" value="';
			if($liste_docs['docname']=="") {
				echo $liste_docs['docurl']; }
			else { echo $liste_docs['docname']; }
			echo '" accept="'.implode(',', array_map(function($ext) { return '.' . $ext; }, $extensions)).'" /></p>';
			$h++;
		}
	}
	echo '<p><b><i>'.EDIT_DOCS2.'</i></b></p>';
	for($j=1;$j<=(9-$seqdocs_nb);$j++) {
		if($j>1) { echo '<div id="divdiv'.$j.'"';
		if(!isset($nivsseq[$j])) { echo ' style="display:none; '; }
		echo '">'; }
		echo '<p>'.FILE.' ('.($j+$seqdocs_nb).') : ';
		echo '<input type="file" class="boutontab" name="fichier'.$j.'" accept="'.implode(',', array_map(function($ext) { return '.' . $ext; }, $extensions)).'" style="width:350px;" />';
		if($j<(9-$seqdocs_nb)) {
			echo ' <a onmousedown="toggleModu(\'divdiv'.($j+1).'\',\'lplus'.$j.'\',\'lminus'.$j.'\',\'lminus'.($j-1).'\');"><i id="lplus'.$j.'" class="fas fa-plus-circle"';
			if(isset($nivsseq[($j+1)])) { echo ' style="display:none;" '; }
			echo '></i><i id="lminus'.$j.'" class="fas fa-minus-circle"';
			if(!isset($nivsseq[($j-1)])||!isset($nivsseq[($j+1)])) { echo ' style="display:none;" '; }
			echo '></i></a>';
		}
		if($j>1) { echo '</div>'; }
		echo '<input type="hidden" name="nbfiles" value="'.(10-$seqdocs_nb).'" />';
	}
	echo '<div class="right"><input class="bouton boutseq bsright" type="button" name="docs_'.$txt_type[$i].'" id="hidedocv" value="'.VALID.'" onclick="checkchamps(\'divch\');" /><br>
	<div id="divch" style="display:none;"><p class="signal">'.EDIT_INFO8.' <input class="bouton boutseq bsright" name="docsconfirm" type="submit" name="docsok_'.$txt_type[$i].'" value="'.CONFIRM.'" /></p></div>
	</form></div>';
}
else if($txt_type[$i]=="licence") {
	echo '<div class="box"><select name="licence" id="t_'.$txt_type[$i].'" value="">';		
		for($lico=0;$lico<count($licences);$lico++) {
			echo '<option';
			if($seq_info['licence']==$licences[$lico]) { echo ' selected '; }
			echo '>'.$licences[$lico].'</option>';
		}
		echo '</select></div> <a href="https://creativecommons.org/licenses/" target="_blank"><i>'.EDIT_DROITS3.'</i></a>';
}
else if($txt_type[$i]=="seq_complete") {
	echo '<input type="checkbox" id="seq_completeopt" name="seq_complete" value="1" ';
	if($seq_info['seq_complete']=="1") { echo 'checked'; }
	echo ' /><label for="seq_completeopt"> <i>'.EDIT_INFO9.'</i></label>';
}
else if($txt_type[$i]=="notions") {
	$nots = mysqli_query($bdd,"SELECT * FROM ".$prefix."_seqnot seqn INNER JOIN ".$prefix."_savnotions savn ON seqn.notion_id = savn.notion_id WHERE seq_id = '".$seq_info['seq_id']."' ORDER BY savn.notion_nom ASC");
	if(mysqli_num_rows($nots)>0) {
		$notsseq = [];
		foreach($nots as $not) {
			$notion_fr = $not['notion_nom'];
    		$notsseq[] = isset($translations[$notion_fr]) ? $translations[$notion_fr] : $notion_fr;			
		}
	}
	echo '<br><i>'.EDIT_NOTS4.'</i> (<a href="https://wikinotions.apden.org/notions.php" target="_blank"><i class="fa-solid fa-eye"></i> voir la liste</a>)<br>';
	for($j=0;$j<=14;$j++) {
		if($j>0) { echo '<div id="notdiv'.$j.'"';
		if(!isset($notsseq[$j])) { echo ' style="display:none; '; }
		echo '">'; }
		echo '<br>'.EDIT_NOTS5.' ('.($j+1).') : ';
		echo ' <input type="text" class="inptxt" name="notion'.$j.'" size="15" id="notions'.$j.'" value="';
		if(isset($notsseq[$j])) { echo $notsseq[$j]; }
		echo '" />'; ?>
		<script type="text/javascript">
		$('#notions<?php echo $j ?>').autocomplete({
			source: <?php echo json_encode($tabNotions) ?>
	        });
	     </script><?php
	    if($j<14) {
			echo ' <a onmousedown="toggleModu(\'notdiv'.($j+1).'\',\'nplus'.$j.'\',\'nminus'.$j.'\',\'nminus'.($j-1).'\');"><i id="nplus'.$j.'" class="fas fa-plus-circle"';
			if(isset($notsseq[($j+1)])) { echo ' style="display:none;" '; }
			echo '></i><i id="nminus'.$j.'" class="fas fa-minus-circle"';
			if(!isset($notsseq[($j-1)])||!isset($notsseq[($j+1)])) { echo ' style="display:none;" '; }
			echo '></i></a>';
		}
		if($j>0) { echo '</div>'; }
	}
	$tinymce[$i]=1;
	echo '<br> <br><i>'.EDIT_NOTS3.'</i><br><textarea name="'.$txt_type[$i].'" id="t_'.$txt_type[$i].'" rows="15" cols="110" class="tinymce">'; if($seq_info['notions']!='') { echo $seq_info['notions']; } echo '</textarea>';
}
else {
	$tinymce[$i]=1;
	echo '<textarea name="'.$txt_type[$i].'" id="t_'.$txt_type[$i].'" rows="15" cols="110" class="tinymce">'; if($seq_info[$txt_type[$i]]!='') { echo $seq_info[$txt_type[$i]]; } echo '</textarea>';
}
if($txt_type[$i]!='documents'&&$txt_type[$i]!='SAVOIRS') {
	echo '<div class="right">';
	echo '<script type="text/javascript">
		function validp'.$txt_type[$i].'(texte,item,seq_id) { ';
			if($txt_type[$i]=="niveau") {
				echo 'var texte = document.getElementById("niveaugo").value;';
			}
			else if($txt_type[$i]=="seq_complete") {
				echo 'var texte = "0";
			    $(":checkbox").each(function () {
			        var ischecked = $(this).is(":checked");
			        if (ischecked) {
			            texte = "1";
			        }
			    });';
			}
			else if($txt_type[$i]=="notions") {
				//LISTING DES NOTIONS
				for($j=0;$j<=14;$j++) {
					echo 'var noti'.$j.' = document.getElementById("notions'.$j.'").value;';
				}
				echo 'var texte = tinymce.get("t_'.$txt_type[$i].'").getContent();';
			}
			else if($tinymce[$i]==1) {
				echo 'var texte = tinymce.get("t_'.$txt_type[$i].'").getContent();';
			}
			else {
				echo 'var texte = document.getElementById("t_'.$txt_type[$i].'").value;';
			}
		  echo '
		  	$.ajax({
		    type: "POST",
		    data: { ';
		    if($txt_type[$i]=="notions") {
		    	for($j=0;$j<=14;$j++) {
					echo ' noti'.$j.' : noti'.$j.', ';
				}
		    }
		    echo 'texte: texte, item: item, seq_id: seq_id },
		    url: "js/idl/seq_item_modif.php",
		   });
		}
		</script>';
	echo '<input class="bouton boutseq bsright" type="submit" id="vt_'.$txt_type[$i].'" name="vt_'.$txt_type[$i].'" value="'.VALID.'" onclick="validp'.$txt_type[$i].'(this,\''.$txt_type[$i].'\',\''.$seq_info['seq_id'].'\'); validcolor(\'vt_'.$txt_type[$i].'\');" /><br></div>';
}
?>