<?php ob_start();
include("security.php"); ?>
<?php require_once('../setup.php'); ?>
<?php if (isset($_GET['i'])) {
  $transaction_id = base64_decode($_GET['i']);
} else {
  header("Location: dashboard");
  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 -->
  <link rel="stylesheet" href="vendors/datatables.net-bs4/dataTables.bootstrap4.css">
  <link rel="stylesheet" href="vendors/ti-icons/css/themify-icons.css">
  <link rel="stylesheet" type="text/css" href="js/select.dataTables.min.css">
  <link rel="stylesheet" href="vendors/mdi/css/materialdesignicons.min.css">
  <!-- 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">
    <!-- partial:partials/_navbar.html -->
    <?php include('topbar.php'); ?>
    <!-- partial -->
    <div class="container-fluid page-body-wrapper">
      <!-- partial:partials/_sidebar.html -->
      <?php include('nav.php'); ?>
      <!-- partial -->
      <div class="main-panel">
        <div class="content-wrapper">
          <div class="row">
            <div class="col-md-6 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <h4 class="card-title">Enter COT Code!</h4>
                  <p class="card-description">Cost of Transfer is required. Kindly insert your Cost of Transfer Code to facilitate the transfer of your funds.</p>
                  <p class="card-description">
                    <b>Transaction ID</b> <?= $transaction_id; ?>
                  </p>
                  <?php

                  use PHPMailer\PHPMailer\PHPMailer;

                  if (isset($_POST['transfer'])) {
                    require_once("conn.php");
                    $authorize = mysqli_real_escape_string($dbconfig, $_POST['authorize']);
                    $pin = mysqli_real_escape_string($dbconfig, $_POST['pin']);
                    $transaction_id = mysqli_real_escape_string($dbconfig, $_POST['transaction_id']);
                    $transaction_id_coded = base64_encode($transaction_id);

                    $query = mysqli_query($dbconfig, ("SELECT * FROM transactions WHERE transaction_id= '$transaction_id'"));
                    if (mysqli_num_rows($query) > 0) {
                      while ($row0 = $query->fetch_assoc()) {
                        $old_name = $row0['account_name'];
                      }
                    }

                    $query1 = mysqli_query($dbconfig, ("SELECT * FROM cot_code;"));
                    if (mysqli_num_rows($query1) == 1) {
                      while ($row1 = $query1->fetch_assoc()) {
                        $primary_cot = $row1['code'];
                      }
                    }

                    if ($authorize == 'Reject' and $primary_cot == $pin) {
                      $sql = "UPDATE transactions SET status='canceled' WHERE transaction_id='$transaction_id'";
                      if ($dbconfig->query($sql) === TRUE) {
                        header("Refresh: 3; url=dashboard");
                        echo '<div align="center" class=" alert alert-success"><b>Transaction Canceled</b><br>Hold - Redirecting...</div>';
                      }
                    } elseif ($authorize == 'Authorize' and $primary_cot == $pin) {
                      $sql = "UPDATE transactions SET cot_code='$primary_cot' WHERE transaction_id='$transaction_id'";
                      if ($dbconfig->query($sql) === TRUE) {
                        header("Refresh: 15; url=verify-vat?i=$transaction_id_coded");
                        echo '
                            <div class="progress" style="height:35px; background-color: #333;">
                              <div class="progress-bar progress-bar-striped active" style="font-size: 15px; font-weight: bold;" role="progressbar" aria-valuemin="0" aria-valuemax="100"></div>
                            </div>
                            <br>
                          ';
                      }
                    } else {
                      echo '<div align="center" class=" alert alert-danger">
                              <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                              <strong>Warning!</strong> Invalid COT Code.
                            </div>';
                    }
                  }
                  ?>
                  <form class="forms-sample" action="" method="POST">
                    <div class="form-group">
                      <select name="authorize" class="form-control" required>
                        <option value="">-- Approve Transaction --</option>
                        <option value="Authorize">Authorize</option>
                        <option value="Reject">Reject</option>
                      </select>
                    </div>
                    <div class="form-group">
                      <input type="hidden" value="<?= $transaction_id; ?>" name="transaction_id" required>
                      <input type="password" class="form-control" id="exampleInputUsername1" placeholder="Enter COT code here..." name="pin" maxlength="10" required>
                    </div>
                    <button type="submit" class="btn btn-primary mr-2" name="transfer">Continue Transaction</button>
                    <a href="dashboard" class="btn btn-light">Cancel</a>
                  </form>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- content-wrapper ends -->
        <!-- partial:partials/_footer.html -->
        <footer class="footer">
          <?php
          include('footer.php');
          ?>
        </footer>
        <!-- partial -->
      </div>
      <!-- main-panel ends -->
    </div>
    <!-- page-body-wrapper ends -->
  </div>
  <!-- container-scroller -->

  <!-- Progress Bar -->
  <script>
    var progressBar = document.getElementsByClassName('progress-bar')[0];
    var progress = 0;
    startProgress();

    function startProgress() {
      setTimeout(() => {
        progressBar.innerHTML = '<span>Connecting ...</span>';
        progressBar.style.width = '40%';
        progressBar.style.height = '40px';
        progressBar.classList.add('bg-warning')
      }, 250);
      setTimeout(() => {
        progressBar.innerHTML = '<span>Verifying COT Code</span>';
        progressBar.style.width = '60%';
        progressBar.style.height = '40px';
        progressBar.classList.remove('bg-warning')
        progressBar.classList.add('bg-info')
      }, 2000);
      setTimeout(() => {
        progressBar.innerHTML = '<span>COT Code Verified</span>';
        progressBar.style.width = '70%';
        progressBar.style.height = '40px';
        progressBar.classList.remove('bg-info')
        progressBar.classList.add('bg-primary')
      }, 5500);
      setTimeout(() => {
        progressBar.innerHTML = '<span><i>Processing VAT, IMF & OTF ...</i></span>';
        progressBar.style.width = '80%';
        progressBar.style.height = '40px';
        progressBar.classList.remove('bg-primary')
        progressBar.classList.add('bg-success')
      }, 7000);
    }
  </script>

  <!-- plugins:js -->
  <script src="vendors/js/vendor.bundle.base.js"></script>
  <!-- endinject -->
  <!-- Plugin js for this page -->
  <script src="vendors/chart.js/Chart.min.js"></script>
  <script src="vendors/datatables.net/jquery.dataTables.js"></script>
  <script src="vendors/datatables.net-bs4/dataTables.bootstrap4.js"></script>
  <script src="js/dataTables.select.min.js"></script>

  <!-- 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 -->
  <!-- Custom js for this page-->
  <script src="js/dashboard.js"></script>
  <script src="js/Chart.roundedBarCharts.js"></script>
  <!-- End custom js for this page-->
  <?php include('../chat.php'); ?>
</body>

</html>
