<?php
// Include your session configuration
include_once 'session_config_emp.php';

// --- Handle Session Reset ---
if (isset($_POST['reset_session'])) {
    session_unset();
    session_destroy();
    header("Location: " . $_SERVER['PHP_SELF']);
    exit();
}

// --- Handle Token Match Test (POST) ---
$post_csrf = $_POST['csrf_token'] ?? '';
$session_csrf = $_SESSION['csrf_token'] ?? '';
$tokens_match = ($post_csrf && $session_csrf && hash_equals($session_csrf, $post_csrf));
?>

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSRF & Session Debug</title>
<style>
  body {
    font-family: Consolas, monospace;
    background: #f8f9fa;
    color: #222;
    padding: 20px;
  }
  h2 {
    color: #0069d9;
  }
  pre {
    background: #fff;
    padding: 15px;
    border: 1px solid #ccc;
    border-radius: 8px;
    overflow-x: auto;
  }
  .btn {
    display: inline-block;
    margin: 10px 5px 20px 0;
    padding: 10px 20px;
    background: #0069d9;
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    text-decoration: none;
  }
  .btn:hover {
    background: #0056b3;
  }
  .btn-danger {
    background: #dc3545;
  }
  .btn-danger:hover {
    background: #c82333;
  }
</style>
</head>
<body>

<h2>CSRF & Session Debug Utility</h2>

<!-- Buttons -->
<form method="POST" style="display:inline;">
  <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($_SESSION['csrf_token']); ?>">
  <button type="submit" class="btn">Test CSRF Token Match</button>
</form>

<form method="POST" style="display:inline;">
  <button type="submit" name="reset_session" value="1" class="btn btn-danger">Reset Session</button>
</form>

<pre>
<strong>--- Session Details ---</strong>
Session ID: <?php echo session_id(); ?>

Session Status: <?php echo session_status(); ?> (2 = Active)
CSRF Token in Session: <?php echo $session_csrf ?: 'NOT SET'; ?>

POST CSRF Token: <?php echo $post_csrf ?: 'NOT POSTED'; ?>

Tokens Match: <?php echo $tokens_match ? 'YES ✅' : 'NO ❌'; ?>


<strong>--- Form Data Status ---</strong>
Form Data Exists: <?php echo isset($_SESSION['form_data']) ? 'YES ✅' : 'NO ❌'; ?>


<strong>--- Step Completion Status ---</strong>
<?php
$steps = ['emp1.php', 'emp2.php', 'emp3.php', 'emp4.php', 'emp5.php', 'emp6.php', 'emp7.php'];
foreach ($steps as $step) {
    $step_num = substr($step, 3, 1);
    $completed = isset($_SESSION['form_data']['step_' . $step_num]);
    echo str_pad($step, 12) . ': ' . ($completed ? 'COMPLETED ✅' : 'PENDING ❌') . "\n";
}
?>

<strong>--- Stored Form Data (Partial View) ---</strong>
<?php
if (!empty($_SESSION['form_data'])) {
    print_r(array_slice($_SESSION['form_data'], 0, 30)); // show first 30 items for readability
} else {
    echo "No form data stored.\n";
}
?>
</pre>

</body>
</html>