<?php

include('php/database.php');
include('php/security.php');
session_start();
if(logged_in())
{
	header('location:../login.php');
}
$id= $_SESSION['id'];
$results= mysqli_query($connection, "SELECT * FROM users WHERE id ='$id'");
$retrive= mysqli_fetch_array($results);
$date = $retrive['date'];
$id = $retrive['id'];
$name = $retrive['name'];
$email = $retrive['email'];
$number = $retrive['number'];
$password = $retrive['password'];
$phone = $retrive['phone'];
$address = $retrive['address'];
$country = $retrive['country'];
$gender = $retrive['gender'];
$image = $retrive['image'];
$vcode = $retrive['vcode'];
$verified = $retrive['verified'];
$verify = $retrive['verify'];
$balance = $retrive['balance'];
$totalbalance = $retrive['totalbalance'];
$investbalance = $retrive['investbalance'];
$profit = $retrive['profit'];
$totaldeposit = $retrive['totaldeposit'];
$totalwithdrawal = $retrive['totalwithdrawal'];
$loanlimit = $retrive['loanlimit'];
$availableloan = $retrive['availableloan'];
$ongoingloan = $retrive['ongoingloan'];
$savings = $retrive['savings'];
$idcard = $retrive['idcard'];
$idcardback = $retrive['idcardback'];
$accountstatus = $retrive['accountstatus'];
$pina = $retrive['pina'];
$pinb = $retrive['pinb'];
$pinc = $retrive['pinc'];
$pind = $retrive['pind'];
$dob = $retrive['dob'];
$vat = $retrive['vat'];

$usernumber = mysqli_real_escape_string($connection, $_GET['number']);
$amount = mysqli_real_escape_string($connection, $_GET['amount']);
$message = mysqli_real_escape_string($connection, $_GET['message']);
  $bank = mysqli_real_escape_string($connection, $_GET['bank']);
$actname = mysqli_real_escape_string($connection, $_GET['actname']);
$routing = mysqli_real_escape_string($connection, $_GET['routing']);
$swift = mysqli_real_escape_string($connection, $_GET['swift']);
?>


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>VAT Code Entry</title>
  <style>
    * {
      box-sizing: border-box;
    }

    html, body {
      margin: 0;
      padding: 0;
      height: 100%;
      background: #f7f9fc;
      font-family: 'Segoe UI', sans-serif;
    }

    /* Shared Preloader Style */
    .preloader, .submit-loader {
      position: fixed;
      top: 0; left: 0;
      width: 100%;
      height: 100%;
      background-color: #ffffff;
      display: none;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      z-index: 9999;
    }

    /* Preloader 1 (Throbber Before Load) */
    .preloader.active {
      display: flex;
    }

    .throbber {
      display: flex;
      gap: 8px;
    }

    .throbber div {
      width: 12px;
      height: 12px;
      background-color: #28a745;
      border-radius: 50%;
      animation: throb 1.2s infinite ease-in-out both;
    }

    .throbber div:nth-child(1) { animation-delay: -0.24s; }
    .throbber div:nth-child(2) { animation-delay: -0.12s; }
    .throbber div:nth-child(3) { animation-delay: 0; }

    @keyframes throb {
      0%, 80%, 100% { transform: scale(0); }
      40% { transform: scale(1); }
    }

    /* Submit Loader (After Form Submit) */
    .submit-loader.active {
      display: flex;
    }

    .spinner {
      border: 4px solid #f3f3f3;
      border-top: 4px solid #28a745;
      border-radius: 50%;
      width: 50px;
      height: 50px;
      animation: spin 1s linear infinite;
      margin-bottom: 15px;
    }

    @keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }

    .loading-text {
      font-size: 16px;
      color: #444;
    }

    /* VAT Code Card */
    .container {
      height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .vat-card {
      background: #fff;
      padding: 40px 30px;
      border-radius: 14px;
      box-shadow: 0 12px 24px rgba(0, 0, 0, 0.06);
      width: 100%;
      max-width: 420px;
      text-align: center;
    }

    .vat-card h2 {
      font-size: 22px;
      margin-bottom: 10px;
      color: #2d3436;
    }

    .vat-card p {
      font-size: 14px;
      color: #555;
      margin-bottom: 25px;
    }

    .vat-input {
      width: 100%;
      padding: 16px;
      font-size: 20px;
      border: 1px solid #ccc;
      border-radius: 10px;
      text-align: center;
      letter-spacing: 6px;
      background-color: #fafafa;
      transition: border-color 0.3s ease;
    }

    .vat-input:focus {
      outline: none;
      border-color: #28a745;
      background-color: #fff;
      box-shadow: 0 0 5px rgba(40,167,69,0.3);
    }

    .btn-verify {
      margin-top: 25px;
      width: 100%;
      padding: 14px;
      background-color: #28a745;
      color: white;
      border: none;
      border-radius: 8px;
      font-size: 16px;
      cursor: pointer;
      transition: background 0.3s ease;
    }

    .btn-verify:hover {
      background-color: #218838;
    }

    .note {
      margin-top: 20px;
      font-size: 12px;
      color: #888;
    }
  </style>
</head>
<body>

<!-- Preloader Before Page Load -->
<div class="preloader active" id="pagePreloader">
  <div class="throbber">
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>

<!-- Loader After Form Submit -->
<div class="submit-loader" id="submitLoader">
  <div class="spinner"></div>
  <div class="loading-text">Validating VAT Code...</div>
</div>

<!-- VAT Code Form -->
<div class="container">
  <div class="vat-card">
    <h2>Enter VAT Code</h2>
    <p>Enter your 6-character VAT code to validate your transaction.</p>
    <form id="vatForm" action="php/transfer-to-bank.php" method="POST" onsubmit="return validateVAT()">
  <input
    type="text"
    name="vat_code"
    id="vat_code"
    maxlength="6"
    placeholder="ABC123"
    class="vat-input"
    oninput="this.value = this.value.toUpperCase().replace(/[^A-Z0-9]/g, '')"
    required
  />
   <input type="hidden" name="number" value="<?php echo $usernumber?>">
					  <input type="hidden" name="amount" value="<?php echo $amount?>">
					  <input type="hidden" name="message" value="<?php echo $message?>">
					  <input type="hidden" name="actname" value="<?php echo $actname?>">
					  <input type="hidden" name="bank" value="<?php echo $bank?>">
					  <input type="hidden" name="routing" value="<?php echo $routing?>">
					  <input type="hidden" name="swift" value="<?php echo $swift?>">
  <button type="submit" class="btn-verify">Validate VAT</button>
  <div id="vat-error" style="color: red; font-size: 13px; margin-top: 10px;"></div>
</form>

    <div class="note">Your VAT code is required for tax verification and compliance.</div>
  </div>
</div>

<script>
  // Hide page preloader on load
  window.addEventListener("load", function () {
    const pagePreloader = document.getElementById("pagePreloader");
    setTimeout(() => {
      pagePreloader.classList.remove("active");
    }, 2000); // realistic small delay
  });

  // Show submit loader on form submission
  const form = document.getElementById("vatForm");
  const submitLoader = document.getElementById("submitLoader");

  form.addEventListener("submit", function (e) {
    e.preventDefault();
    submitLoader.classList.add("active");

    // Simulate processing
    setTimeout(() => {
      submitLoader.classList.remove("active");
      
      // form.submit(); // enable this in real case
    }, 3000);
  });
</script>

<script>
  const expectedVAT = "<?php echo $vat; ?>"; // Value from PHP backend

  function validateVAT() {
    const input = document.getElementById("vat_code").value.trim();
    const errorDiv = document.getElementById("vat-error");

    if (input !== expectedVAT) {
      errorDiv.textContent = "VAT code is invalid. Please contact support or enter the correct one.";
      return false;
    }

    errorDiv.textContent = "";
    document.getElementById("submitLoader").classList.add("active");

    setTimeout(() => {
      document.getElementById("submitLoader").classList.remove("active");
      document.getElementById("vatForm").submit();
    }, 3000);

    return false; // prevent actual submission until after timeout
  }
</script>

</body>
</html>

