<?php
session_start();
require_once '../admin/admin-config.php';

echo "<h2>Login Test Debug</h2>";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo "<h3>POST Data Received:</h3>";
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    
    $admin_name = trim($_POST['admin_name'] ?? '');
    $admin_password = trim($_POST['admin_password'] ?? '');
    
    echo "<h3>Processed Data:</h3>";
    echo "Username: '$admin_name'<br>";
    echo "Password: '$admin_password'<br><br>";
    
    if (!empty($admin_name) && !empty($admin_password)) {
        echo "<h3>Database Query:</h3>";
        
        $sql = 'SELECT id, admin_name, admin_password FROM admin WHERE admin_name = ?';
        if ($stmt = mysqli_prepare($link, $sql)) {
            mysqli_stmt_bind_param($stmt, 's', $admin_name);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_store_result($stmt);
            
            echo "Rows found: " . mysqli_stmt_num_rows($stmt) . "<br>";
            
            if (mysqli_stmt_num_rows($stmt) == 1) {
                mysqli_stmt_bind_result($stmt, $id, $db_admin_name, $hashed_password);
                if (mysqli_stmt_fetch($stmt)) {
                    echo "Admin ID: $id<br>";
                    echo "Admin Name: $db_admin_name<br>";
                    echo "Stored Hash: $hashed_password<br><br>";
                    
                    echo "<h3>Password Verification:</h3>";
                    $is_valid = password_verify($admin_password, $hashed_password);
                    echo "Password valid: " . ($is_valid ? "✅ YES" : "❌ NO") . "<br>";
                    
                    if ($is_valid) {
                        $_SESSION['admin_id'] = $id;
                        $_SESSION['admin_name'] = $db_admin_name;
                        $_SESSION['loggedin'] = true;
                        
                        echo "<h3>Session Variables Set:</h3>";
                        echo "admin_id: " . $_SESSION['admin_id'] . "<br>";
                        echo "admin_name: " . $_SESSION['admin_name'] . "<br>";
                        echo "loggedin: " . $_SESSION['loggedin'] . "<br>";
                        
                        echo "<br><a href='admin-dashboard.php'>Go to Dashboard</a>";
                    }
                }
            } else {
                echo "No admin found with username: $admin_name";
            }
            mysqli_stmt_close($stmt);
        } else {
            echo "Database error: " . mysqli_error($link);
        }
    }
} else {
    echo "<h3>Current Session:</h3>";
    echo "<pre>";
    print_r($_SESSION);
    echo "</pre>";
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Login Test</title>
</head>
<body>
    <h3>Test Login Form:</h3>
    <form method="post">
        <label>Username:</label><br>
        <input type="text" name="admin_name" value="admin" required><br><br>
        
        <label>Password:</label><br>
        <input type="password" name="admin_password" value="admin123" required><br><br>
        
        <button type="submit">Test Login</button>
    </form>
    
    <br>
    <a href="admin-login.php">Back to Real Login</a>
</body>
</html>