<?php
  $page_title = 'Cambiar contraseña';
  require_once('../../includes/load.php');
  // Checkin What level user has permission to view this page
  page_require_level(5);
?>
<?php $user = current_user(); ?>
<?php
  if(isset($_POST['update'])){

    $req_fields = array('new-password','old-password','id' );
    validate_fields($req_fields);

    if(empty($errors)){

             if(sha1($_POST['old-password']) !== current_user()['password'] ){
               $session->msg('d', "Tu antigua contraseña no coincide");
               redirect('change_password.php',false);
             }

            $id = (int)$_POST['id'];
            $new = remove_junk($db->escape(sha1($_POST['new-password'])));
            $sql = "UPDATE users SET password ='{$new}' WHERE id='{$db->escape($id)}'";
            $result = $db->query($sql);
                if($result && $db->affected_rows() === 1):
                  $session->logout();
                  $session->msg('s',"Inicia sesión con tu nueva contraseña.");
                  redirect('../../index.php', false);
                else:
                  $session->msg('d',' Lo siento, actualización falló.');
                  redirect('change_password.php', false);
                endif;
    } else {
      $session->msg("d", $errors);
      redirect('change_password.php',false);
    }
  }
?>
<?php include_once('../../layouts/header.php'); ?>
<div class="login-page">
  <div class="row col-md-16">
       <?php echo display_msg($msg); ?>
        <form method="post" action="change_password.php" class="clearfix">
          <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"> Cambiar contraseña <i class="fas fa-user-alt"></i></p>
              </div>
            </div>
            <div class="row"> 
                <div class="col-12">
                  <p class="h3 text-primary">Datos de la Contraseña <i class="fas fa-user-circle"></i></p>
                </div>
                <div class="col-12 col-lg-6"> 
                  <p class="text-primary mb-1">Nueva contraseña</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 type="password" class="form-control" name="new-password" required="" placeholder="Nueva contraseña">  
                  </div>
                </div>  
                <div class="col-12 col-lg-6"> 
                  <p class="text-primary mb-1">Antigua contraseña</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 type="password" class="form-control"  required="" name="old-password" placeholder="Antigua contraseña">
                  </div>
                </div>        
              </div>
              <div class="row mb-12">
                  <div class="col-12">
                    <center>
                      <input type="hidden" name="id" value="<?php echo (int)$user['id'];?>">
                      <button type="submit" name="update" class="btn btn-info">Cambiar</button>
                    </center>
                  </div>
                </div>
          </div>
      </form>
  </div>
</div>
<?php include_once('../../layouts/footer.php'); ?>
