<?php require_once 'inc/includes.php';
if($_SERVER['REQUEST_METHOD'] == "POST"){
	$id =intval($_POST['id']);
	$validate = true;
	if(isset($_POST['active'])){
		$active =1;
	}else{
		$active =0;
	}
	$data= array(
		'id' => $id,
		'username'=>$_POST['username'],
		'email'=>$_POST['email'],
		'name'=>$_POST['name'],
		'prenom'=>$_POST['prenom'],
		'role'=>$_POST['role'],
		'bio'=>$_POST['bio'],
		'active'=>$active,
		'avatar'=>$_POST['avatar_old']
		);
	if(empty($_POST['username'])){
		$validate =false ;
		$erreur_username = "Veuillez entrer le login de l'utilisateur";
	}
	if(empty($_POST['email'])){
		$validate =false ;
		$erreur_email = "Veuillez entrer l' email de l'utilisateur";
	}

	if(!empty($_FILES['avatar']['name']) && $validate){
			$extensions = array('.png','.gif','.jpg','.jpeg');
			$extension = strrchr($_FILES['avatar']['name'], '.');

			$dossier =UPLOAD;
			if(!in_array($extension,$extensions)){
				$erreur_avatar = "Vous devez uploader une avatar de type png, jpg, gif ,jpeg";
				$validate = false;
			}else{
				$avatar =md5($_FILES['avatar']['name'])."$extension";
				if(!move_uploaded_file($_FILES['avatar']['tmp_name'],'../'.$dossier.$avatar)){
					$validate = false;
					$_SESSION['erreur'] = "Un problème est survenu lors de l'Upload de l'avatar.";
				}else{
					$data['avatar'] =$dossier.$avatar;	
				}
			}
		}

	if($validate){
		$nb= $DB->insert("UPDATE users SET username=:username,email=:email,name=:name,prenom=:prenom,active=:active,bio=:bio,role=:role,avatar=:avatar WHERE id=:id",$data);
		if($nb){
			$_SESSION['message'] = "le profil de l'utilisateur a été mis a jour avec succès.";
			header('location:users.php');
			exit();
		}else{	
			$_SESSION['erreur'] = "Un problème de sauvegarde !";
		}
	}

}
if(isset($_GET['id'])){
	$user = $DB->query("SELECT * FROm users WHERE id=:id",array('id'=>$_GET['id']));

}


include 'inc/header.php' ?>


<section>

<aside id="sidebar2">
      		<!-- widget --> 
       		<section class="widget">
            
               <ul>
               	<li class="catsidebar active"><a href="role.php">Roles</a></li>
                <li class="catsidebar"><a href="users.php">Utilisateurs</a></li>
               	<li class="catsidebar"><a href="echantillons.php">Nature d'echantillon</a></li>
				<li class="catsidebar"><a href="typestocks.php">Type de stockage</a></li>

               </ul>
           </section>
           <!-- Fin widget -->
       
           <!-- Fin widget --> 
       
       
       </aside>
       
       

</section>


<!-- fiche -->

        <article style="width: 65%;margin: auto;0px; position: relative;">
	
 <div class="container-fluid">
                    
<div class="row">

	<div class="col-sm-12">
	<form action="editusers.php?id=<?php echo $user[0]->id; ?>" method="POST" enctype="multipart/form-data">

				<input type="hidden" name ="id" value ="<?php echo $user[0]->id; ?>">

		<div class="form-group">
		
			
					<label for="role">Role </label>&nbsp  <input type="text" name="role" value ="<?php echo $user[0]->role ;?>"  class="form-control col-sm-16" style="width:68%; position: relative; left:40px;">>
				</p>

		</div>
		</div>
	</div>
	
	<div >
	



		<div class="form-group">
		
			<p>
					<label for="text">Pseudo </label>&nbsp  <input type="text" name="username" value ="<?php echo $user[0]->username ;?>"  class="form-control col-sm-16" style="width:68%; position: relative; left:40px;">>
				</p>
<?php if (!empty($erreur_username)): ?>
					<div class="error"><?php echo $erreur_username; ?></div>
				<?php endif ?>
		</div>
	</div>
	
			
	<div >
		<div class="form-group">
			<label for="name">Nom </label> &nbsp&nbsp&nbsp&nbsp&nbsp <input type="text" value ="<?php echo $user[0]->name ;?>" name="name" id="text"  class="form-control col-sm-16" style="width:68%; position: relative; left:40px;">

		</div>
	</div>
					
	<div >
		<div class="form-group">
			<label for="prenom">Prénoms</label><input value ="<?php echo $user[0]->prenom ;?>" type="text" name="prenom" id="text"  class="form-control col-sm-16" style="width:68%; position: relative; left:40px;">

		</div>
	</div>
		<div >
		<div class="form-group">
			<label for="text">Email </label> &nbsp&nbsp&nbsp <input  value ="<?php echo $user[0]->email ;?>" type="text" name="email" class="form-control col-sm-16" style="width:68%; position: relative; left:40px;">
<?php if (!empty($erreur_email)): ?>
					<div class="error"><?php echo $erreur_email; ?></div>
				<?php endif ?>
		</div>
	</div>
	
				<input type="hidden" name="avatar_old" value ="<?php echo !empty($user[0]->avatar)?$user[0]->avatar:'' ?>">
				<?php if (!empty($user[0]->avatar)): ?>
					<p class="visu">
						<img src="../<?php echo $user[0]->avatar; ?>">
					</p>
				<?php else:  ?>
				<p>Aucun avatar.</p>
				<?php endif ?>
				<p>
	<div >
		<div class="form-group">
			<label for="text">Avatar </label> &nbsp&nbsp&nbsp <input type="file" name="avatar"  class="form-control col-sm-16" style="width:68%; position: relative; left:40px;">
	<?php if (!empty($erreur_avatar)): ?>
					<div class="error"><?php echo $erreur_avatar; ?></div>
				<?php endif ?>
		</div>
	</div>
		
		<div >
		<div class="form-group">
			<label for="text">Active </label> &nbsp&nbsp&nbsp  <input type="checkbox" id="text" name="active" <?php echo ( $user[0]->active  == 1)?'checked="checked"':'' ; ?>value ="<?php echo $user[0]->active ;?>" >

		</div>
	</div>
	
	<p>
					<input type="submit" value="Enregistrer">
				</p>
		
<!-- FIN DES INPUTS HORIZONTAL -->
	</form>		  
		  

</div>
 
 </article>
 </div>
 
 
 
  <!-- jQuery -->
<script src="https://extremelab.tech/plugins/jquery/jquery.min.js"></script>
<!-- jQuery UI 1.11.4 -->
<script src="https://extremelab.tech/plugins/jquery-ui/jquery-ui.min.js"></script>
<!-- Bootstrap 4 -->
<script src="https://extremelab.tech/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- ChartJS -->
<script src="https://extremelab.tech/plugins/chart.js/Chart.min.js"></script>
<!-- Sparkline -->
<script src="https://extremelab.tech/plugins/sparklines/sparkline.js"></script>
<!-- jQuery Knob Chart -->
<script src="https://extremelab.tech/plugins/jquery-knob/jquery.knob.min.js"></script>
<!-- daterangepicker -->
<script src="https://extremelab.tech/plugins/moment/moment.min.js"></script>
<script src="https://extremelab.tech/plugins/daterangepicker/daterangepicker.js"></script>
<!-- Tempusdominus Bootstrap 4 -->
<script src="https://extremelab.tech/plugins/tempusdominus-bootstrap-4/js/tempusdominus-bootstrap-4.min.js"></script>
<!-- Summernote -->
<script src="https://extremelab.tech/plugins/summernote/summernote-bs4.min.js"></script>
<!-- overlayScrollbars -->
<script src="https://extremelab.tech/plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<!-- AdminLTE App -->
<script src="https://extremelab.tech/dist/js/adminlte.js"></script>
<!-- DataTables -->
<script src="https://extremelab.tech/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-fixedheader/js/dataTables.fixedHeader.min.js" type="text/javascript"></script>
<script src="https://extremelab.tech/plugins/datatables-buttons/js/dataTables.buttons.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-buttons/js/buttons.bootstrap4.min.js"></script>
<script src="https://extremelab.tech/plugins/jszip/jszip.min.js"></script>
<script src="https://extremelab.tech/plugins/pdfmake/pdfmake.min.js"></script>
<script src="https://extremelab.tech/plugins/pdfmake/vfs_fonts.js"></script>
<script src="https://extremelab.tech/plugins/datatables-buttons/js/buttons.php5.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-buttons/js/buttons.print.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-buttons/js/buttons.colVis.min.js"></script>
<!-- Toastr-->
<script src="https://extremelab.tech/js/toastr.min.js"></script>
<!-- Validate -->
<script src="https://extremelab.tech/plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="https://extremelab.tech/plugins/print/jQuery.print.min.js"></script>
<script src="https://extremelab.tech/js/jquery.classyqr.min.js"></script>
<script src="https://extremelab.tech/js/select2.js"></script>
<script src="https://extremelab.tech/plugins/sweet-alert/sweetalert.min.js"></script>
<!-- Scripts Translation -->

<!-- Main dashboard -->
  <script src="https://extremelab.tech/js/admin/main.js"></script>
<!-- Flash messages -->
<script>
    </script>

    <!-- Switch -->
  <script src="https://extremelab.tech/plugins/swtich-netliva/js/netliva_switch.js"></script>
  <script src="https://extremelab.tech/js/admin/dashboard.js"></script>
  
  <script src="//code.tidio.co/wm6kjlncgktzimpuaad5qetmtv9h1cui.js" async></script>

</body>
</html>
  
		 
