<?php
  $page_title = 'Agregar usuarios';
  require_once('../../includes/load.php');
  $groups = find_all('user_groups');
?>
<?php
  if(isset($_POST['add_user'])){
   if(empty($errors)){
       $name   = remove_junk($db->escape($_POST['full-name']));
       $username   = remove_junk($db->escape($_POST['username']));
       $password   = remove_junk($db->escape($_POST['password']));
       $user_level = (int)$db->escape($_POST['level']);
       $password = sha1($password);
       $status = remove_junk($db->escape($_POST['status']));
       $last_name = remove_junk($db->escape($_POST['last_name']));
       $sex = remove_junk($db->escape($_POST['sex']));
       $celular = remove_junk($db->escape($_POST['celular']));
       $cargo = remove_junk($db->escape($_POST['cargo']));
       $licencia = remove_junk($db->escape($_POST['licencia']));
       $id_area = remove_junk($db->escape($_POST['id_area']));
       
       $sql = "select * from users where username='$username'";
       $result = $db->query($sql);
       if($db->num_rows($result)>0) {
           $session->msg('d',' El DNI ya Existe.');
          redirect('add_user.php', false);
       } else {
        if(empty($errors)){

        $query = "INSERT INTO users (";
        $query .="name,username,password,user_level,status,last_name, sex, celular, cargo, licencia, id_area";
        $query .=") VALUES (";
        $query .=" '{$name}', '{$username}', '{$password}', '{$user_level}', '{$status}', '{$last_name}', '{$sex}', '{$celular}', '{$cargo}', '{$licencia}', '{$id_area}'";
        $query .=")";
        if($db->query($query)){
          //sucess
          $session->msg('s'," Cuenta de usuario ha sido creada");
          redirect('users.php', false);
        } else {
          //failed
          $session->msg('d',' No se pudo crear la cuenta.');
          redirect('add_user.php', false);
        }
     }else {
     $session->msg("d", $errors);
      redirect('add_user.php',false);
   }
 }
}
}
?>
<?php include_once('../../layouts/header.php'); ?>
  <?php echo display_msg($msg); ?>
  <div class="row col-md-16">
    <div class="panel panel-default">
          <form method="post" action="add_user.php">
            <div class="container pt-1 mt-1 animated fadeIn">
              <div class="row pt-4">
                <div class="col-12 mb-3">
                  <p class="h1 text-center text-primary font-weight-bold"> Registrar Usuario <i class="fas fa-user-alt"></i></p>
                </div>
              </div> 
              
              <div class="row"> 
                <div class="col-12">
                  <p class="h3 text-primary">Datos Generales <i class="fas fa-user-circle"></i></p>
                </div>
                <div class="col-12 col-lg-6"> 
                  <p class="text-primary mb-1">DNI</p>
                  <div class="input-group mb-3"> 
                    <div class="input-group-prepend">
                      <span class="input-group-text"><i class="fas fa-id-card"></i></span>
                    </div>
                    <input maxlength="8"  placeholder="DNI" aria-label="DNI"  type="text" autocomplete="off" name="username" id="username" class="form-control is-requerido" required>
                       
                  </div>
                </div>         
              </div> 
              <div class="row">
                <div class="col-12 col-lg-6">
                  <p class="text-primary mb-1">Nombres</p>
                  <div class="input-group mb-3">
                    <div class="input-group-prepend">
                      <span class="input-group-text"><i class="fas fa-user"></i></span>
                    </div>
                    <input  maxlength="50" type="text" class="form-control is-requerido " placeholder="Nombres" autocomplete="off" name="full-name" id="full-name" aria-label="Nombres" required>
                  </div>
                  
                </div>
                <div class="col-12 col-lg-6">
                  <p class="text-primary mb-1">Apellidos</p>
                  <div class="input-group mb-3">
                    <div class="input-group-prepend">
                      <span class="input-group-text"><i class="fas fa-user"></i></span>
                    </div>
                    <input  maxlength="50" type="text" class="form-control is-requerido " placeholder="Apellidos" name="last_name" id="last_name" required>
                  </div>
                </div>
                <div class="col-12 col-lg-6"> 
                  <p class="text-primary mb-1">Sexo</p>
                  <div class="input-group mb-3">
                    <div class="input-group-prepend">
                      <label class="input-group-text" for="selectTipo"><i class="fas fa-user-cog"></i></label>
                    </div> 
                    <select class="custom-select is-requerido" id="sex" name="sex" required>
                      <option value="" selected disabled>SELECCIONE</option>
                      <option value="1" >FEMENINO</option>
                      <option value="2" >MASCULINO</option>

                    </select>
                  </div>
                </div>
                <div class="col-12 col-lg-6">
                  <p class="text-primary mb-1">Celular</p>
                  <div class="input-group mb-3">
                    <div class="input-group-prepend">
                      <span class="input-group-text"><i class="fas fa-phone"></i></span>
                    </div>
                    <input maxlength="9" type="text" class="form-control is-requerido" placeholder="N° de Celular" name="celular" id="celular" required>
                  </div>
                </div>
                <div class="col-12 col-lg-6">
                  <p class="text-primary mb-1">Licencia</p>
                  <div class="input-group mb-3">
                    <div class="input-group-prepend">
                      <span class="input-group-text"><i class="fas fa-id-card"></i></span>
                    </div>
                    <input maxlength="9" type="text" class="form-control is-requerido" placeholder="N° de Licencia" name="licencia" id="licencia" required>
                  </div>
                </div>
                <div class="col-12 col-lg-6"> 
                  <p class="text-primary mb-1">Estado</p>
                  <div class="input-group mb-3">
                    <div class="input-group-prepend">
                      <label class="input-group-text" for="selectTipo"><i class="fas fa-user-cog"></i></label>
                    </div> 
                    <select class="custom-select is-requerido" id="status" name="status" required>
                      <option value="" selected disabled>SELECCIONE</option>
                      <option value="1" >ACTIVO</option>
                      <option value="0" >NO ACTIVO</option>

                    </select>
                  </div>
                </div>
                <div class="col-12 col-lg-6">
                  <p class="text-primary mb-1">Cargo</p>
                  <div class="input-group mb-3">
                    <div class="input-group-prepend">
                      <span class="input-group-text"><i class="fas fa-user"></i></span>
                    </div>
                    <input maxlength="30" type="text" class="form-control is-requerido " placeholder="Nombre de cargo" name="cargo" id="cargo" required>
                  </div>
                </div>
                <div class="col-12 col-lg-6">
                  <p class="text-primary mb-1">Gerencia</p>
                  <div class="input-group mb-3">
                    <div class="input-group-prepend">
                      <span class="input-group-text"><i class="fas fa-user"></i></span>
                    </div>
                   
                    <select class="custom-select is-requerido" name="id_gerencia" id="id_gerencia" required>
                      <option value="" disabled selected>SELECCIONE</option>
                        <?php   
                          $consulta = $db->query("SELECT * FROM gerencia");
                            while ($row = $db->fetch_assoc($consulta)) {   
                              $id = $row['id'];  
                              $NOMB_GER = $row['NOMB_GER'];   
                              echo "<option value='$id'>$NOMB_GER</option>";
                            }
                        ?>                    
                    </select>
                  </div>
                </div>
                <div class="col-12 col-lg-6">
                  <p class="text-primary mb-1">Área</p>
                  <div class="input-group mb-3">
                    <div class="input-group-prepend">
                      <span class="input-group-text"><i class="fas fa-user"></i></span>
                    </div>
                   
                    <select class="custom-select is-requerido" required name="id_area" id="id_area" required>
                      <option value="" selected disabled>SELECCIONE GERENCIA</option>
                    </select>
                  </div>
                </div>
                <div class="col-12">
                  <p class="h3 text-primary">Autentificación <i class="fas fa-id-card"></i></p>
                </div>
               
                <div class="col-12">
                  <p class="text-primary mb-1">Contraseña</p>
                  <div class="input-group mb-3">
                    <div class="input-group-prepend">
                      <span class="input-group-text"><i class="fas fa-unlock-alt"></i></span>
                    </div>
                    <input type="password" class="form-control" placeholder="Contraseña" autocomplete="new-password" name ="password" id="password" required>
                  </div>
                </div>
                <div class="col-12 col-lg-6"> 
                  <p class="text-primary mb-1">Tipo de Usuario</p>
                  <div class="input-group mb-3">
                    <div class="input-group-prepend">
                      <label class="input-group-text" for="selectTipo"><i class="fas fa-user-cog"></i></label>
                    </div> 
                      <select class="custom-select is-requerido" name="level" id="level" required>
                           <option value="" selected disabled>SELECCIONE</option>
                          <?php foreach ($groups as $group ):?>
                           <option value="<?php echo $group['group_level'];?>"><?php echo ucwords($group['group_name']);?></option>
                          <?php endforeach;?>
                      </select>
                  </div>
                </div>
              </div>    
              <div class="row mb-4">
                <div class="col-12">
                  <button class="btn btn-block btn-primary" type="submit" name="add_user">Registrar Usuario</button>
                </div>
              </div>
            </div>
        </form>
        </div>
      </div>
    </div>
<?php include_once('../../layouts/footer.php'); ?>
<script type="text/javascript" src="js/add_user.js"></script>
