<?php
require_once 'includes/functions.php';

if (isLoggedIn()) {
    redirect(isAdmin() ? ADMIN_URL : USER_URL);
}

$error = '';
$redirectUrl = $_GET['redirect'] ?? '';

// Get settings for logo
$settings = getAllSettings();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = sanitize($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    
    if (empty($email) || empty($password)) {
        $error = 'Please fill in all fields.';
    } else {
        $db = getDBConnection();
        $stmt = $db->prepare("SELECT * FROM users WHERE email = ? AND status = 'active'");
        $stmt->execute([$email]);
        $user = $stmt->fetch();
        
        if ($user && password_verify($password, $user['password'])) {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['user_name'] = $user['name'];
            $_SESSION['user_email'] = $user['email'];
            $_SESSION['user_role'] = $user['role'];
            $_SESSION['user_image'] = $user['profile_image'];
            
            logActivity($user['id'], $user['role'] . '_login', $user['name'] . ' logged in');
            
            if (!empty($redirectUrl)) {
                redirect($redirectUrl);
            } elseif ($user['role'] === 'admin') {
                redirect(ADMIN_URL);
            } else {
                redirect(USER_URL);
            }
        } else {
            $error = 'Invalid email or password.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Traveler</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&family=Playfair+Display:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <link rel="stylesheet" href="<?= BASE_URL ?>assets/css/style.css">
</head>
<body>
    <div class="auth-page">
        <div class="auth-card">
            <div class="auth-header">
                <?php if (($settings['logo_type'] ?? 'image') === 'text'): ?>
                    <?php $textLogo = $settings['text_logo'] ?? 'Traveler'; ?>
                    <?php $textLogoFont = $settings['text_logo_font'] ?? 'Playfair Display'; ?>
                    <?php $textLogoSize = $settings['text_logo_size'] ?? '32'; ?>
                    <?php $textLogoColor = $settings['text_logo_color'] ?? '#FF6B35'; ?>
                    <div class="brand-text text-logo" style="font-family: '<?= htmlspecialchars($textLogoFont, ENT_QUOTES, 'UTF-8') ?>', serif; font-size: <?= htmlspecialchars($textLogoSize, ENT_QUOTES, 'UTF-8') ?>px; color: <?= htmlspecialchars($textLogoColor, ENT_QUOTES, 'UTF-8') ?>; margin-bottom: 10px;">
                        <?= htmlspecialchars($textLogo, ENT_QUOTES, 'UTF-8') ?>
                    </div>
                <?php else: ?>
                    <?php $logoImage = $settings['site_logo'] ?? ''; ?>
                    <?php if (!empty($logoImage) && file_exists(UPLOAD_PATH . 'general/' . $logoImage)): ?>
                        <img src="<?= htmlspecialchars(UPLOAD_URL . 'general/' . $logoImage, ENT_QUOTES, 'UTF-8') ?>" alt="<?= htmlspecialchars($settings['site_name'] ?? 'Logo', ENT_QUOTES, 'UTF-8') ?>" style="max-height:60px; margin-bottom:10px;">
                    <?php else: ?>
                        <div class="brand-text">Travel<span>er</span></div>
                    <?php endif; ?>
                <?php endif; ?>
                <p>Welcome back! Please login to your account.</p>
            </div>
            
            <?php if ($error): ?>
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-circle"></i> <?= $error ?>
                </div>
            <?php endif; ?>
            
            <form method="POST" data-validate>
                <input type="hidden" name="redirect" value="<?= sanitize($redirectUrl) ?>">
                
                <div class="form-group">
                    <label><i class="fas fa-envelope"></i> Email Address</label>
                    <input type="email" name="email" class="form-control" placeholder="Enter your email" required value="<?= sanitize($_POST['email'] ?? '') ?>">
                </div>
                
                <div class="form-group">
                    <label><i class="fas fa-lock"></i> Password</label>
                    <input type="password" name="password" class="form-control" placeholder="Enter your password" required>
                </div>
                
                <button type="submit" class="btn btn-primary btn-block btn-lg">
                    <i class="fas fa-sign-in-alt"></i> Login
                </button>
            </form>
            
            <div class="auth-footer">
                Don't have an account? <a href="<?= BASE_URL ?>register.php">Register Now</a>
            </div>
            
            <div style="text-align:center;margin-top:15px;">
                <a href="<?= BASE_URL ?>" style="color:var(--gray);font-size:0.85rem;">
                    <i class="fas fa-arrow-left"></i> Back to Home
                </a>
            </div>
        </div>
    </div>
    <script src="<?= BASE_URL ?>assets/js/main.js"></script>
</body>
</html>
