<?php
ob_start(); // ADD THIS LINE
// login.php - User Login with Professional Design
// File location: /bidwheels.net/user/login.php

require_once '../includes/auth.php';
require_once '../includes/header.php';

// Redirect if already logged in
if (isLoggedIn()) {
    redirect('/index.php');
}

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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = sanitize($_POST['email']);
    $password = $_POST['password'];
    $remember = isset($_POST['remember']) ? true : false;
    
    if (empty($email) || empty($password)) {
        $error = 'Please enter both email and password.';
    } else {
        $result = loginUser($email, $password);
        if ($result['success']) {
            // Set remember me cookie if checked
            if ($remember) {
                setcookie('remember_email', $email, time() + (86400 * 30), "/");
            } else {
                setcookie('remember_email', '', time() - 3600, "/");
            }
            redirect('/index.php');
        } else {
            $error = $result['message'];
        }
    }
}

// Check for remember me cookie
$remembered_email = isset($_COOKIE['remember_email']) ? $_COOKIE['remember_email'] : '';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - <?php echo SITE_NAME; ?></title>
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        .login-page {
            min-height: calc(100vh - 200px);
            background: linear-gradient(135deg, #EFEBE0 0%, #e5e0d4 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 60px 24px;
        }
        
        .login-container {
            max-width: 1100px;
            width: 100%;
            margin: 0 auto;
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 0;
            background: white;
            border-radius: 32px;
            overflow: hidden;
            box-shadow: 0 25px 50px rgba(0,0,0,0.1);
        }
        
        /* Left Side - Welcome Panel */
        .login-welcome {
            background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
            padding: 48px 40px;
            color: white;
            display: flex;
            flex-direction: column;
            position: relative;
            overflow: hidden;
        }
        
        .login-welcome::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,140,62,0.1) 0%, transparent 70%);
            pointer-events: none;
        }
        
        .welcome-logo {
            margin-bottom: 40px;
        }
        
        .welcome-logo img {
            height: 50px;
            width: auto;
        }
        
        .welcome-title {
            font-size: 36px;
            font-weight: 800;
            margin-bottom: 20px;
            line-height: 1.2;
        }
        
        .welcome-title span {
            color: #FF8C3E;
        }
        
        .welcome-text {
            color: #aaa;
            font-size: 15px;
            line-height: 1.6;
            margin-bottom: 40px;
        }
        
        .welcome-features {
            list-style: none;
            margin-bottom: 40px;
        }
        
        .welcome-features li {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 16px;
            font-size: 14px;
            color: #ddd;
        }
        
        .welcome-features li i {
            width: 24px;
            color: #FF8C3E;
            font-size: 18px;
        }
        
        .welcome-quote {
            margin-top: auto;
            padding-top: 40px;
            border-top: 1px solid rgba(255,255,255,0.1);
            font-style: italic;
            color: #888;
            font-size: 14px;
        }
        
        .welcome-quote i {
            color: #FF8C3E;
            margin-right: 8px;
        }
        
        .policy-links {
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid rgba(255,255,255,0.1);
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
        }
        
        .policy-links a {
            color: #aaa;
            text-decoration: none;
            font-size: 11px;
            transition: color 0.3s;
        }
        
        .policy-links a:hover {
            color: #FF8C3E;
        }
        
        /* Right Side - Form Panel */
        .login-form-panel {
            padding: 48px 48px;
            background: white;
        }
        
        .form-header {
            margin-bottom: 32px;
        }
        
        .form-header h2 {
            font-size: 28px;
            font-weight: 700;
            color: #1a1a1a;
            margin-bottom: 8px;
        }
        
        .form-header p {
            color: #666;
            font-size: 14px;
        }
        
        .form-header p a {
            color: #FF8C3E;
            text-decoration: none;
            font-weight: 600;
        }
        
        .form-header p a:hover {
            text-decoration: underline;
        }
        
        /* Demo Credentials Box */
        .demo-box {
            background: #fef5ee;
            border-left: 4px solid #FF8C3E;
            border-radius: 16px;
            padding: 16px 20px;
            margin-bottom: 28px;
        }
        
        .demo-title {
            font-size: 13px;
            font-weight: 700;
            color: #1a1a1a;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .demo-title i {
            color: #FF8C3E;
        }
        
        .demo-row {
            display: flex;
            gap: 20px;
            flex-wrap: wrap;
        }
        
        .demo-item {
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 13px;
            background: white;
            padding: 6px 14px;
            border-radius: 30px;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .demo-item:hover {
            background: #FF8C3E;
            color: white;
        }
        
        .demo-item .label {
            color: #888;
        }
        
        .demo-item .value {
            color: #FF8C3E;
            font-weight: 600;
            font-family: monospace;
        }
        
        .demo-item:hover .label,
        .demo-item:hover .value {
            color: white;
        }
        
        /* Form Styles */
        .form-group {
            margin-bottom: 24px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            font-size: 13px;
            color: #333;
        }
        
        .form-group label i {
            color: #FF8C3E;
            margin-right: 6px;
        }
        
        .input-wrapper {
            position: relative;
        }
        
        .input-wrapper i {
            position: absolute;
            left: 16px;
            top: 50%;
            transform: translateY(-50%);
            color: #aaa;
            font-size: 16px;
        }
        
        .input-wrapper input {
            width: 100%;
            padding: 14px 16px 14px 48px;
            border: 2px solid #f0f0f0;
            border-radius: 16px;
            font-size: 14px;
            transition: all 0.3s;
            background: #f8f8f8;
        }
        
        .input-wrapper input:focus {
            outline: none;
            border-color: #FF8C3E;
            background: white;
        }
        
        .input-wrapper input::placeholder {
            color: #bbb;
        }
        
        /* Password Toggle */
        .password-toggle {
            position: absolute;
            right: 16px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #aaa;
            background: none;
            border: none;
            font-size: 16px;
        }
        
        .password-toggle:hover {
            color: #FF8C3E;
        }
        
        /* Form Options */
        .form-options {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 28px;
            flex-wrap: wrap;
            gap: 12px;
        }
        
        .checkbox-group {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .checkbox-group input {
            width: 16px;
            height: 16px;
            cursor: pointer;
            accent-color: #FF8C3E;
        }
        
        .checkbox-group label {
            margin: 0;
            font-size: 13px;
            font-weight: normal;
            color: #666;
            cursor: pointer;
        }
        
        .forgot-link {
            color: #FF8C3E;
            text-decoration: none;
            font-size: 13px;
            font-weight: 500;
            transition: color 0.3s;
        }
        
        .forgot-link:hover {
            text-decoration: underline;
        }
        
        /* Login Button */
        .login-btn {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #FF8C3E, #e67a22);
            border: none;
            border-radius: 50px;
            color: white;
            font-weight: 700;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }
        
        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(255,140,62,0.3);
        }
        
        /* Alert Styles */
        .alert {
            padding: 14px 18px;
            border-radius: 16px;
            margin-bottom: 24px;
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 13px;
        }
        
        .alert-danger {
            background: #f8d7da;
            color: #721c24;
            border-left: 4px solid #dc3545;
        }
        
        .alert i {
            font-size: 18px;
        }
        
        /* Responsive */
        @media (max-width: 992px) {
            .login-container {
                grid-template-columns: 1fr;
                max-width: 550px;
            }
            
            .login-welcome {
                padding: 32px 32px;
                text-align: center;
            }
            
            .welcome-features li {
                justify-content: center;
            }
            
            .policy-links {
                justify-content: center;
            }
            
            .login-form-panel {
                padding: 40px 32px;
            }
        }
        
        @media (max-width: 576px) {
            .login-page {
                padding: 40px 16px;
            }
            
            .login-welcome {
                padding: 24px 20px;
            }
            
            .welcome-title {
                font-size: 28px;
            }
            
            .login-form-panel {
                padding: 30px 20px;
            }
            
            .form-header h2 {
                font-size: 24px;
            }
            
            .form-options {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .demo-row {
                flex-direction: column;
                gap: 10px;
            }
        }
    </style>
</head>
<body>

<div class="login-page">
    <div class="login-container">
        <!-- Left Side - Welcome Panel -->
        <div class="login-welcome">
            <div class="welcome-logo">
                <img src="<?php echo SITE_URL . SITE_LOGO; ?>" alt="<?php echo SITE_NAME; ?>">
            </div>
            <div class="welcome-title">
                Welcome <span>Back!</span>
            </div>
            <div class="welcome-text">
                Sign in to access your dashboard, track your bids, and discover exclusive vehicle auctions.
            </div>
            <ul class="welcome-features">
                <li><i class="fas fa-gavel"></i> Manage your active bids</li>
                <li><i class="fas fa-bell"></i> Get outbid notifications</li>
                <li><i class="fas fa-heart"></i> Save favorite listings</li>
                <li><i class="fas fa-chart-line"></i> Track auction history</li>
            </ul>
            <div class="welcome-quote">
                <i class="fas fa-quote-left"></i> The best deals come to those who bid.
            </div>
            <div class="policy-links">
                <a href="<?php echo SITE_URL; ?>/privacy.php">Privacy Policy</a>
                <a href="<?php echo SITE_URL; ?>/terms.php">Terms of Service</a>
                <a href="<?php echo SITE_URL; ?>/refund.php">Refund Policy</a>
                <a href="<?php echo SITE_URL; ?>/auction-policies.php">Auction Policies</a>
            </div>
        </div>
        
        <!-- Right Side - Form Panel -->
        <div class="login-form-panel">
            <div class="form-header">
                <h2>Sign In</h2>
                <p>Don't have an account? <a href="register.php">Create one now</a></p>
            </div>
            
           
            
            <?php if ($error): ?>
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-circle"></i>
                    <span><?php echo $error; ?></span>
                </div>
            <?php endif; ?>
            
            <form method="POST" action="" id="loginForm">
                <div class="form-group">
                    <label><i class="fas fa-envelope"></i> Email Address</label>
                    <div class="input-wrapper">
                        <i class="fas fa-envelope"></i>
                        <input type="email" name="email" id="email" placeholder="your@email.com" value="<?php echo htmlspecialchars($remembered_email ?: $email); ?>" required>
                    </div>
                </div>
                
                <div class="form-group">
                    <label><i class="fas fa-lock"></i> Password</label>
                    <div class="input-wrapper">
                        <i class="fas fa-lock"></i>
                        <input type="password" name="password" id="password" placeholder="Enter your password" required>
                        <button type="button" class="password-toggle" onclick="togglePassword()">
                            <i class="fas fa-eye-slash" id="toggleIcon"></i>
                        </button>
                    </div>
                </div>
                
                <div class="form-options">
                    <div class="checkbox-group">
                        <input type="checkbox" name="remember" id="remember" <?php echo $remembered_email ? 'checked' : ''; ?>>
                        <label for="remember">Remember me</label>
                    </div>
                    <a href="forgot_password.php" class="forgot-link">Forgot password?</a>
                </div>
                
                <button type="submit" class="login-btn">
                    <i class="fas fa-sign-in-alt"></i> Sign In
                </button>
            </form>
        </div>
    </div>
</div>

<script>
// Password visibility toggle
function togglePassword() {
    const passwordInput = document.getElementById('password');
    const toggleIcon = document.getElementById('toggleIcon');
    
    if (passwordInput.type === 'password') {
        passwordInput.type = 'text';
        toggleIcon.classList.remove('fa-eye-slash');
        toggleIcon.classList.add('fa-eye');
    } else {
        passwordInput.type = 'password';
        toggleIcon.classList.remove('fa-eye');
        toggleIcon.classList.add('fa-eye-slash');
    }
}

// Fill demo credentials
function fillDemoCredentials(email, password) {
    document.getElementById('email').value = email;
    document.getElementById('password').value = password;
}

// Form validation
document.getElementById('loginForm').addEventListener('submit', function(e) {
    const email = document.getElementById('email').value.trim();
    const password = document.getElementById('password').value;
    
    if (!email) {
        e.preventDefault();
        alert('Please enter your email address');
        return false;
    }
    
    if (!password) {
        e.preventDefault();
        alert('Please enter your password');
        return false;
    }
    
    return true;
});
</script>

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