<?php
/**
 * Tom's Java Jive - Customer Login
 */

$pageTitle = "Login - Tom's Java Jive";
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/auth.php';

// Redirect if already logged in
if (CustomerAuth::isLoggedIn()) {
    redirect('/account/');
}

$error = '';
$email = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    
    if (empty($email) || empty($password)) {
        $error = 'Please enter your email and password.';
    } else {
        if (CustomerAuth::login($email, $password)) {
            $redirectTo = $_SESSION['redirect_after_login'] ?? '/account/';
            unset($_SESSION['redirect_after_login']);
            redirect($redirectTo);
        } else {
            $error = 'Invalid email or password.';
        }
    }
}

$metaTitle = "Sign In | Tom's Java Jive";
$metaDescription = "Sign in to your Tom's Java Jive account.";
$canonicalUrl = 'https://tomsjavajive.com/login.php';
$metaRobots = "noindex, nofollow";
$suppressSchema = true;
require_once __DIR__ . '/includes/header.php';
?>

<section class="section" style="padding-top: 2rem;">
    <div class="container">
        <div class="card" style="max-width: 450px; margin: 0 auto;">
            <div class="card-body">
                <h1 style="text-align: center; margin-bottom: 1.5rem;">Welcome Back</h1>
                
                <?php if ($error): ?>
                    <div class="alert alert-error">
                        <i class="fas fa-exclamation-circle"></i>
                        <?= htmlspecialchars($error) ?>
                    </div>
                <?php endif; ?>
                
                <form method="POST" action="">
                    <div class="form-group">
                        <label class="form-label" for="email">Email Address</label>
                        <input type="email" id="email" name="email" class="form-input" 
                               value="<?= htmlspecialchars($email) ?>" required autofocus>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label" for="password">Password</label>
                        <input type="password" id="password" name="password" class="form-input" required>
                    </div>
                    
                    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem;">
                        <label class="form-checkbox">
                            <input type="checkbox" name="remember"> Remember me
                        </label>
                        <a href="/forgot-password.php">Forgot password?</a>
                    </div>
                    
                    <button type="submit" class="btn btn-primary btn-lg btn-block">
                        Sign In
                    </button>
                </form>
                
                <hr style="margin: 1.5rem 0;">
                
                <p style="text-align: center; margin-bottom: 1rem;">
                    Don't have an account? <a href="/register.php">Create one</a>
                </p>
                
                <p style="text-align: center; color: var(--color-text-muted); font-size: 0.875rem;">
                    Or <a href="/track-order.php">track your order</a> with your order number
                </p>
            </div>
        </div>
    </div>
</section>

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