<?php
if(!empty($_SESSION['admin_connect_id']))
{

if(!isset($_GET['id_user'])){
  include("pages/404.php");
 }else{
$id_user=mysqli_real_escape_string($con,htmlspecialchars(trim($_GET['id_user']))) ;
$req_get_user="SELECT * FROM users where id='$id_user'";
$query_get_user=mysqli_query($con,$req_get_user);

$user = mysqli_fetch_array($query_get_user); 

?>

      <h4 class="title"><i class="fa fa-users "></i> Modifier utilisateur</h4>
  <div class="row">
              <div class="col-lg-12">
                  <div class="form-panel">
                   <form id="edit-user-form" method="" class="form-horizontal" action=""  enctype="multipart/form-data">
                 <div class="row">
                 <div class="col-sm-6 col-xs-12">
                      <div class="form-group">
                      <div class="col-sm-12 col-xs-12">
                      <label>Nom </label>
                      <input type="text" required="required"  class="form-control"  id="nom" name="nom" value="<?php echo $user['nom'] ?>">
                      </div>
                      </div>
                <div class="form-group">
                        <div class="col-sm-12 col-xs-12">
                      <label>Prénom </label>
                      <input type="text" class="form-control" required="required"   id="prenom" name="prenom" value="<?php echo $user['prenom'] ?>" >
                      </div>
                      </div>
                      <div class="form-group">
                       <div class="col-sm-12 col-xs-12">
                      <label>Type </label>
                       <select name="type" class="form-control"  >
                        <option value="<?php echo $user['type'] ?>"><?php echo $user['type'] ?></option>
                        <option value="admin">admin</option>
                        <option value="superviseur">superviseur</option>
                       </select>
                      </div>

                     </div>
                     <div class="form-group">
                        <div class="col-sm-12 col-xs-12">
                      <label>Username </label>
                      <input type="text" required="required"  class="form-control"  id="username" name="username" value="<?php echo $user['username'] ?>" >

                      </div>
                      </div>
                    <div class="form-group">
                      <div class="col-sm-12 col-xs-12">
                      <label>Images </label>
                      <input type="file" class="form-control"  id="files" name="files" >

                      </div>
                      </div>
   <div class="form-group">
                         <div class="col-sm-6 col-xs-12">

     <input type="hidden" name="image"value="<?php echo $user["img"]; ?>">
     <input type="hidden" name="id_user" value="<?php echo $user["id"]; ?>">
     <input type="submit" id="modifier" class="btn btn-primary btn-block" value="Modifier">
                     </div>
                     </div>
                     </div>
<div class="col-sm-6 col-xs-12">
  <img src="assets/img_user/<?php echo $user["img"]; ?>" style="width: 80%; display: block; float: right;" />
</div>
</div>


                <div class="input-group message col-sm-6" id="mess">

              </div>
                </form>

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

<script type="text/javascript" src="assets/js/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready( function () {
$("#g_users").addClass("active");
document.title = 'Modifier utilisateur';
jQuery.validator.addMethod('selectcheck', function (value) {
return (value != '0');

    }, "type required");
$("#edit-user-form").validate({
submitHandler: function(form) {
$.ajax({
   url: 'actions/user/modifier.php',
 type: "POST",
 data: new FormData(form),
 contentType: false,
 cache: false,
 processData: false,
 success: function(data){
 if(data == 1){
$("#mess").html('<div class="alert alert-success">  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>   <strong>Modification avec succès </strong> </div>')  ;
              $("#edit-user-form")[0].reset();
              setTimeout(function() {
location.reload();
}, 2000);
    }  else if(data == 0){
     $("#mess").html('<div class="alert alert-danger">  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>   <strong>Erreur </strong> </div>')  ;
     }
         else if(data == 2) {
     $("#mess").html('<div class="alert alert-danger">  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>   <strong>Ce nom d\'utilisateur est disponible.</strong> </div>')  ;

     }     else  {
     $("#mess").html('<div class="alert alert-danger">  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>   <strong>Erreur.</strong> </div>')  ;

     }
},
 error: function(){}
 });
  } ,
                rules: {
               nom: "required",
               prenom: "required",
               type: "selectcheck",
               username: "required",
               password: {
                  required: true,
                  minlength: 8
                  }

                 },
                messages: {

                    nom: "Entrez le nom ",
                    username: "Entrez le nom de l'utilisateur",
                    prenom: "Entrez le prénom ",
                    type: "sélectionnez le type",
                   password: {
                       required: "veuillez fournir un mot de passe",
                       minlength: "Votre mot de passe doit comprendre 8 caractères ou plus."
                        }


                },
                errorElement: "em",
                errorPlacement: function ( error, element ) {

                    error.addClass( "help-block" );

                    element.parents( ".col-sm-12" ).addClass( "has-feedback" );

                    if ( element.prop( "type" ) === "checkbox" ) {
                        error.insertAfter( element.parent( "label" ) );
                    } else {
                        error.insertAfter( element );
                    }

                },

                highlight: function ( element, errorClass, validClass ) {
                    $( element ).parents( ".col-sm-12" ).addClass( "has-error" ).removeClass( "has-success" );
                    $( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
                },
                unhighlight: function ( element, errorClass, validClass ) {
                    $( element ).parents( ".col-sm-12" ).addClass( "has-success" ).removeClass( "has-error" );
                    $( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
                }
            } );
        } );


</script>
<?php } }else{   include("pages/404.php");  } ?>