<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
require_once __DIR__ . '/../config/db.php';

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

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = isset($_POST['email']) ? trim($_POST['email']) : '';
    $password = isset($_POST['password']) ? trim($_POST['password']) : '';

    if (empty($email) || empty($password)) {
        $error = 'Please fill out both fields.';
    } else {
        try {
            $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ? AND is_admin = TRUE");
            $stmt->execute([$email]);
            $user = $stmt->fetch();

            if ($user && password_verify($password, $user['password'])) {
                $_SESSION['admin_logged_in'] = true;
                $_SESSION['admin_id'] = $user['id'];
                $_SESSION['admin_name'] = $user['full_name'];
                $_SESSION['admin_email'] = $user['email'];
                
                header("Location: index.php");
                exit;
            } else {
                $error = 'Invalid administrative credentials.';
            }
        } catch (Exception $e) {
            $error = 'System error: ' . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Atelier Admin Access | MARTLUX</title>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- Custom fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;500;600;700&family=Playfair+Display:ital,wght@0,400;0,600;0,700;1,400&display=swap" rel="stylesheet">
    <style>
        :root {
            --bg-black: #000000;
            --bg-dark: #0E0E0E;
            --bg-card: #151515;
            --border-gray: #2A2A2A;
            --accent-gold: #DCC7A3;
            --text-white: #FFFFFF;
            --text-gray: #A0A0A0;
            --font-heading: 'Playfair Display', serif;
            --font-body: 'Montserrat', sans-serif;
        }

        body {
            background-color: var(--bg-black);
            color: var(--text-white);
            font-family: var(--font-body);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            overflow: hidden;
        }

        .login-wrapper {
            width: 100%;
            max-width: 420px;
            padding: 20px;
        }

        .login-card {
            background: rgba(21, 21, 21, 0.75);
            backdrop-filter: blur(15px);
            -webkit-backdrop-filter: blur(15px);
            border: 1px solid var(--border-gray);
            border-radius: 24px;
            padding: 2.5rem;
            box-shadow: 0 20px 50px rgba(0, 0, 0, 0.9);
            transition: border-color 0.3s ease;
        }

        .login-card:hover {
            border-color: var(--accent-gold);
        }

        .navbar-logo {
            height: 38px;
            width: auto;
            object-fit: cover;
            object-position: top;
            border-radius: 4px;
        }

        .form-control {
            background-color: var(--bg-dark) !important;
            border: 1px solid var(--border-gray) !important;
            color: var(--text-white) !important;
            padding: 0.8rem 1rem;
            border-radius: 12px;
            font-size: 0.9rem;
            transition: all 0.3s;
        }

        .form-control:focus {
            border-color: var(--accent-gold) !important;
            box-shadow: 0 0 10px rgba(220, 199, 163, 0.15) !important;
        }

        .btn-warning {
            background-color: var(--accent-gold);
            border-color: var(--accent-gold);
            color: #000;
            font-weight: 700;
            padding: 0.9rem;
            border-radius: 50px;
            text-transform: uppercase;
            letter-spacing: 1.5px;
            font-size: 0.8rem;
            transition: all 0.3s cubic-bezier(0.165, 0.84, 0.44, 1);
        }

        .btn-warning:hover {
            background-color: var(--text-white);
            border-color: var(--text-white);
            transform: translateY(-2px);
            box-shadow: 0 8px 16px rgba(255, 255, 255, 0.1);
        }

        .text-warning {
            color: var(--accent-gold) !important;
        }

        .form-label {
            font-size: 0.75rem;
            text-transform: uppercase;
            letter-spacing: 1.5px;
            font-weight: 600;
            color: var(--text-gray);
            margin-bottom: 0.5rem;
        }

        .atelier-title {
            font-family: var(--font-heading);
            font-size: 1.5rem;
            font-weight: 400;
            letter-spacing: 1px;
            margin-top: 1.2rem;
            margin-bottom: 0.3rem;
        }

        .atelier-subtitle {
            font-size: 0.65rem;
            text-transform: uppercase;
            letter-spacing: 3px;
            color: var(--accent-gold);
            font-weight: 700;
            margin-bottom: 2rem;
        }
    </style>
</head>
<body>

<div class="login-wrapper">
    <div class="login-card text-center">
        <!-- Logo -->
        <img src="../assets/images/logo.png" alt="MARTLUX Logo" class="navbar-logo">
        
        <h2 class="atelier-title text-white">Administrative Portal</h2>
        <div class="atelier-subtitle">Atelier Couture</div>

        <?php if (!empty($error)): ?>
            <div class="alert alert-danger border-danger bg-dark text-danger rounded-3 py-2 px-3 small text-start mb-4" role="alert">
                <i class="fa-solid fa-circle-exclamation me-2"></i> <?php echo htmlspecialchars($error); ?>
            </div>
        <?php endif; ?>

        <!-- Form -->
        <form action="login.php" method="POST" class="text-start">
            <div class="mb-3">
                <label for="email" class="form-label">Admin Email</label>
                <input type="email" class="form-control" id="email" name="email" required placeholder="admin@mndirect.com" autocomplete="email">
            </div>
            
            <div class="mb-4">
                <label for="password" class="form-label">Vault Password</label>
                <input type="password" class="form-control" id="password" name="password" required placeholder="••••••••" autocomplete="current-password">
            </div>

            <button type="submit" class="btn btn-warning w-100 mt-2">
                Verify Credentials &rarr;
            </button>
        </form>
        
        <div class="mt-4 pt-2 text-muted" style="font-size: 0.7rem; letter-spacing: 0.5px;">
            <i class="fa-solid fa-lock text-warning me-1"></i> SSL Protected Client Database Server
        </div>
        <div class="mt-2 text-muted text-uppercase fw-bold" style="font-size: 0.65rem; letter-spacing: 1px;">
            Support: <a href="tel:0777101402" class="text-warning text-decoration-none">0777 101 402</a>
        </div>
    </div>
</div>

</body>
</html>
