<?php ob_start();
require_once('../setup.php'); ?>
<?php
if (isset($_GET['e'])) {
  $email = base64_decode($_GET['e']);
} else {
  header("Location: register");
  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-5 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>
              <h5 class="font-weight-light text-center"><b>ONE MORE PROCESS TO GO</b></h5>
              <hr>
              <div class="row">
                <div class="col-md-12">
                  <?php
                  if (isset($_POST['register'])) {
                    require_once("conn.php");

                    $username = mysqli_real_escape_string($dbconfig, $_POST['username']);
                    $pin = mysqli_real_escape_string($dbconfig, $_POST['pin']);
                    $password = mysqli_real_escape_string($dbconfig, $_POST['password']);
                    $repeat_password = mysqli_real_escape_string($dbconfig, $_POST['re_password']);
                    $security_question = mysqli_real_escape_string($dbconfig, $_POST['security_question']);
                    $security_answer = mysqli_real_escape_string($dbconfig, $_POST['security_answer']);

                    $email = $_POST['email'];

                    $username_encoded = base64_encode($username);

                    $query = mysqli_query($dbconfig, ("SELECT username FROM accounts WHERE username= '$username'"));
                    if (mysqli_num_rows($query) > 0) {
                      echo '<div align="center" class=" alert alert-danger">Username Already Taken. <b>Kindly Try Something Else!</b></div>';
                    } elseif (!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>';
                    } elseif ($password != $repeat_password) {
                      echo '<div align="center" class=" alert alert-danger">Passwords Don\'t Match. <b>Retype Your Password!</b></div>';
                    } else {
                      $sql = "UPDATE accounts SET status='pending', username='$username', pin='$pin', password='$password', security_question='$security_question', security_answer='$security_answer' WHERE email='$email'";
                      if ($dbconfig->query($sql) === TRUE) {
                        header("Refresh: 6; url=login?u=$username_encoded");
                        echo '<div align="center" class=" alert alert-success"><b>All Done!<br>Your account is currently awaiting approval, you would get an email once this process is completed.</b><br><b>Redirecting to login...</b></div>';
                      }
                    }
                  }
                  ?>
                  <div class="alert alert-primary text-center"><b>Let's Set Up Your Online Banking...</b></div>
                </div>
              </div>
              <form class="pt-3" action="" method="POST">
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <input type="hidden" name="email" value="<?= $email; ?>" required>
                      <input type="text" class="form-control form-control-lg" id="exampleInputUsername1" placeholder="Username" name="username" maxlength="100" value="<?php if (isset($_POST['username'])) {
                                                                                                                                                                          echo $_POST['username'];
                                                                                                                                                                        } ?>" required>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input type="password" class="form-control form-control-lg" id="exampleInputUsername1" placeholder="4 Digits Transaction Pin" name="pin" maxlength="4" required>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <input type="password" class="form-control form-control-lg" id="exampleInputUsername1" placeholder="Password" name="password" minlength="6" maxlength="50" required>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input type="password" class="form-control form-control-lg" id="exampleInputUsername1" placeholder="Re-type Password" name="re_password" minlength="6" maxlength="50" required>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <input type="text" class="form-control form-control-lg" id="exampleInputUsername1" placeholder="Security Question" name="security_question" maxlength="200" value="<?php if (isset($_POST['security_question'])) {
                                                                                                                                                                                            echo $_POST['security_question'];
                                                                                                                                                                                          } ?>" required>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input type="text" class="form-control form-control-lg" id="exampleInputUsername1" placeholder="Security Answer" name="security_answer" maxlength="200" value="<?php if (isset($_POST['security_question'])) {
                                                                                                                                                                                        echo $_POST['security_question'];
                                                                                                                                                                                      } ?>" required>
                    </div>
                  </div>
                </div>
                <div>
                  <button type="submit" name="register" class="btn btn-block btn-primary btn-lg font-weight-medium auth-form-btn">COMPLETE REGISTRATION</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>
