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

<head>

  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">

  <title><?= $main_name; ?></title>

  <!-- Custom fonts for this template-->
  <link rel="shortcut icon" href="../../assets/images/logo.png" />
  <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">

  <!-- Custom styles for this template -->
  <link href="css/sb-admin-2.min.css" rel="stylesheet">

  <!-- Custom styles for this page -->
  <link href="vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">

</head>

<body id="page-top">

  <!-- Page Wrapper -->
  <div id="wrapper">

    <!-- Sidebar -->
    <?php include('admin_nav.php'); ?>
    <!-- End of Sidebar -->

    <!-- Content Wrapper -->
    <div id="content-wrapper" class="d-flex flex-column">

      <!-- Main Content -->
      <div id="content">

        <!-- Topbar -->
        <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">&nbsp;</nav>
        <!-- End of Topbar -->

        <!-- Begin Page Content -->
        <div class="container-fluid">

          <!-- Page Heading -->
          <h1 class="h3 mb-1 text-primary"><i class="fa fa-key"></i> International Monetary Fund Code</h1>
          <p class="mb-4">To generate a new IMF Code kindly click on the button below.</p>

          <!-- Content Row -->
          <div class="row">

            <!-- Border Left Utilities -->
            <div class="col-lg-6">
              <div class="card mb-4 py-3 border-left-primary">
                <div class="card-body">
                  <?php

                  require("../conn.php");
                  $query1 = mysqli_query($dbconfig, ("SELECT * FROM imf_code;"));
                  if (mysqli_num_rows($query1) == 1) {
                    while ($row1 = $query1->fetch_assoc()) {
                      $primary_imf_id = $row1['id'];
                      $primary_imf = $row1['code'];
                    }
                  }


                  if (isset($_POST['generate'])) {

                    $ran_imf = rand(111111111, 999999999);

                    $sql = "UPDATE imf_code SET code='$ran_imf' WHERE id='$primary_imf_id'";
                    if ($dbconfig->query($sql) === TRUE) {
                      header("Location: admin-imf-code");
                    }
                  }

                  ?>
                  <form action="" method="POST">
                    <div class="form-group">
                      <label for="email">International Monetary Fund Code:</label>
                      <input type="text" class="form-control" name="code" value="<?= $primary_imf; ?>" readonly required>
                    </div>
                    <button type="submit" name="generate" class="btn btn-primary">Generate Another IMF Code</button>
                  </form>
                </div>
              </div>
            </div>

          </div>

        </div>
        <!-- /.container-fluid -->

      </div>
      <!-- End of Main Content -->

    </div>
    <!-- End of Content Wrapper -->

  </div>
  <!-- End of Page Wrapper -->

  <!-- Scroll to Top Button-->
  <a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
  </a>

  <!-- Bootstrap core JavaScript-->
  <script src="vendor/jquery/jquery.min.js"></script>
  <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

  <!-- Core plugin JavaScript-->
  <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

  <!-- Custom scripts for all pages-->
  <script src="js/sb-admin-2.min.js"></script>

  <!-- Page level plugins -->
  <script src="vendor/datatables/jquery.dataTables.min.js"></script>
  <script src="vendor/datatables/dataTables.bootstrap4.min.js"></script>

  <!-- Page level custom scripts -->
  <script src="js/demo/datatables-demo.js"></script>

</body>

</html>
