<?php
session_start();
require_once '../db_connect.php'; // Database file

$error = "";

// If already logged in, redirect to dashboard
if (isset($_SESSION['admin_logged_in'])) {
    header("Location: dashboard");
    exit;
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = trim($_POST['email']);
    $password = md5($_POST['password']); // Kept your existing MD5 logic

    if (!empty($email) && !empty($password)) {
        $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ? AND password = ? AND role = 'admin'");
        $stmt->execute([$email, $password]);
        $admin = $stmt->fetch();

        if ($admin) {
            // Set session on successful login
            $_SESSION['admin_logged_in'] = true;
            $_SESSION['admin_name'] = $admin['name'];
            $_SESSION['admin_id'] = $admin['id'];
            
            header("Location: dashboard");
            exit;
        } else {
            $error = "Invalid email or password, or you do not have admin access!";
        }
    } else {
        $error = "Please provide both email and password!";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login | Ahmad Academy</title>
    
    <link rel="icon" type="image/png" href="http://ahmadonlineacademy.com/assets/images/logo.png">
    
    <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    
    <style>
        :root {
            --primary: #064e3b;        /* Deep Green */
            --primary-dark: #022c22;   /* Darker Green */
            --accent: #d4af37;         /* Golden */
        }

        body { 
            background: linear-gradient(135deg, var(--primary-dark) 0%, var(--primary) 100%);
            font-family: 'Outfit', sans-serif; 
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            overflow: hidden;
        }

        /* Background Shapes for modern look */
        .bg-shape {
            position: absolute;
            border-radius: 50%;
            background: rgba(212, 175, 55, 0.05); /* Subtle Golden Glow */
            z-index: -1;
            animation: float 20s infinite linear;
        }
        @keyframes float {
            0% { transform: translate(0, 0) rotate(0deg); }
            100% { transform: translate(100px, 100px) rotate(360deg); }
        }

        .login-container {
            width: 100%;
            max-width: 420px;
            padding: 20px;
            animation: slideUp 0.8s ease-out;
        }

        .login-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 24px;
            border: 1px solid rgba(212, 175, 55, 0.2); /* Golden border tint */
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
            padding: 45px 35px;
            text-align: center;
        }

        /* Adjusted Logo Box for rectangular logo */
        .logo-box {
            background: #fff;
            padding: 15px 25px;
            border-radius: 15px;
            margin: -75px auto 25px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
            display: inline-block;
            border: 2px solid rgba(212, 175, 55, 0.1);
        }
        .logo-box img { 
            height: auto; 
            max-height: 55px; 
            object-fit: contain; 
        }

        .login-title { color: var(--primary-dark); font-weight: 800; font-size: 26px; margin-bottom: 5px; }
        .login-subtitle { color: #64748b; font-size: 15px; margin-bottom: 35px; font-weight: 500; }

        .form-label { font-weight: 600; color: #475569; font-size: 14px; text-align: left; display: block; margin-bottom: 8px; }
        
        .input-group-custom {
            position: relative;
            margin-bottom: 20px;
        }
        .input-group-custom i {
            position: absolute;
            left: 18px;
            top: 50%;
            transform: translateY(-50%);
            color: #94a3b8;
            transition: 0.3s;
        }
        .form-control {
            border-radius: 12px;
            padding: 14px 18px 14px 50px;
            border: 2px solid #e2e8f0;
            background: #f8fafc;
            font-weight: 500;
            transition: 0.3s;
            font-size: 15px;
        }
        .form-control:focus {
            border-color: var(--accent);
            background: #fff;
            box-shadow: 0 0 0 4px rgba(212, 175, 55, 0.15);
            color: var(--primary-dark);
        }
        .form-control:focus + i { color: var(--accent); }

        .btn-login {
            background: var(--primary);
            color: #fff;
            padding: 14px;
            border-radius: 12px;
            font-weight: 700;
            font-size: 17px;
            border: none;
            width: 100%;
            margin-top: 15px;
            transition: 0.4s;
            box-shadow: 0 8px 20px rgba(6, 78, 59, 0.25);
        }
        .btn-login:hover {
            background: var(--primary-dark);
            transform: translateY(-3px);
            box-shadow: 0 12px 25px rgba(6, 78, 59, 0.35);
            color: var(--accent);
        }

        .alert {
            border-radius: 12px;
            font-size: 14px;
            font-weight: 600;
            margin-bottom: 25px;
            border: none;
            animation: shake 0.5s ease-in-out;
        }

        @keyframes slideUp {
            0% { opacity: 0; transform: translateY(30px); }
            100% { opacity: 1; transform: translateY(0); }
        }
        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-5px); }
            75% { transform: translateX(5px); }
        }

        .footer-text {
            color: rgba(255,255,255,0.6);
            font-size: 13px;
            margin-top: 25px;
            font-weight: 500;
        }
    </style>
</head>
<body>

    <div class="bg-shape" style="width: 350px; height: 350px; top: -100px; left: -100px;"></div>
    <div class="bg-shape" style="width: 250px; height: 250px; bottom: -50px; right: -50px; animation-delay: -5s;"></div>

    <div class="login-container">
        <div class="login-card shadow-lg">
            
            <div class="logo-box">
                <img src="http://ahmadonlineacademy.com/assets/images/logo.png" alt="Ahmad Academy Logo">
            </div>

            <h2 class="login-title">Admin Panel</h2>
            <p class="login-subtitle">Login to access the system</p>

            <?php if($error): ?>
                <div class="alert alert-danger shadow-sm">
                    <i class="fas fa-exclamation-circle me-2"></i> <?php echo $error; ?>
                </div>
            <?php endif; ?>

            <form action="" method="POST">
                
                <div class="text-start">
                    <label class="form-label">Email Address</label>
                    <div class="input-group-custom">
                        <input type="email" name="email" class="form-control" placeholder="admin@example.com" required>
                        <i class="fas fa-envelope"></i>
                    </div>
                </div>

                <div class="text-start">
                    <label class="form-label">Password</label>
                    <div class="input-group-custom">
                        <input type="password" name="password" class="form-control" placeholder="••••••••" required>
                        <i class="fas fa-lock"></i>
                    </div>
                </div>

                <button type="submit" class="btn btn-login">
                    Login <i class="fas fa-sign-in-alt ms-2"></i>
                </button>

            </form>

            <div class="mt-4 border-top pt-3">
                <a href="../" class="text-decoration-none small fw-bold" style="color: var(--primary);">
                    <i class="fas fa-arrow-left me-1"></i> Back to Main Website
                </a>
            </div>
        </div>

        <div class="text-center footer-text">
            &copy; <?php echo date('Y'); ?> Ahmad Academy. All Rights Reserved.
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>