<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="JavaScript">
function toggle(source) {
  checkboxes = document.getElementsByName('valide');
  for(var i=0, n=checkboxes.length;i<n;i++) {
    checkboxes[i].checked = source.checked;
  }
}


</script>

<script type="text/javascript">
    $("form").submit(function(){
        var str = $(this).serializeArray();
        $.ajax('action.php', str, function(result){
            alert(result);
        });
        return(false);
    });
</script>

<?php

if(isset($_REQUEST['showthreated'])) $showthreated=filter_var($_REQUEST['showthreated'], FILTER_SANITIZE_STRING); else $showthreated=false;
if(isset($_REQUEST['show'])) $Show=filter_var($_REQUEST['show'], FILTER_SANITIZE_STRING); else $Show='';

require_once "functions.php";

if ($Show == 'Emails') {
	$requette = "SELECT * FROM `service_suivis` WHERE S_email != 'NoEmail'";
}
elseif ($Show == 'Courriers') {
	$requette = "SELECT * FROM `service_suivis` WHERE S_email = 'NoEmail'";
}
else $requette = "SELECT * FROM `service_suivis`";

$resultat = Connection::executeRawQueryWithfetchAll($requette) or die('Erreur SQL !<br>'.$requette.'<BR>'.Connection::getConnection()->errorInfo());
$numrows=count($resultat);

print "Nombre d'entrées: ".$numrows;
print "<br>";
if(!$showthreated){
	print 'Afficher ceux <form action="default.php"> 
		<input type=submit name="show" value="Traité">
		<input type=hidden name="showthreated" value="true">
		<input type=hidden name="Type" value="2">
		<input type=hidden name="NoVisu" value="0">
		<input type=hidden name="Sous_Type" value="14"></form>';
}
else print 'Cacher ceux <form action="default.php"> 
		<input type=submit name="show" value="Traité">
		<input type=hidden name="showthreated" value="false">
		<input type=hidden name="Type" value="2">
		<input type=hidden name="NoVisu" value="0">
		<input type=hidden name="Sous_Type" value="14"></form>';
print "<br>";

print '<form action="menu/vehicule/suivis/action.php">
			<input type=submit name="lots" value="Traiter ceux séléctionnés">&nbsp; &nbsp;<br></form>';

if (($Show == 'Tous') || ($Show == '')){
	print 'Afficher les <form action="default.php">
			<input type=submit name="show" value="Emails">&nbsp; &nbsp;
			<input type=hidden name="Type" value="2">
			<input type=hidden name="NoVisu" value="0">
			<input type=hidden name="Sous_Type" value="14"></form>';
	print 'Afficher les <form action="default.php">
			<input type=submit name="show" value="Courriers">&nbsp; &nbsp;
			<input type=hidden name="Type" value="2">
			<input type=hidden name="NoVisu" value="0">
			<input type=hidden name="Sous_Type" value="14"></form>';
}
elseif ($Show == 'Courriers'){
	print 'Afficher les <form action="default.php">
			<input type=submit name="show" value="Emails">&nbsp; &nbsp;
			<input type=hidden name="Type" value="2">
			<input type=hidden name="NoVisu" value="0">
			<input type=hidden name="Sous_Type" value="14"></form>';
	print 'Afficher les <form action="default.php">
			<input type=submit name="show" value="Tous">&nbsp; &nbsp;
			<input type=hidden name="Type" value="2">
			<input type=hidden name="NoVisu" value="0">
			<input type=hidden name="Sous_Type" value="14"></form>';
}
elseif ($Show == 'Emails'){ 
	print 'Afficher les <form action="default.php">
			<input type=submit name="show" value="Courriers">&nbsp; &nbsp;
			<input type=hidden name="Type" value="2">
			<input type=hidden name="NoVisu" value="0">
			<input type=hidden name="Sous_Type" value="14"></form>';
	print 'Afficher les <form action="default.php">
			<input type=submit name="show" value="Tous">&nbsp; &nbsp;
			<input type=hidden name="Type" value="2">
			<input type=hidden name="NoVisu" value="0">
			<input type=hidden name="Sous_Type" value="14"></form>';
}			
print "<br>";

?>
<input type="checkbox" onClick="toggle(this)" /> Selectionner/De-Selectionner tous
<br/><br/><br/>

<table border='1'>
	<tr>
		<td>SEL</td>
		<td>ID </td>
		<td>No Atelier</td>
		<td>Nom</td>
		<td>Prenom</td>
		<td>Email</td>
		<td>No Chassis</td>
		<td>Model</td>
		<td>date dernier passage</td>
		<td>Km</td>
		<td>Last Service</td>
		<td>Next Srvc</td>
		<td>No Commande</td>
		<td>Traité</td>
    <td>Action</td>
	</tr>
<?php
$cnter='1';
for($i=0; $i < $numrows;$i++){
    $ligne = $resultat[ $i ];

    setlocale(LC_TIME, 'fr','fr_FR','fr_FR@euro','fr_FR.utf8','fr-FR','fra');
    $Treated = ChckTreated($ligne['S_Chassis']);
    $SrvcKMfromID=GetSrvcKMfromID($ligne['S_srvID']);
    $SrvcDatefr=strftime("%A %d %B %Y",strtotime($ligne['S_Date'])); 
    	if($Treated != 'OnHold'){
    		if ($showthreated){
			    if ($Treated){
			    	print '<tr bgcolor="#424242"><form action="menu/vehicule/suivis/action.php" id="actform">';
			    	print '<td>&nbsp;</td>';
			    }
			    else {
			    	print '<tr><form action="menu/vehicule/suivis/action.php" id="actform">';
			    	print '<td>< input type="checkbox" name="valide" value="valide"></td>';
			    }
			    print '<td>'.$cnter." </td>";
				print "<td><a target=_new href='atelier.php?NoAtelier=".$ligne['S_ID']."&new=0' >".$ligne['S_ID']."</a></td>";
				print '<td>'.$ligne["S_Nom"]." </td>";
				print '<td>'.$ligne["S_Prenom"]." </td>";
				print '<td>'.$ligne['S_email']."</td>";
				print '<td>'.$ligne["S_Chassis"]." </td>";
				print '<td>'.$ligne["S_Vehicule"]." </td>";
				print '<td>'.$SrvcDatefr."</td>";
				print '<td>'.number_format($ligne['S_Km'], 0, '', '\'')." </td>";
				print '<td>'.number_format($SrvcKMfromID, 0, '', '\'')."</td>";
				print '<td>'.number_format($ligne['S_Nxsrvc'], 0, '', '\'')."</td>"; 
				print "<td><a target=_new href='?NoCommande=".$ligne['S_Commande']."&Type=4&Sous_Type=1&Etape=3&NoVisu=0' >".$ligne['S_Commande']."</td>";
				print '<td>'.$Treated."</td>";
				print '<td>';
			  	print '	<select name="actlist" >
			  	      	<option value="courrier">Courrier</option>';
			  	    	if($email != "No Email") print '<option value="email">Email</option>';
			  			print '<option value="hide">hide</option>
			  	    </select>
			  	    <input type="hidden" name="NoClient" value="'.$ligne["S_NoClient"].'">
			  	    <input type="hidden" name="Nom" value="'.$ligne["S_Nom"].'">
			  	    <input type="hidden" name="Prenom" value="'.$ligne["S_Prenom"].'">
			  	    <input type="hidden" name="email" value="'.$ligne['S_email'].'">
			  	    <input type="hidden" name="NoChasis" value="'.$ligne["S_Chassis"].'">
			  	    <input type="hidden" name="Model" value="'.$ligne["S_Vehicule"].'">
			  	    <input type="hidden" name="Date" value="'.$SrvcDatefr.'">
			  	    <input type="hidden" name="Km" value="'.$ligne['S_Km'].'">
			  	    <input type="hidden" name="LastSRvc" value="'.$lastSrvc.'">
			  	    <input type="hidden" name="NxtSrv" value="'.$ligne['S_Nxsrvc'].'">
			  	    <input type="hidden" name="Description" value="'.$ligne['S_Commande'].'">
			  	    <input type="submit" value="Go">
			  	   </form>';
			    print "</td>";
			    print "</tr>";
			    $cnter++;
			} else continue ;
	  	}
	  	elseif($Treated == 'OnHold'){
	  		
    		$SrvcDatefr=strftime("%A %d %B %Y",strtotime($ligne['S_Date']));
	  		$resultatadressewrong = Client::getClientByMailingPaperWrong($ligne["S_NoClient"], $ligne["S_NoClient"]);
			if (count($resultatadressewrong)>0){
				echo "<tr style=\"background-color:rgba(255,255,0,1);\">";
			  }
			  else {
			  	echo "<tr>";
			  }
	  		print '<form action="menu/vehicule/suivis/action.php" id="actform">';
	  		print '<td><input type="checkbox" name="valide" value="valide"></td>';
		    print '<td>'.$cnter." </td>";
			print "<td><a target=_new href='atelier.php?NoAtelier=".$ligne['S_ID']."&new=0' >".$ligne['S_ID']."</a></td>";
			print '<td>'.$ligne["S_Nom"]." </td>";
			print '<td>'.$ligne["S_Prenom"]." </td>";
			print '<td>'.$ligne['S_email']."</td>";
			print '<td>'.$ligne["S_Chassis"]." </td>";
			print '<td>'.$ligne["S_Vehicule"]." </td>";
			print '<td>'.$SrvcDatefr."</td>";
			print '<td>'.number_format($ligne['S_Km'], 0, '', '\'')." </td>";
			print '<td>'.number_format($SrvcKMfromID, 0, '', '\'')."</td>";
			print '<td>'.number_format($ligne['S_Nxsrvc'], 0, '', '\'')."</td>"; 
			print "<td><a target=_new href='?NoCommande=".$ligne['S_Commande']."&Type=4&Sous_Type=1&Etape=3&NoVisu=0' >".$ligne['S_Commande']."</td>";
			print '<td>'.$Treated."</td>";
			print '<td>';
		  	print '<select name="actlist" >
		  	      	<option value="courrier">Courrier</option>';
		  	    	if($ligne['S_email'] != "NoEmail") print '<option selected value="email">Email</option>';
		  			print '<option value="hide">hide</option>
		  	    </select>
		  	    <input type="hidden" name="NoClient" value="'.$ligne["S_NoClient"].'">
		  	    <input type="hidden" name="Nom" value="'.$ligne["S_Nom"].'">
		  	    <input type="hidden" name="Prenom" value="'.$ligne["S_Prenom"].'">
		  	    <input type="hidden" name="email" value="'.$ligne['S_email'].'">
		  	    <input type="hidden" name="NoChasis" value="'.$ligne["S_Chassis"].'">
		  	    <input type="hidden" name="Model" value="'.$ligne["S_Vehicule"].'">
		  	    <input type="hidden" name="Date" value="'.$SrvcDatefr.'">
		  	    <input type="hidden" name="Km" value="'.$ligne['S_Km'].'">
		  	    <input type="hidden" name="LastSRvc" value="'.$lastSrvc.'">
		  	    <input type="hidden" name="NxtSrv" value="'.$ligne['S_Nxsrvc'].'">
		  	    <input type="hidden" name="Description" value="'.$ligne['S_Commande'].'">
		  	    <input type="submit" value="Go">
		  	    </form>';
		    print "</td>";
		    print "</tr>";
		    $cnter++;
	  	}
}

?>
</table>
