<?php
include 'header.php'; // Ensure this includes DB connection ($link)

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$alert_script = '';

// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Fetch and sanitize form input
    $name     = trim($_POST['name'] ?? '');
    $email    = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirm  = $_POST['password_confirmation'] ?? '';
    $terms    = isset($_POST['terms']) ? true : false;

    $errors = [];

    // Validation
    if (empty($name))     $errors[] = "Full Name is required.";
    if (empty($email))    $errors[] = "Email is required.";
    if (empty($password)) $errors[] = "Password is required.";
    if (!$terms)          $errors[] = "You must agree to the Terms and Privacy Policy.";

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = "Invalid email format.";
    }

    if (strlen($password) < 6) {
        $errors[] = "Password must be at least 8 characters.";
    }

    if ($password !== $confirm) {
        $errors[] = "Passwords do not match.";
    }

    if (empty($errors)) {
        // Escape input
        $name     = mysqli_real_escape_string($link, $name);
        $email    = mysqli_real_escape_string($link, $email);
         $password    = mysqli_real_escape_string($link, $password);
      

        // Check if email exists
        $check = mysqli_query($link, "SELECT id FROM users WHERE email = '$email'");
        if (mysqli_num_rows($check) > 0) {
            $alert_script = "<script>alert('Email already registered. Please use a different one.');</script>";
        } else {
            $query = "INSERT INTO users (name, email, password, created_at) VALUES ('$name', '$email', '$password', NOW())";
            if (mysqli_query($link, $query)) {
                $alert_script = "<script>alert('Account created successfully! Redirecting to login...');
                                 window.location.href = 'login.php';</script>";
            } else {
                $error = mysqli_error($link);
                $alert_script = "<script>alert('Database error: $error');</script>";
            }
        }
    } else {
        $alert_script = "<script>alert('Registration Errors:\\n\\n" . implode("\\n", $errors) . "');</script>";
    }
}
?>
<?php echo $alert_script; ?>


    <main class="flex-grow-1" style="padding-top: 80px;">
        
        
        <style>
    .auth-container {
        min-height: calc(100vh - 160px);
        display: flex;
        align-items: center;
        background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        padding: 2rem 0;
    }

    .auth-card {
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(20px);
        border: 1px solid rgba(255, 255, 255, 0.2);
        border-radius: 25px;
        box-shadow: 0 8px 40px rgba(0, 0, 0, 0.12);
        overflow: hidden;
    }

    .auth-header {
        background: var(--success-gradient);
        color: white;
        padding: 2rem;
        text-align: center;
        position: relative;
    }

    .auth-header::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: url('data:image/svg+xml,<svg width="60" height="60" viewBox="0 0 60 60" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><g fill="%23ffffff" fill-opacity="0.1"><circle cx="12" cy="12" r="2"/><circle cx="48" cy="12" r="2"/><circle cx="12" cy="48" r="2"/><circle cx="48" cy="48" r="2"/></g></g></svg>');
    }

    .auth-icon {
        width: 80px;
        height: 80px;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 1rem;
        font-size: 2rem;
        position: relative;
        z-index: 2;
    }

    .auth-title {
        font-size: 1.75rem;
        font-weight: 700;
        margin: 0;
        position: relative;
        z-index: 2;
    }

    .auth-subtitle {
        opacity: 0.9;
        margin-top: 0.5rem;
        position: relative;
        z-index: 2;
    }

    .auth-body {
        padding: 2.5rem;
    }

    .form-floating {
        position: relative;
        margin-bottom: 1.5rem;
    }

    .form-floating .form-control {
        background: rgba(255, 255, 255, 0.8);
        border: 2px solid transparent;
        border-radius: 15px;
        padding: 1.5rem 1rem 0.75rem;
        transition: all 0.3s ease;
        height: auto;
    }

    .form-floating .form-control:focus {
        border-color: #4facfe;
        box-shadow: 0 0 0 0.2rem rgba(79, 172, 254, 0.25);
        background: rgba(255, 255, 255, 1);
    }

    /* Password field styling */
    .password-field .form-control {
        padding-right: 50px !important;
    }
    
    .password-toggle-btn {
        position: absolute;
        right: 15px;
        top: 50%;
        transform: translateY(-50%);
        background: none;
        border: none;
        color: #6c757d;
        cursor: pointer;
        z-index: 100;
        padding: 8px;
        font-size: 16px;
        transition: color 0.2s ease;
        outline: none;
    }
    
    .password-toggle-btn:hover {
        color: #4facfe;
    }
    
    .password-toggle-btn:focus {
        outline: none;
        color: #4facfe;
    }

    .form-floating .form-control.is-valid {
        border-color: #10b981;
        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3e%3cpath fill='%2310b981' d='m2.3 6.73.94-.94 2.94-2.94L6.3 2.8 3.25 5.85l-1-1z'/%3e%3c/svg%3e");
        background-repeat: no-repeat;
        background-position: right 3rem center;
        background-size: 1rem;
    }

    .form-floating .form-control.is-invalid {
        border-color: #ef4444;
        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23ef4444'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath d='m5.8 4.6 1.4 1.4M7.2 4.6l-1.4 1.4'/%3e%3c/svg%3e");
        background-repeat: no-repeat;
        background-position: right 3rem center;
        background-size: 1rem;
    }

    .form-floating label {
        padding: 1rem;
        color: #6c757d;
        font-weight: 500;
    }

    .password-strength {
        margin-top: 0.5rem;
    }

    .strength-bar {
        height: 4px;
        background: #e5e7eb;
        border-radius: 2px;
        overflow: hidden;
        margin-bottom: 0.5rem;
    }

    .strength-fill {
        height: 100%;
        transition: all 0.3s ease;
        border-radius: 2px;
    }

    .strength-weak { width: 25%; background: #ef4444; }
    .strength-fair { width: 50%; background: #f59e0b; }
    .strength-good { width: 75%; background: #06b6d4; }
    .strength-strong { width: 100%; background: #10b981; }

    .strength-text {
        font-size: 0.75rem;
        font-weight: 500;
    }

    .btn-auth-primary {
        background: var(--success-gradient);
        border: none;
        color: white;
        font-weight: 600;
        padding: 1rem 2rem;
        border-radius: 12px;
        transition: all 0.3s ease;
        width: 100%;
        font-size: 1.1rem;
    }

    .btn-auth-primary:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(79, 172, 254, 0.4);
        color: white;
    }

    .btn-auth-link {
        color: #4facfe;
        text-decoration: none;
        font-weight: 500;
        transition: all 0.2s ease;
    }

    .btn-auth-link:hover {
        color: #06b6d4;
        text-decoration: underline;
    }

    .auth-divider {
        display: flex;
        align-items: center;
        margin: 2rem 0;
        color: #9ca3af;
        font-size: 0.875rem;
    }

    .auth-divider::before,
    .auth-divider::after {
        content: '';
        flex: 1;
        height: 1px;
        background: #e5e7eb;
    }

    .auth-divider span {
        padding: 0 1rem;
        background: white;
    }

    .auth-footer {
        text-align: center;
        padding: 1.5rem 2.5rem;
        background: #f8fafc;
        border-top: 1px solid #e2e8f0;
    }

    .social-login {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 1rem;
        margin-bottom: 1rem;
    }

    .btn-social {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        padding: 0.75rem 1rem;
        border: 2px solid #e2e8f0;
        border-radius: 10px;
        background: white;
        color: #4a5568;
        text-decoration: none;
        font-weight: 500;
        transition: all 0.2s ease;
    }

    .btn-social:hover {
        border-color: #cbd5e0;
        transform: translateY(-1px);
        color: #2d3748;
    }

    .error-message {
        color: #e53e3e;
        font-size: 0.875rem;
        margin-top: 0.5rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .success-message {
        color: #10b981;
        font-size: 0.875rem;
        margin-top: 0.5rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .terms-check {
        background: #f8fafc;
        border-radius: 12px;
        padding: 1rem;
        margin: 1.5rem 0;
        border: 1px solid #e2e8f0;
    }

    .form-check {
        display: flex;
        align-items: flex-start;
        gap: 0.75rem;
    }

    .form-check-input {
        width: 1.25rem;
        height: 1.25rem;
        border-radius: 6px;
        border: 2px solid #d1d5db;
        transition: all 0.2s ease;
        margin-top: 0.125rem;
    }

    .form-check-input:checked {
        background-color: #4facfe;
        border-color: #4facfe;
    }

    .form-check-label {
        font-size: 0.875rem;
        color: #4a5568;
        line-height: 1.5;
    }

    @media (max-width: 768px) {
        .auth-container {
            padding: 1rem;
            min-height: calc(100vh - 120px);
        }
        
        .auth-body {
            padding: 2rem 1.5rem;
        }
        
        .auth-header {
            padding: 1.5rem;
        }
        
        .auth-icon {
            width: 60px;
            height: 60px;
            font-size: 1.5rem;
        }
        
        .auth-title {
            font-size: 1.5rem;
        }
        
        .social-login {
            grid-template-columns: 1fr;
        }
    }
</style>

<div class="auth-container">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-6 col-lg-5">
                <div class="auth-card">
                    <div class="auth-header">
                        <div class="auth-icon">
                            <i class="fas fa-user-plus"></i>
                        </div>
                        <h1 class="auth-title">Create Account</h1>
                        <p class="auth-subtitle">Join our community of celebrity fans</p>
                    </div>

                    <div class="auth-body">
                        <form method="POST" action="" id="">
                            <input type="hidden" name="_token" value="zuXNHNzUyS1eZwQz2XskXw3ijpDWGNUfuAzRIeQO" autocomplete="off">
                            <div class="form-floating">
                                <input id="name" 
                                       type="text" 
                                       class="form-control " 
                                       name="name" 
                                       value="" 
                                       required 
                                       autocomplete="name" 
                                       autofocus
                                       placeholder="Enter your full name">
                                <label for="name">Full Name</label>
                                                            </div>

                            <div class="form-floating">
                                <input id="email" 
                                       type="email" 
                                       class="form-control " 
                                       name="email" 
                                       value="" 
                                       required 
                                       autocomplete="email"
                                       placeholder="Enter your email address">
                                <label for="email">Email Address</label>
                                                            </div>

                            <div class="form-floating" style="position: relative !important; margin-bottom: 1.5rem;">
                                <input id="password" 
                                       type="password" 
                                       class="form-control " 
                                       name="password" 
                                       required 
                                       autocomplete="new-password"
                                       style="padding-right: 60px !important; padding-left: 1rem !important; padding-top: 1.5rem !important; padding-bottom: 0.75rem !important;"
                                       placeholder="Create a password">
                                <label for="password" style="padding: 1rem !important;">Password</label>
                                
                                <!-- BULLETPROOF TOGGLE BUTTON -->
                                <span onclick="
                                    var input = document.getElementById('password');
                                    var icon = this.querySelector('i');
                                    if (input && icon) {
                                        if (input.type === 'password') {
                                            input.type = 'text';
                                            icon.className = 'fas fa-eye-slash';
                                            console.log('Password shown');
                                        } else {
                                            input.type = 'password';
                                            icon.className = 'fas fa-eye';
                                            console.log('Password hidden');
                                        }
                                    } else {
                                        console.error('Elements not found');
                                    }
                                " style="
                                    position: absolute !important;
                                    right: 15px !important;
                                    top: 50% !important;
                                    transform: translateY(-50%) !important;
                                    cursor: pointer !important;
                                    z-index: 9999 !important;
                                    background: none !important;
                                    border: none !important;
                                    padding: 8px !important;
                                    color: #6c757d !important;
                                    font-size: 16px !important;
                                    display: block !important;
                                    pointer-events: auto !important;
                                ">
                                    <i class="fas fa-eye"></i>
                                </span>
                                
                                <div class="password-strength" id="passwordStrength" style="display: none;">
                                    <div class="strength-bar">
                                        <div class="strength-fill" id="strengthFill"></div>
                                    </div>
                                    <div class="strength-text" id="strengthText"></div>
                                </div>
                                                            </div>

                            <div class="form-floating" style="position: relative !important; margin-bottom: 1.5rem;">
                                <input id="password-confirm" 
                                       type="password" 
                                       class="form-control" 
                                       name="password_confirmation" 
                                       required 
                                       autocomplete="new-password"
                                       style="padding-right: 60px !important; padding-left: 1rem !important; padding-top: 1.5rem !important; padding-bottom: 0.75rem !important;"
                                       placeholder="Confirm your password">
                                <label for="password-confirm" style="padding: 1rem !important;">Confirm Password</label>
                                
                                <!-- BULLETPROOF TOGGLE BUTTON -->
                                <span onclick="
                                    var input = document.getElementById('password-confirm');
                                    var icon = this.querySelector('i');
                                    if (input && icon) {
                                        if (input.type === 'password') {
                                            input.type = 'text';
                                            icon.className = 'fas fa-eye-slash';
                                            console.log('Confirm password shown');
                                        } else {
                                            input.type = 'password';
                                            icon.className = 'fas fa-eye';
                                            console.log('Confirm password hidden');
                                        }
                                    } else {
                                        console.error('Elements not found');
                                    }
                                " style="
                                    position: absolute !important;
                                    right: 15px !important;
                                    top: 50% !important;
                                    transform: translateY(-50%) !important;
                                    cursor: pointer !important;
                                    z-index: 9999 !important;
                                    background: none !important;
                                    border: none !important;
                                    padding: 8px !important;
                                    color: #6c757d !important;
                                    font-size: 16px !important;
                                    display: block !important;
                                    pointer-events: auto !important;
                                ">
                                    <i class="fas fa-eye"></i>
                                </span>
                                
                                <div id="passwordMatch" style="display: none;"></div>
                            </div>

                            <div class="terms-check">
                                <div class="form-check">
                                    <input class="form-check-input" 
                                           type="checkbox" 
                                           id="terms" 
                                           name="terms" 
                                           required>
                                    <label class="form-check-label" for="terms">
                                        I agree to the <a href="#" class="btn-auth-link">Terms of Service</a> and 
                                        <a href="#" class="btn-auth-link">Privacy Policy</a>. I understand that I will receive 
                                        occasional emails about celebrity updates and platform news.
                                    </label>
                                </div>
                            </div>

                            <button type="submit" class="btn btn-auth-primary">
                                <i class="fas fa-user-plus me-2"></i>
                                Create My Account
                            </button>
                        </form>

                       
                    </div>

                    <div class="auth-footer">
                        <p class="mb-0">
                            Already have an account? 
                            <a href="login.php" class="btn-auth-link">
                                <strong>Sign in here</strong>
                            </a>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
    </main>

 <?php
    include 'footer.php';
?>