<?php
// register.php - User Registration with Policy Links
// File location: /bidwheels.net/user/register.php

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

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

$error = '';
$success = '';
$name = '';
$email = '';
$phone = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = sanitize($_POST['name']);
    $email = sanitize($_POST['email']);
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];
    $phone = sanitize($_POST['phone']);
    
    // Validation
    if (empty($name) || empty($email) || empty($password)) {
        $error = 'Please fill in all required fields.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address.';
    } elseif (strlen($password) < 6) {
        $error = 'Password must be at least 6 characters.';
    } elseif ($password !== $confirm_password) {
        $error = 'Passwords do not match.';
    } else {
        $result = registerUser($name, $email, $password, $phone);
        if ($result['success']) {
            $success = $result['message'];
            $name = $email = $phone = '';
        } else {
            $error = $result['message'];
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Account - <?php echo SITE_NAME; ?></title>
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        .register-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;
        }
        
        .register-container {
            max-width: 1200px;
            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 - Info Panel */
        .register-info {
            background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
            padding: 48px 40px;
            color: white;
            display: flex;
            flex-direction: column;
            position: relative;
            overflow: hidden;
        }
        
        .register-info::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;
        }
        
        .info-logo {
            margin-bottom: 40px;
        }
        
        .info-logo img {
            height: 50px;
            width: auto;
        }
        
        .info-title {
            font-size: 32px;
            font-weight: 800;
            margin-bottom: 20px;
            line-height: 1.2;
        }
        
        .info-title span {
            color: #FF8C3E;
        }
        
        .info-text {
            color: #aaa;
            font-size: 15px;
            line-height: 1.6;
            margin-bottom: 40px;
        }
        
        .info-features {
            list-style: none;
            margin-bottom: 40px;
        }
        
        .info-features li {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 16px;
            font-size: 14px;
            color: #ddd;
        }
        
        .info-features li i {
            width: 24px;
            color: #FF8C3E;
            font-size: 18px;
        }
        
        .info-stats {
            display: flex;
            gap: 30px;
            margin-top: auto;
            padding-top: 40px;
            border-top: 1px solid rgba(255,255,255,0.1);
        }
        
        .info-stat {
            text-align: center;
        }
        
        .info-stat .number {
            font-size: 28px;
            font-weight: 700;
            color: #FF8C3E;
        }
        
        .info-stat .label {
            font-size: 12px;
            color: #888;
        }
        
        /* Policy Links in Info Panel */
        .policy-links {
            margin-top: 30px;
            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: 12px;
            transition: color 0.3s;
        }
        
        .policy-links a:hover {
            color: #FF8C3E;
        }
        
        /* Right Side - Form Panel */
        .register-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;
        }
        
        /* 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;
        }
        
        .form-group .required {
            color: #FF8C3E;
        }
        
        .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;
        }
        
        /* Phone input wrapper */
        .phone-input {
            display: flex;
            gap: 12px;
        }
        
        .country-code {
            width: 100px;
            padding: 14px 12px;
            border: 2px solid #f0f0f0;
            border-radius: 16px;
            background: #f8f8f8;
            font-size: 14px;
        }
        
        .phone-input .input-wrapper {
            flex: 1;
        }
        
        .phone-input .input-wrapper input {
            padding-left: 48px;
        }
        
        /* Password strength */
        .password-strength {
            margin-top: 8px;
            display: flex;
            gap: 8px;
            align-items: center;
        }
        
        .strength-bar {
            flex: 1;
            height: 4px;
            background: #f0f0f0;
            border-radius: 4px;
            overflow: hidden;
        }
        
        .strength-fill {
            height: 100%;
            width: 0%;
            background: #dc3545;
            transition: width 0.3s, background 0.3s;
        }
        
        .strength-text {
            font-size: 11px;
            color: #888;
            min-width: 60px;
        }
        
        /* Terms Checkbox */
        .terms-group {
            display: flex;
            align-items: flex-start;
            gap: 12px;
            margin-bottom: 28px;
        }
        
        .terms-group input {
            width: 18px;
            height: 18px;
            cursor: pointer;
            margin-top: 2px;
            accent-color: #FF8C3E;
        }
        
        .terms-group label {
            margin: 0;
            font-size: 13px;
            font-weight: normal;
            color: #666;
            line-height: 1.5;
        }
        
        .terms-group a {
            color: #FF8C3E;
            text-decoration: none;
            font-weight: 600;
        }
        
        .terms-group a:hover {
            text-decoration: underline;
        }
        
        /* Submit Button */
        .register-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;
        }
        
        .register-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-success {
            background: #d4edda;
            color: #155724;
            border-left: 4px solid #28a745;
        }
        
        .alert-danger {
            background: #f8d7da;
            color: #721c24;
            border-left: 4px solid #dc3545;
        }
        
        .alert i {
            font-size: 18px;
        }
        
        /* Responsive */
        @media (max-width: 992px) {
            .register-container {
                grid-template-columns: 1fr;
                max-width: 600px;
            }
            
            .register-info {
                padding: 32px 32px;
                text-align: center;
            }
            
            .info-features li {
                justify-content: center;
            }
            
            .info-stats {
                justify-content: center;
            }
            
            .policy-links {
                justify-content: center;
            }
            
            .register-form-panel {
                padding: 40px 32px;
            }
        }
        
        @media (max-width: 576px) {
            .register-page {
                padding: 40px 16px;
            }
            
            .register-info {
                padding: 24px 20px;
            }
            
            .info-title {
                font-size: 24px;
            }
            
            .info-stats {
                gap: 20px;
            }
            
            .info-stat .number {
                font-size: 22px;
            }
            
            .register-form-panel {
                padding: 30px 20px;
            }
            
            .form-header h2 {
                font-size: 24px;
            }
            
            .phone-input {
                flex-direction: column;
                gap: 10px;
            }
            
            .country-code {
                width: 100%;
            }
        }
    </style>
</head>
<body>

<div class="register-page">
    <div class="register-container">
        <!-- Left Side - Info Panel -->
        <div class="register-info">
            <div class="info-logo">
                <img src="<?php echo SITE_URL . SITE_LOGO; ?>" alt="<?php echo SITE_NAME; ?>">
            </div>
            <div class="info-title">
                Join the <span>Bidding Revolution</span>
            </div>
            <div class="info-text">
                Create an account to start bidding on luxury vehicles, motorcycles, yachts, and more. Get access to exclusive deals and real-time auction updates.
            </div>
            <ul class="info-features">
                <li><i class="fas fa-gavel"></i> Bid on thousands of premium vehicles</li>
                <li><i class="fas fa-bell"></i> Real-time outbid notifications</li>
                <li><i class="fas fa-heart"></i> Save your favorite listings</li>
                <li><i class="fas fa-history"></i> Track your bidding history</li>
                <li><i class="fas fa-shield-alt"></i> Secure and verified transactions</li>
            </ul>
            <div class="info-stats">
                <div class="info-stat">
                    <div class="number">10k+</div>
                    <div class="label">Active Users</div>
                </div>
                <div class="info-stat">
                    <div class="number">5k+</div>
                    <div class="label">Vehicles Sold</div>
                </div>
                <div class="info-stat">
                    <div class="number">98%</div>
                    <div class="label">Satisfaction</div>
                </div>
            </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="register-form-panel">
            <div class="form-header">
                <h2>Create Account</h2>
                <p>Already have an account? <a href="login.php">Sign in here</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; ?>
            
            <?php if ($success): ?>
                <div class="alert alert-success">
                    <i class="fas fa-check-circle"></i>
                    <span><?php echo $success; ?> <a href="login.php" style="color: #155724; font-weight: 600;">Login now</a></span>
                </div>
            <?php endif; ?>
            
            <form method="POST" action="" id="registerForm">
                <div class="form-group">
                    <label><i class="fas fa-user"></i> Full Name <span class="required">*</span></label>
                    <div class="input-wrapper">
                        <i class="fas fa-user"></i>
                        <input type="text" name="name" placeholder="Enter your full name" value="<?php echo htmlspecialchars($name); ?>" required>
                    </div>
                </div>
                
                <div class="form-group">
                    <label><i class="fas fa-envelope"></i> Email Address <span class="required">*</span></label>
                    <div class="input-wrapper">
                        <i class="fas fa-envelope"></i>
                        <input type="email" name="email" placeholder="your@email.com" value="<?php echo htmlspecialchars($email); ?>" required>
                    </div>
                </div>
                
                <div class="form-group">
                    <label><i class="fas fa-phone"></i> Phone Number</label>
                    <div class="phone-input">
                        <select class="country-code" name="country_code">
                            <option value="+1">🇺🇸 +1 (US)</option>
                            <option value="+44">🇬🇧 +44 (UK)</option>
                            <option value="+1">🇨🇦 +1 (Canada)</option>
                            <option value="+61">🇦🇺 +61 (Australia)</option>
                            <option value="+49">🇩🇪 +49 (Germany)</option>
                            <option value="+33">🇫🇷 +33 (France)</option>
                            <option value="+81">🇯🇵 +81 (Japan)</option>
                            <option value="+86">🇨🇳 +86 (China)</option>
                        </select>
                        <div class="input-wrapper">
                            <i class="fas fa-phone"></i>
                            <input type="tel" name="phone" placeholder="Phone number" value="<?php echo htmlspecialchars($phone); ?>">
                        </div>
                    </div>
                </div>
                
                <div class="form-group">
                    <label><i class="fas fa-lock"></i> Password <span class="required">*</span></label>
                    <div class="input-wrapper">
                        <i class="fas fa-lock"></i>
                        <input type="password" name="password" id="password" placeholder="Create a password (min. 6 characters)" required>
                    </div>
                    <div class="password-strength">
                        <div class="strength-bar">
                            <div class="strength-fill" id="strengthFill"></div>
                        </div>
                        <span class="strength-text" id="strengthText">Weak</span>
                    </div>
                </div>
                
                <div class="form-group">
                    <label><i class="fas fa-lock"></i> Confirm Password <span class="required">*</span></label>
                    <div class="input-wrapper">
                        <i class="fas fa-check-circle"></i>
                        <input type="password" name="confirm_password" id="confirmPassword" placeholder="Confirm your password" required>
                    </div>
                </div>
                
                <div class="terms-group">
                    <input type="checkbox" name="terms" id="terms" required>
                    <label for="terms">
                        I agree to the <a href="<?php echo SITE_URL; ?>/terms.php">Terms of Service</a> and 
                        <a href="<?php echo SITE_URL; ?>/privacy.php">Privacy Policy</a>
                    </label>
                </div>
                
                <button type="submit" class="register-btn">
                    <i class="fas fa-user-plus"></i> Create Account
                </button>
            </form>
        </div>
    </div>
</div>

<script>
// Password strength checker
const password = document.getElementById('password');
const strengthFill = document.getElementById('strengthFill');
const strengthText = document.getElementById('strengthText');

password.addEventListener('input', function() {
    const value = this.value;
    let strength = 0;
    let color = '#dc3545';
    let text = 'Weak';
    
    if (value.length >= 6) strength += 1;
    if (value.length >= 8) strength += 1;
    if (/[A-Z]/.test(value)) strength += 1;
    if (/[0-9]/.test(value)) strength += 1;
    if (/[^A-Za-z0-9]/.test(value)) strength += 1;
    
    if (strength >= 4) {
        color = '#28a745';
        text = 'Strong';
    } else if (strength >= 2) {
        color = '#ffc107';
        text = 'Medium';
    } else {
        color = '#dc3545';
        text = 'Weak';
    }
    
    const percentage = (strength / 5) * 100;
    strengthFill.style.width = percentage + '%';
    strengthFill.style.background = color;
    strengthText.textContent = text;
    strengthText.style.color = color;
});

// Password confirmation validation
const confirmPassword = document.getElementById('confirmPassword');
confirmPassword.addEventListener('input', function() {
    if (this.value !== password.value) {
        this.setCustomValidity('Passwords do not match');
    } else {
        this.setCustomValidity('');
    }
});

// Form submission validation
document.getElementById('registerForm').addEventListener('submit', function(e) {
    if (password.value !== confirmPassword.value) {
        e.preventDefault();
        alert('Passwords do not match');
        return false;
    }
    
    const terms = document.getElementById('terms');
    if (!terms.checked) {
        e.preventDefault();
        alert('You must agree to the Terms of Service and Privacy Policy');
        return false;
    }
    
    return true;
});
</script>

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