<?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: 70vh;">
    <div class="box" style="width: 100%; max-width: 450px; padding: 3.5rem;">
        <div style="text-align: center; margin-bottom: 2.5rem;">
            <h2 style="font-size: 2.2rem; font-weight: 900; margin-bottom: 0.5rem;">Welcome Back</h2>
            <p style="color: var(--text-secondary);">Sign in to your AfgCars account</p>
        </div>
        
        <?php if ($error): ?>
            <div class="glass" style="padding: 1rem; border-color: rgba(255, 71, 87, 0.3); background: rgba(255, 71, 87, 0.05); color: var(--danger); margin-bottom: 1.5rem; border-radius: 12px; font-size: 0.9rem; font-weight: 600; text-align: center;">
                <?= $error ?>
            </div>
        <?php endif; ?>

        <form method="POST">
            <div class="form-group">
                <label style="font-size: 0.85rem; text-transform: uppercase; letter-spacing: 1px; color: var(--text-secondary); font-weight: 700;">Email Address</label>
                <input type="email" name="email" class="form-control" required placeholder="admin@gmail.com" style="margin-top: 0.5rem;">
            </div>
            <div class="form-group">
                <label style="font-size: 0.85rem; text-transform: uppercase; letter-spacing: 1px; color: var(--text-secondary); font-weight: 700;">Password</label>
                <input type="password" name="password" class="form-control" required placeholder="••••••••" style="margin-top: 0.5rem;">
            </div>
            <button type="submit" class="btn btn-primary" style="width: 100%; margin-top: 1.5rem; padding: 1.1rem;">Sign In</button>
        </form>
        
        <p style="text-align: center; margin-top: 2.5rem; color: var(--text-secondary); font-size: 0.95rem;">
            New to AfgCars? <a href="register.php" style="color: var(--primary-color); font-weight: 700; text-decoration: none;">Create an account</a>
        </p>
    </div>
</div>

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