<?php
session_start();
if (!isset($_SESSION['acces'])) {
	header ('Location: acces.php?anh_san=paray_ci');
}
include('connexion.php');
$date_img=date('d_m_Y_H_i_s_');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr"><head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="style/style_admin.css" type="text/css">
<link rel="stylesheet" media="screen" type="text/css" title="Design" href="style/style.css">
<link rel="stylesheet" media="screen" type="text/css" title="Design" href="style/bootstrap.css">
<title>AURORE Admin</title>

</head>

<body>
<?php
if (isset($_GET['id']))
{
	$id=addslashes($_GET['id']);
	$chaiz=addslashes($_GET['chaiz']);
	$page_prec='modifier_element.php?id='.$id.'&chaiz='.$chaiz.'';

	switch ($chaiz)
	{
		case 'produit': $sql = 'SELECT *
			FROM '.$chaiz.'
			WHERE id_prod='.$id.'';
			$reponse=$bdd-> query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
			$data =$reponse->fetch();
			$aff='Modification de Produit';
?>
<div id="toute_off" align="center">
    <div class="page"> <?php echo $aff;?></div>
    <div align="right"><a href="ajouter_element.php?chaiz=<?php echo $chaiz;?>" class="myButton">Ajouter</a></div>
        
    <div class="cov_off" style="border-radius:15px; margin-bottom:20px;">
    	<form role="form" method="post" action="" enctype="multipart/form-data" name="form2">
        	<table border="0" width="100%">
			<tr>
                <td class="menu_off" width="25%">Nom</td>
                <td><input type="text" class="form-control" name="nom" value="<?php echo ($data[1]);?>"></td>
            </tr>
            <tr>
                <td class="menu_off">Description</td>
                <td><textarea class="form-control" name="descrip" id="textarea_prod"><?php echo ($data[2]);?></textarea></td>
            </tr>
			
			<tr>
                <td class="menu_off" width="25%">Ancien Prix ci-possible</td>
                <td><input type="text" class="form-control" name="old" value="<?php echo ($data[3]);?>"></td>
            </tr>
			<tr>
                <td class="menu_off" width="25%">Nouveau Prix</td>
                <td><input type="text" class="form-control" name="new" value="<?php echo ($data[4]);?>"></td>
            </tr>
			<tr>
                <td class="menu_off" width="25%">Image</td>
                <td><input type="file" class="form-control" name="tof"><img src="../../uploads/<?php echo ($data[5]);?>" class="modif_img"></td>
            </tr>
			
				<tr>
                <td class="menu_off" width="25%">Type de produit</td>
                <td><input type="text" class="form-control" name="type_proda" value="<?php echo ($data[7]);?>"></td>
            </tr>
			
            </table>
            <input type="submit" class="btn btn-default" value="Valider" name="valider"> <a href="admin_element.php?chaiz=<?php echo $chaiz;?>">Retour</a>
        </form>
        
        <?php
		if (isset($_POST['valider']))
		{$type_proda=addslashes(($_POST['type_proda']));
			$nom=addslashes(($_POST['nom']));
			$descrip=addslashes(($_POST['descrip']));
			$old=addslashes(($_POST['old']));
			$new=addslashes(($_POST['new']));
			
			$name_img=$_FILES['tof']['name'];
			$type_img=$_FILES['tof']['type'];
			$size_img=$_FILES['tof']['size'];
			$temp_img=$_FILES['tof']['tmp_name'];
			$error_img=$_FILES['tof']['error'];

			if (empty($nom) or empty($new))
			{
				echo '<div style="text-align:center; color:red;"><img src="img/rouge.png" width="20">Veuillez renseigner tous les champs</div>';
			}
			else
			{
				if ($error_img==0)
				{
					if (($type_img=="image/png")or ($type_img=="image/gif")or ($type_img=="image/jpeg") or ($type_img=="image/jpg") or ($type_img=="") or ($type_img=="image/bmp"))
					{
						move_uploaded_file($temp_img, "../../uploads/".$date_img.$name_img);
						$lien_img=$date_img.$name_img;
					}
					else
					{
						echo '<div style="text-align:center; color:red;"><img src="img/rouge.png" width="20">Ce format est incorrecte !</div>';
					}
				}
				else
				{
					$lien_img=$data['img_prod'];
				}
				$req='UPDATE '.$chaiz.' SET lib_prod="'.$nom.'", descrip_prod="'.$descrip.'", old_prod="'.$old.'", new_prod="'.$new.'", img_prod="'.$lien_img.'" , type_proda="'.$type_proda.'"  WHERE id_prod='.$id.'';
				$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
				echo '<div style="text-align:center; color:green;"><img src="img/vert.png" width="20"> Produit modifié avec succès </div>';
				?> 
<script type="text/javascript">window.setTimeout("location=('<?php echo $page_prec; ?>');",2200);</script>
				<?php
			}
		}
		?>
    </div>
</div>
<?php 		
		break;

		default: header ('Location: administration.php?anh_san=paray_ci');
}
?>
<table border="0" align="center"><tr>
    <td class="bouton_design">
    <a href="administration.php?anh_san=paray_ci">Accueil Panneau</a></td>
</tr></table>
<?php 
}
else
{
	header ('Location: administration.php?anh_san=paray_ci');
}
?>
</body>
</html>