<?php
if(isset($_GET['p']) && $_GET['p']=='supprimer'){
	$login_ajout = $_POST["login"];
	$jour = $_POST["jour"];
	$heure = $_POST["heure"];
	$minute = $_POST["minute"];
	$fin = $_POST["fin"];

	$req = 'SELECT idjoueur FROM matgraph_users WHERE login = "'.$login_ajout.'" limit 1';
	$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
	$num = mysql_num_rows($res);
	if ( $num > 0 ) {
		while ( $row = mysql_fetch_array($res) ) {
			$requete = 'INSERT INTO matgraph_avertissement VALUES ("", "'.$login_ajout.'", "'.$jour.'", "'.$heure.'", "'.$minute.'", "'.$fin.'")';
			$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
	
			$req2 = "SELECT id,fer_on,or_on,exfer_on,exor_on,ada_on,ura_on FROM matgraph_users WHERE idjoueur = $row[idjoueur] ORDER by id ASC";
			$res2 = mysql_query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());
			$num2 = mysql_num_rows($res2);
			if ( $num2 > 0 ) {
	  		  while ( $row2 = mysql_fetch_array($res2) ) {
	  			$fer_on = 0;
	  			$or_on = 0;
	  			$exfer_on = 0;
	  			$exor_on = 0;
	  			$ada_on = 0;
	  			$ura_on = 0;
	  			if($row2['fer_on'] != 0){
	  				$fer_on = 0.75;
	  			}
	  			if($row2['or_on'] != 0){
	  				$or_on = 0.75;
	  			}
	  			if($row2['exfer_on'] != 0){
	  				$exfer_on = 0.75;
	  			}
	  			if($row2['exor_on'] != 0){
	  				$exor_on = 0.75;
	  			}
	  			if($row2['ada_on'] != 0){
	  				$ada_on = 0.75;
	  			}
	  			if($row2['ura_on'] != 0){
	  				$ura_on = 0.75;
	  			}
	  	
				$sql4 = "INSERT INTO `matgraph_logadmin` ( `id` , `mdj` , `date` , `action` ) VALUES ('', '".$login."', now(), 'Ajout averto | ".addslashes($login_ajout)." | ".addslashes($jour)."j ".addslashes($heure)."h ".addslashes($minute)."m | ".addslashes($fin)."')";
				$res4 = mysql_query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());			
	  			$requete = "UPDATE matgraph_users SET fer_on=$fer_on,or_on=$or_on,exfer_on=$exfer_on,exor_on=$exor_on,ada_on=$ada_on,ura_on=ura_on,techrare_moinsressource=1 WHERE id='$row2[id]'";
				$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
			  }  
			}
		}
	}else{
		echo '<script language=javascript>alert (" '.$login_ajout.' introuvable ");</script>';
	}
}elseif(isset($_GET['p']) && $_GET['p']=='ajouter'){
	$id = $_POST["id"];

$sql4 = "SELECT * FROM matgraph_avertissement WHERE id=$id";
$res4 = mysql_query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
$row4 = mysql_fetch_array($res4);   

$req = 'SELECT idjoueur FROM matgraph_users WHERE login = "'.$row4[login].'" limit 1';
$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$row = mysql_fetch_array($res);

$req2 = "SELECT id,fer_on,or_on,exfer_on,exor_on,ada_on,ura_on FROM matgraph_users WHERE idjoueur = $row[idjoueur] ORDER by id ASC";
$res2 = mysql_query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());
$num2 = mysql_num_rows($res2);
if ( $num2 > 0 ) {
  While ( $row2 = mysql_fetch_array($res2) ) {
  	$fer_on = 0;
	$or_on = 0;
	$exfer_on = 0;
	$exor_on = 0;
  	$ada_on = 0;
  	$ura_on = 0;
  	if($row2['fer_on'] != 0){
  		$fer_on = 1;
  	}
  	if($row2['or_on'] != 0){
  		$or_on = 1;
  	}
  	if($row2['exfer_on'] != 0){
  		$exfer_on = 1;
  	}
  	if($row2['exor_on'] != 0){
  		$exor_on = 1;
  	}
  	if($row2['ada_on'] != 0){
  		$ada_on = 1;
  	}
  	if($row2['ura_on'] != 0){
  		$ura_on = 1;
  	}

	$sql4 = "INSERT INTO `matgraph_logadmin` ( `id` , `mdj` , `date` , `action` ) VALUES ('', '".$login."', now(), 'Supr averto | ".addslashes($row4['login'])." | ".addslashes($row4['jour'])."j ".addslashes($row4['heure'])."h ".addslashes($row4['minute'])."m | ".addslashes($row4['date'])."')";
	$res4 = mysql_query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());

  	$requete = "UPDATE matgraph_users SET fer_on=$fer_on,or_on=$or_on,exfer_on=$exfer_on,exor_on=$exor_on,ada_on=$ada_on,ura_on=ura_on,techrare_moinsressource=0 WHERE idjoueur='$row2[id]'";
	$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
	}  
}
$sql4 = "DELETE FROM matgraph_avertissement WHERE id = $id";
$res4 = mysql_query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
}else{
echo"<div align='center'><br><br><table width=\"659\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  <tr style =\"background-color:#222222;\">
    <td><div align=\"center\"><b>Login</b></div></td>
    <td><div align=\"center\"><b>Temps</b></div></td>
    <td><div align=\"center\"><b>Fin</b></div></td>
	<td><div align=\"center\"><b>Del</b></div></td>
  </tr>";
  
$req = "SELECT * FROM matgraph_avertissement ORDER by date DESC";
$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if ( $num > 0 ) {
  $i=1;
  While ( $row = mysql_fetch_array($res) ) {
	echo"<tr";
	// gère le quadrillage du tableau
			if (($i/2) == ceil($i/2)){
				echo' style ="background-color:#111111;"' ;
			}
	echo">
    <td><div align=\"center\">$row[login]</div></td>
    <td><div align=\"center\">$row[jour] j $row[heure] h $row[minute] min</div></td>
    <td><div align=\"center\">$row[date]</div></td>
	<form name=\"suprimer\" method=\"post\" action=\"index.php?action=avertissement&amp;p=supprimer\" target=\"_parent\"><td align=\"center\">
           <input type=\"hidden\" name=\"id\" value=\"$row[id]\">
          <input type=\"image\" src=\"../imgs/del.gif\" Style=\"border:0px solid #7E7E7E;\" width=\"25\">
                  </td></form>
  </tr>";
  $i++;
  }
}
  
echo"</table>";

echo"
<br><br><div align='center'>
	<form name=\"avertissement_ajout\" method=\"post\" action=\"index.php?action=avertissement&amp;p=ajouter\">
	<table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  <tr><td>login : </td><td><input type=\"text\" name=\"login\"></td></tr>
  <tr><td>nombre de jours : </td><td><input type=\"text\" name=\"jour\"></td></tr>
  <tr><td>nombre de heures : </td><td><input type=\"text\" name=\"heure\"></td></tr>
  <tr><td>nombre de minutes : </td><td><input type=\"text\" name=\"minute\"></td></tr>
  <tr><td>Date Fin : </td><td><input type=\"text\" name=\"fin\" value=\"".strftime("%Y-%m-%d %H:%M:%S", time())."\"></td></tr>
  <tr><td colspan=\"2\" align=\"center\"><br><input type=\"submit\" name=\"Submit\" value=\"Ajouter\" Style=\"border:1px solid #7E7E7E;\"></td></tr>
</table></form></div>";

$sql4 = "SELECT id,login FROM matgraph_avertissement WHERE date < now() ";
$res4 = mysql_query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
$num4 = mysql_num_rows($res4);
if ( $num4 > 0 ) {
	  While ( $row4 = mysql_fetch_array($res4) ) {  
	
	$req2 = "SELECT idjoueur,fer_on,or_on,exfer_on,exor_on,ada_on,ura_on FROM matgraph_users WHERE login = '$row4[login]' ORDER by id ASC";
	$res2 = mysql_query($req2) or die('Erreur SQL!1 <br>'.$req2.'<br>'.mysql_error());
	$num2 = mysql_num_rows($res2);
	if ( $num2 > 0 ) {
	  While ( $row2 = mysql_fetch_array($res2) ) {
	  	$fer_on = 0;
		$or_on = 0;
		$exfer_on = 0;
		$exor_on = 0;
	  	$ada_on = 0;
	  	$ura_on = 0;
	  	if($row2['fer_on'] != 0){
	  		$fer_on = 1;
	  	}
	  	if($row2['or_on'] != 0){
	  		$or_on = 1;
	  	}
	  	if($row2['exfer_on'] != 0){
	  		$exfer_on = 1;
	  	}
	  	if($row2['exor_on'] != 0){
	  		$exor_on = 1;
	  	}
	  	if($row2['ada_on'] != 0){
	  		$ada_on = 1;
	  	}
	  	if($row2['ura_on'] != 0){
	  		$ura_on = 1;
	  	}
	  	
	  	$requete = "UPDATE matgraph_users SET fer_on=$fer_on,or_on=$or_on,exfer_on=$exfer_on,exor_on=$exor_on,ada_on=$ada_on,ura_on=ura_on,techrare_moinsressource=0 WHERE idjoueur='$row2[idjoueur]'";
		$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
		}  
	}
	
	
	$sql5 = "DELETE FROM matgraph_avertissement WHERE id = $row4[id]";
	$res5 = mysql_query($sql5)or die('erreur sql<br>'.$sql5.'<br>'.mysql_error());
	
	}
}
echo ' 
<br/><br/><center>Avertissement a jours</center><br/>';
}
if(isset($_GET['p'])){
	echo "<script type=\"text/javascript\">
		window.location.replace('index.php?action=avertissement');
		</script>";
}
echo "</div>";
?>