<?php
session_start();
ob_start();
?>
<?php require_once('../setup.php'); ?>
<?php if(isset($_GET['i'])){
  $id = base64_decode($_GET['i']);
} else {
  header("Location: login");
  exit();
} ?>
<!DOCTYPE html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title><?= $main_capital_name; ?></title>
  <!-- plugins:css -->
  <link rel="stylesheet" href="vendors/feather/feather.css">
  <link rel="stylesheet" href="vendors/ti-icons/css/themify-icons.css">
  <link rel="stylesheet" href="vendors/css/vendor.bundle.base.css">
  <!-- endinject -->
  <!-- Plugin css for this page -->
  <!-- End plugin css for this page -->
  <!-- inject:css -->
  <link rel="stylesheet" href="css/vertical-layout-light/style.css">
  <!-- endinject -->
  <link rel="shortcut icon" href="../assets/images/logo.png" />
</head>

<body>
  <div class="container-scroller">
    <div class="container-fluid page-body-wrapper full-page-wrapper">
      <div class="content-wrapper d-flex align-items-center auth px-0">
        <div class="row w-100 mx-0">
          <div class="col-lg-4 mx-auto">
            <div class="auth-form-light text-left py-5 px-4 px-sm-5">
              <div class="brand-logo">
                <img src="../assets/images/logo.png" style="width: 50px; display: block; margin-left: auto; margin-right: auto;" alt="logo">
              </div>
              <h4 class="text-center"><?= $main_capital_name; ?></h4>
              <h6 class="font-weight-light text-center"> <small><b>AUTHENTICATION PIN</b></small></h6>
              <hr>
              <div class="row">
                <div class="col-md-12">
                  <?php
                  if (isset($_POST['authenticate'])) {
                    require_once("conn.php");
                    $id = mysqli_real_escape_string($dbconfig, $_POST['id']);
                    $pin = mysqli_real_escape_string($dbconfig, $_POST['pin']);

                    if (!is_numeric($pin)) {
                      echo '<div align="center" class=" alert alert-danger">Your pin must be a numeric value.<br><b>Retype Your Pin!</b></div>';
                    } else {
                      $sql = "SELECT * FROM accounts WHERE id='$id' AND pin='$pin'";
                      $result = $dbconfig->query($sql);
                      if ($result->num_rows == 1) {
                        $_SESSION['hash'] = $id;
                        header("Refresh: 3; url=dashboard");
                        echo '<div align="center" class="alert alert-success"><b>Authentication Successful</b><br>Redirecting, Please wait...</div>';
                        exit();
                      } else {
                        echo '<div align="center" class="alert alert-danger">Invalid Security Pin</div>';
                      }
                    }
                  }
                  ?>
                </div>
              </div>
              <form class="pt-3" action="" method="POST">
                <div class="form-group">
                  <input type="hidden" name="id" value="<?= $id; ?>">
                  <input type="password" class="form-control form-control-lg" id="exampleInputPassword1" placeholder="Enter Online Banking Pin" name="pin" maxlength="4" required>
                </div>
                <div class="mt-3">
                  <button type="submit" name="authenticate" class="btn btn-block btn-primary btn-lg font-weight-medium auth-form-btn">AUTHENTICATE</button>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
      <!-- content-wrapper ends -->
    </div>
    <!-- page-body-wrapper ends -->
  </div>
  <!-- container-scroller -->
  <!-- plugins:js -->
  <script src="vendors/js/vendor.bundle.base.js"></script>
  <!-- endinject -->
  <!-- Plugin js for this page -->
  <!-- End plugin js for this page -->
  <!-- inject:js -->
  <script src="js/off-canvas.js"></script>
  <script src="js/hoverable-collapse.js"></script>
  <script src="js/template.js"></script>
  <script src="js/settings.js"></script>
  <script src="js/todolist.js"></script>
  <!-- endinject -->
  <?php include('../chat.php'); ?>
</body>

</html>
