<?php
// ১. এরর ডিবাগিং চালু (যাতে সাদা পেজ না দেখিয়ে এরর দেখায়)
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once 'common/config.php';

// ২. সেশন চেক (যদি config.php তে না থাকে তবে এখানে নিশ্চিত করা হলো)
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// ৩. ইউজার অলরেডি লগইন থাকলে রিডাইরেক্ট
if (isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit();
}

$error = "";
$redirect_to = isset($_GET['redirect']) ? $_GET['redirect'] : 'index.php';

if (isset($_POST['login'])) {
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = $_POST['password'];

    // ডাটাবেস থেকে ইউজার চেক
    $sql = "SELECT * FROM users WHERE email = '$email'";
    $result = $conn->query($sql);
    
    if ($result && $result->num_rows > 0) {
        $user = $result->fetch_assoc();
        
        // পাসওয়ার্ড চেক
        if (password_verify($password, $user['password'])) {
            // সেশনে ডাটা সেভ করা
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['user_balance'] = isset($user['balance']) ? $user['balance'] : 0;
            
            // রিডাইরেক্ট লজিক (সবচেয়ে গুরুত্বপূর্ণ)
            // সাদা পেজ এড়াতে আউটপুট বাফারিং চেক করা হয়
            if (!headers_sent()) {
                header("Location: " . $redirect_to);
                exit();
            } else {
                echo '<script>window.location.href="'.$redirect_to.'";</script>';
                exit();
            }
        } else {
            $error = "AUTH_FAILED: Access Key is Invalid!";
        }
    } else {
        $error = "AUTH_FAILED: User Identity Not Found!";
    }
}

include 'common/header.php';
?>

<div class="flex items-center justify-center min-h-[calc(100vh-80px)] px-4 py-10">
    <div class="cyber-card w-full max-w-4xl overflow-hidden border-t-4 border-cyan-500/50 shadow-2xl bg-slate-900/90 backdrop-blur-xl">
        <div class="flex flex-col md:flex-row">
            <!-- বাম অংশ: টাইটেল -->
            <div class="md:w-5/12 p-10 flex flex-col items-center justify-center text-center border-b md:border-b-0 md:border-r border-white/5 relative">
                <div class="w-20 h-20 bg-cyan-500/10 rounded-3xl border border-cyan-500/30 flex items-center justify-center mb-6 shadow-[0_0_20px_rgba(34,211,238,0.2)]">
                    <i class="fa-solid fa-fingerprint text-5xl text-cyan-400"></i>
                </div>
                <h2 class="text-3xl font-bold logo-font text-white uppercase leading-none">
                    USER <br><span class="text-cyan-400">LOGIN</span>
                </h2>
                <p class="text-[10px] text-gray-500 uppercase tracking-[3px] mt-6 font-black italic">Terminal Authentication</p>
            </div>

            <!-- ডান অংশ: ইনপুট ফর্ম -->
            <div class="md:w-7/12 p-8 md:p-12">
                <?php if($error): ?>
                    <div class="bg-red-500/10 border border-red-500/30 text-red-500 p-3 rounded-xl mb-6 text-[10px] font-bold text-center uppercase tracking-widest">
                        <i class="fa-solid fa-triangle-exclamation mr-2"></i> <?php echo $error; ?>
                    </div>
                <?php endif; ?>

                <form action="login.php?redirect=<?php echo urlencode($redirect_to); ?>" method="POST" class="space-y-6">
                    <div class="space-y-2">
                        <label class="block text-[10px] font-black text-gray-500 uppercase tracking-[2px] ml-1">Terminal ID (Email)</label>
                        <div class="relative group">
                            <i class="fa-solid fa-at absolute left-4 top-1/2 -translate-y-1/2 text-cyan-500/30 group-focus-within:text-cyan-400 transition-colors"></i>
                            <input type="email" name="email" required placeholder="ENTER EMAIL" 
                                   class="w-full bg-slate-950/50 border border-white/10 rounded-xl py-4 pl-12 pr-4 text-white focus:border-cyan-500 outline-none transition-all text-sm">
                        </div>
                    </div>

                    <div class="space-y-2">
                        <label class="block text-[10px] font-black text-gray-500 uppercase tracking-[2px] ml-1">Access Key (Password)</label>
                        <div class="relative group">
                            <i class="fa-solid fa-lock absolute left-4 top-1/2 -translate-y-1/2 text-cyan-500/30 group-focus-within:text-cyan-400 transition-colors"></i>
                            <input type="password" name="password" required placeholder="••••••••" 
                                   class="w-full bg-slate-950/50 border border-white/10 rounded-xl py-4 pl-12 pr-4 text-white focus:border-cyan-500 outline-none transition-all text-sm tracking-widest">
                        </div>
                    </div>

                    <button type="submit" name="login" 
                            class="w-full bg-gradient-to-r from-blue-600 to-cyan-500 text-white font-black py-4 rounded-xl shadow-lg hover:scale-[1.01] active:scale-95 transition-all uppercase tracking-[4px] text-xs">
                        EXECUTE ACCESS
                    </button>
                </form>

                <div class="mt-8 text-center">
                    <p class="text-[10px] text-gray-500 uppercase tracking-[3px] font-black">
                        New Protocol? <a href="register.php" class="text-cyan-400 border-b border-cyan-500/20 ml-1">Register Identity</a>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .cyber-card { border-radius: 2.5rem; }
    .logo-font { font-family: 'Orbitron', sans-serif; }
</style>

<?php include 'common/footer.php'; ?>