<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once "../account/include/db.php"; 

session_start();

$error = "";

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';

    if (empty($username) || empty($password)) {
        $error = "Please enter both username and password.";
    } else {
        // 1. Prepare
        $sql = "SELECT id, username, password FROM admins WHERE username = ? LIMIT 1";
        $stmt = mysqli_prepare($conn, $sql);
        
        if ($stmt) {
            // 2. Bind the input
            mysqli_stmt_bind_param($stmt, "s", $username);
            mysqli_stmt_execute($stmt);
            
            // 3. Store result and bind the columns to variables
            mysqli_stmt_store_result($stmt);
            
            if (mysqli_stmt_num_rows($stmt) > 0) {
                // Bind the database columns to these PHP variables
                mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password);
                mysqli_stmt_fetch($stmt);

                // 4. Verify password (Direct string comparison as per your original code)
                if ($password === $db_password) {
                    $_SESSION['admin_id'] = $db_id;
                    $_SESSION['admin_username'] = $db_username;
                    header("Location: dashboard.php");
                    exit();
                } else {
                    $error = "Invalid username or password.";
                }
            } else {
                $error = "Invalid username or password.";
            }
            mysqli_stmt_close($stmt);
        } else {
            $error = "Database error: " . mysqli_error($conn);
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Access</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;500;600&family=Inter:wght@300;400;500;600&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Inter', sans-serif;
            background-color: #0a0e17;
            background-image: 
                radial-gradient(circle at 20% 50%, rgba(16, 185, 129, 0.03) 0%, transparent 50%),
                radial-gradient(circle at 80% 80%, rgba(59, 130, 246, 0.03) 0%, transparent 50%);
        }
        
        .mono {
            font-family: 'JetBrains Mono', monospace;
        }
        
        .hex-bg {
            background-image: url("data:image/svg+xml,%3Csvg width='28' height='49' viewBox='0 0 28 49' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill-rule='evenodd'%3E%3Cg fill='%2310b981' fill-opacity='0.04'%3E%3Cpath d='M13.99 9.25l13 7.5v15l-13 7.5L1 31.75v-15l12.99-7.5zM3 17.9v12.7l10.99 6.34 11-6.35V17.9l-11-6.34L3 17.9zM0 15l12.98-7.5V0h-2v6.35L0 12.69v2.3zm0 18.5L12.98 41v8h-2v-6.85L0 35.81v-2.3zM15 0v7.5L27.99 15H28v-2.31h-.01L17 6.35V0h-2zm0 49v-8l12.99-7.5H28v2.31h-.01L17 42.15V49h-2z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .input-glow:focus {
            box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.15);
        }
        
        .btn-glow:hover {
            box-shadow: 0 0 20px rgba(16, 185, 129, 0.3);
        }
        
        .corner-bracket {
            position: absolute;
            width: 12px;
            height: 12px;
            border: 2px solid rgba(16, 185, 129, 0.3);
        }
        
        .corner-tl { top: -1px; left: -1px; border-right: 0; border-bottom: 0; }
        .corner-tr { top: -1px; right: -1px; border-left: 0; border-bottom: 0; }
        .corner-bl { bottom: -1px; left: -1px; border-right: 0; border-top: 0; }
        .corner-br { bottom: -1px; right: -1px; border-left: 0; border-top: 0; }
    </style>
</head>
<body class="min-h-screen flex items-center justify-center hex-bg">

    <div class="w-full max-w-md px-6">
        
        <!-- Card -->
        <div class="bg-slate-900/80 backdrop-blur-sm border border-slate-800 rounded-xl p-8 relative">
            
            <!-- Corner Brackets -->
            <div class="corner-bracket corner-tl rounded-tl-lg"></div>
            <div class="corner-bracket corner-tr rounded-tr-lg"></div>
            <div class="corner-bracket corner-bl rounded-bl-lg"></div>
            <div class="corner-bracket corner-br rounded-br-lg"></div>
            
            <!-- Header -->
            <div class="text-center mb-8">
                <div class="inline-flex items-center gap-2 mb-4 px-3 py-1 rounded-full bg-emerald-500/10 border border-emerald-500/20">
                    <div class="w-2 h-2 rounded-full bg-emerald-500 animate-pulse"></div>
                    <span class="mono text-xs text-emerald-400 tracking-wider">SECURE TERMINAL</span>
                </div>
                <h1 class="text-2xl font-semibold text-white tracking-tight">Admin Access</h1>
                <p class="text-slate-400 mt-1 text-sm">Authentication Required</p>
            </div>

            <!-- Error Message -->
            <?php if ($error): ?>
            <div class="mb-6 p-3 bg-red-500/10 border border-red-500/20 text-red-400 text-sm rounded-lg flex items-center gap-2 mono text-xs">
                <span class="text-red-500 font-bold">[!]</span>
                <?php echo htmlspecialchars($error); ?>
            </div>
            <?php endif; ?>

            <!-- Login Form -->
            <form method="POST" action="" class="space-y-5">
                
                <!-- Username -->
                <div>
                    <label class="block text-xs font-medium text-slate-400 mb-1.5 mono tracking-wide">USERNAME</label>
                    <div class="relative">
                        <span class="absolute left-3 top-1/2 -translate-y-1/2 text-emerald-600 mono text-sm">&gt;</span>
                        <input 
                            type="text" 
                            name="username" 
                            required 
                            autocomplete="off"
                            class="w-full bg-slate-950 border border-slate-700 text-slate-200 pl-9 pr-4 py-2.5 rounded-lg focus:outline-none focus:border-emerald-600 input-glow transition-all mono text-sm placeholder-slate-600"
                            placeholder="enter_username"
                        >
                    </div>
                </div>

                <!-- Password -->
                <div>
                    <label class="block text-xs font-medium text-slate-400 mb-1.5 mono tracking-wide">PASSWORD</label>
                    <div class="relative">
                        <span class="absolute left-3 top-1/2 -translate-y-1/2 text-emerald-600 mono text-sm">#</span>
                        <input 
                            type="password" 
                            name="password" 
                            required
                            class="w-full bg-slate-950 border border-slate-700 text-slate-200 pl-9 pr-4 py-2.5 rounded-lg focus:outline-none focus:border-emerald-600 input-glow transition-all mono text-sm placeholder-slate-600"
                            placeholder="enter_password"
                        >
                    </div>
                </div>

                <!-- Submit -->
                <button 
                    type="submit" 
                    class="w-full bg-emerald-600 hover:bg-emerald-500 text-white font-medium py-2.5 px-4 rounded-lg transition-all duration-200 btn-glow mono text-sm tracking-wide"
                >
                    [ AUTHENTICATE ]
                </button>

            </form>

            <!-- Footer -->
            <div class="mt-6 pt-4 border-t border-slate-800">
                <div class="flex justify-between items-center">
                    <span class="text-[10px] text-slate-600 mono">ID: <?php echo substr(uniqid(), -6); ?></span>
                    <span class="text-[10px] text-slate-600 mono">ENC: NONE</span>
                </div>
            </div>
        </div>

    </div>

</body>
</html>