 <!-- Content Header (Page header) -->
<section class="content-header"style="margin-top: 50px;">
    <h1>
       utilisateur

    </h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin/dashboard')?>"><i class="fa fa-dashboard"></i> <?php echo lang('my_profile')?></a></li>

        <li class="active"><?php echo lang('my_profile')?></li>
    </ol>
    <?php

          if($this->session->flashdata('message'))
              $message = $this->session->flashdata('message');
            if($this->session->flashdata('error'))
              $error  = $this->session->flashdata('error');
            if(function_exists('validation_errors') && validation_errors() != '')
            {
              $error  = validation_errors();
            }
        ?>

              <?php if(!empty($error) || !empty($message)){
$etat=$this->session->flashdata('etat');
                ?>
        <div class="container">
                      <?php if (!empty($error)): ?>
                      <div class="alert alert-danger alert-dismissable col-md-10">
                          <i class="fa fa-ban"></i>
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                          <?php echo $error; ?>
                      </div>
                      <?php endif; ?>

                      <?php if (!empty($message)): ?>
                      <div class="alert <?php echo $etat; ?> alert-dismissable col-md-10">
                          <i class="fa fa-info"></i>
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                         <?php echo $message; ?>
                      </div>
                      <?php endif; ?>

             </div>
             <?php }?>
</section>

<section class="content">
    <div class="row">
        <!-- left column -->
        <div class="col-md-12">
            <!-- general form elements -->
            <div class="box box-primary">
                <div class="box-header">
                    <h3 class="box-title">
					FORMULAIRE utilisateur
                    </h3>
                </div><!-- /.box-header -->
                <!-- form start -->
				<form method="post" action="<?php echo site_url('admin/users/form/'.$id); ?>" enctype="multipart/form-data" id="signup_form">
                    <div class="box-body">

					  <div class="row">

              <div class="col-md-6">
                <div class="form-group">

                            <label>EMPLOYE</label> <b style="color: red;font-style: italic;font-size: 11px;">*</b>
                            <select name="id" id="id" autocomplete="off" class="form-control">
                              <option value="">--Employé--</option>
                                                      <?php
                                if(!empty($employes)){

                                  foreach($employes as $st){
                                    if($st->is_user==0 && $st->tokens==1){?>
                                <option value="<?php echo $st->id?>" <?php echo ($st->id)?'selected="selected"':'' ?> > <?php echo $st->firstname?> <?php echo $st->lastname?></option>
                              <?php 	}}
                                }
                              ?>
                            </select>


              </div>



            <div class="form-group">

                            <label><?php echo lang('username')?></label> <b style="color: red;font-style: italic;font-size: 11px;">*</b>
                            <input type="hidden" name="oldusername" value="<?php echo $username?>" />
                <?php
                  $data	= array('name'=>'username', 'id'=>'username','autocomplete'=>'off','value'=>set_value('username', $username), 'class'=>'form-control username');
                  echo form_input($data); ?>
             </div>
            <div class="form-group">
                            <label><?php echo lang('email')?></label> <b style="color: gray;font-style: italic;font-size: 11px;">(Facultatif)</b>
                            <input type="hidden" name="oldemail" value="<?php echo $email?>" />
                <?php
                  $data	= array('name'=>'email', 'autocomplete'=>'off','id'=>'email','value'=>set_value('email', $email), 'class'=>'form-control');
                  echo form_input($data); ?>

              </div>


              </div>

						<div class="col-md-6">
              <div class="form-group">

                          <label>ROLE</label> <b style="color: red;font-style: italic;font-size: 11px;">*</b>
                          <select name="user_role" autocomplete="off" id="user_role" class="form-control">
                            <option selected="selected" value="">--Role--</option>
                                                    <?php
                              if(!empty($role)){

                                foreach($role as $st){
                                  if($st->is_user==0){?>
                              <option value="<?php echo $st->id?>" <?php echo ($st->id)?'selected="selected"':'' ?> > <?php echo $st->name?></option>
                            <?php 	}}
                              }
                            ?>
                          </select>


            </div>
					<div class="form-group">

                      		<label><?php echo lang('password')?></label> <b style="color: red;font-style: italic;font-size: 11px;">*</b>
                      		<?php
								$data	= array('name'=>'password','id'=>'password','autocomplete'=>'off', 'class'=>'form-control');
								echo form_password($data); ?>
						</div>
						<div class="form-group">
                      		<label><?php echo lang('password_confirm') ?></label> <b style="color: red;font-style: italic;font-size: 11px;">*</b>
                      		<?php
								$data	= array('name'=>'confirm', 'id'=>'confirm','autocomplete'=>'off','class'=>'form-control');
								echo form_password($data); ?>
						</div>
					  </div>

  </div>

                    </div><!-- /.box-body -->
    				 <div class="box-footer">
                        <button type="submit" class="btn btn-primary">Enregistrer</button>
                    </div>
             <?php form_close()?>
            </div><!-- /.box -->
        </div>
     </div>
</section>
<script src="<?php echo base_url('assets/admin/plugins')?>/jquery-validation/jquery.validate.min.js"></script>
<script src="<?php echo base_url('assets/admin/plugins/toastr')?>/toastr.min.js"></script>
<script type="text/javascript">
$(function() {

$("#signup_form").validate({

    rules: {
    id: { required: true},
    user_role: { required: true},
    password: {required:true},
    confirm: { required: true},
    username: {required:true},
    },messages: {
      id: {
                      required: "Vous devez choisir un employé",
                  },

      user_role: {
                      required: "Vous devez choisir un role",

                  },

      password: {
                      required: "Le mot de passe est obligatoire",

                  },      confirm: {
                                        required: "Le mot de passe confirmation est obligatoire",
                                    },

                        username: {
                                        required: "Le Pseudo est obligatoire",

                                    },

              },submitHandler: function(form) {

      form.submit();
      }
});

  $("#signup_form").validate({
    submitHandler: function(form) {
                 if ($(form).valid())
                   form.submit();
                 return false; // prevent normal form posting
            }
   });

});

</script>

<script>
$(".username").on('change, blur',function() {

	var username	=	$(this).val();
	   if(username){
	   call_loader();
		$.ajax({
			url: '<?php echo site_url('admin/account/check_username_user') ?>',
			type:'POST',
			data:{username:username},
			success:function(result){
				//alert(result);return false;
					$("#overlay1").remove();
				  if(result==1)
					{
						alert('Désolé..Ce Pseudo est est déjà utilisé par un autre utilisateur');
						$(".username").val('');
						$('#username-success').html('');
				   }
				   if(result==0)
					{

				    	$('#username-error').html('');
					}

			 }
		  });
	}

});
</script>
