<?php
require_once __DIR__ . '/includes/header.php';

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = $_POST['email'] ?? '';
    $password = $_POST['password'] ?? '';
    
    $pdo = db();
    $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ? AND deleted_at IS NULL");
    $stmt->execute([$email]);
    $user = $stmt->fetch();
    
    if ($user && password_verify($password, $user['password'])) {
        if ($user['status'] === 'active') {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['user_name'] = $user['name'];
            $_SESSION['user_email'] = $user['email'];
            $_SESSION['role'] = $user['role'];
            
            // Redirect based on role
            if ($user['role'] === 'admin') {
                header('Location: admin_dashboard.php');
            } else {
                header('Location: dashboard.php');
            }
            exit;
        } else {
            $error = "Your account is inactive. Please contact admin.";
        }
    } else {
        $error = "Invalid email or password.";
    }
}
?>

<div class="container" style="display: flex; justify-content: center; align-items: center; min-height: 80vh; padding: 4rem 0;">
    <div class="box" style="width: 100%; max-width: 500px; padding: 4.5rem;">
        <div class="text-center mb-3">
            <h2 class="fw-black" style="font-size: 2.8rem; color: #fff; margin-bottom: 0.5rem; line-height: 1.1;">Welcome Back</h2>
            <p class="text-secondary fw-bold" style="font-size: 1.1rem;">Sign in to your premium AfgCars portal</p>
        </div>
        
        <?php if ($error): ?>
            <div class="alert alert-error mb-2 text-center" style="justify-content: center;">
                <?= $error ?>
            </div>
        <?php endif; ?>

        <form method="POST">
            <div class="form-group">
                <label class="text-secondary fw-black mb-1" style="text-transform: uppercase; letter-spacing: 2px; font-size: 0.75rem;">Email Address</label>
                <input type="email" name="email" class="form-control" required placeholder="name@example.com">
            </div>
            <div class="form-group">
                <label class="text-secondary fw-black mb-1" style="text-transform: uppercase; letter-spacing: 2px; font-size: 0.75rem;">Security Password</label>
                <input type="password" name="password" class="form-control" required placeholder="••••••••">
            </div>
            <button type="submit" class="btn btn-primary w-full mt-2 fw-black" style="padding: 1.3rem; font-size: 1.1rem; letter-spacing: 1px;">SIGN IN TO ACCOUNT</button>
        </form>
        
        <p class="text-center mt-3 text-secondary fw-bold" style="font-size: 1rem;">
            New to AfgCars? <a href="register.php" class="text-gold" style="text-decoration: none; border-bottom: 1px solid var(--primary-color);">Create your account</a>
        </p>
    </div>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>