/home/awneajlw/.trash/social-media-reg.php
<?php
/**
 * Social Media Registration Page
 * Final step of registration - saves social media details and completes user registration
 * Automatically logs in the user after successful completion
 */

// Start session if not already started
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Include required files
require_once 'config/database.php';  // Database connection configuration
require_once 'includes/auth.php';    // Authentication functions

$success_message = '';
$error_message = '';

// Check if user_id exists in session
if (!isset($_SESSION['user_id'])) {
    // Debug: Check what's in session
    error_log("Social media page: No user_id in session. Session data: " . print_r($_SESSION, true));
    
    // Try to get the latest user from registration email
    if (isset($_SESSION['registration_email'])) {
        try {
            $database = new Database();
            $db = $database->getConnection();
            $email = $_SESSION['registration_email'];
            
            $user_query = "SELECT id FROM users WHERE email = ? ORDER BY id DESC LIMIT 1";
            $user_stmt = $db->prepare($user_query);
            $user_stmt->execute([$email]);
            $user = $user_stmt->fetch(PDO::FETCH_ASSOC);
            
            if ($user) {
                $_SESSION['user_id'] = $user['id'];
                error_log("Recovered user_id from email: " . $user['id']);
            } else {
                $error_message = 'Registration not found. Please start registration again.';
            }
        } catch (Exception $e) {
            error_log("Error recovering user_id: " . $e->getMessage());
            $error_message = 'Session expired. Please complete registration again.';
        }
    } else {
        $error_message = 'Session expired. Please complete registration again.';
    }
}

/**
 * Social Media Form Submission Handler
 * Process social media details and complete registration
 */
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user_id'])) {
    try {
        $database = new Database();
        $db = $database->getConnection();
        
        $user_id = $_SESSION['user_id'];
        $whatsapp_number = sanitizeInput($_POST['whatsapp_number'] ?? '');
        $facebook_id = sanitizeInput($_POST['facebook_id'] ?? '');
        $instagram_id = sanitizeInput($_POST['instagram_id'] ?? '');
        $website = sanitizeInput($_POST['website'] ?? '');
        
        // Debug log
        error_log("Social media submission - User ID: $user_id, WhatsApp: $whatsapp_number");
        
        // Add social media columns to existing shops table
        $alter_queries = [
            "ALTER TABLE shops ADD COLUMN shop_whatsapp VARCHAR(20) DEFAULT NULL",
            "ALTER TABLE shops ADD COLUMN shop_facebook VARCHAR(255) DEFAULT NULL", 
            "ALTER TABLE shops ADD COLUMN shop_instagram VARCHAR(255) DEFAULT NULL",
            "ALTER TABLE shops ADD COLUMN shop_website VARCHAR(255) DEFAULT NULL"
        ];
        
        foreach ($alter_queries as $query) {
            try {
                $db->exec($query);
            } catch (PDOException $e) {
                // Column already exists, continue
                error_log("Column might already exist: " . $e->getMessage());
            }
        }
        
        // First check if shop exists for this user
        $check_query = "SELECT id FROM shops WHERE user_id = ?";
        $check_stmt = $db->prepare($check_query);
        $check_stmt->execute([$user_id]);
        
        if ($check_stmt->rowCount() > 0) {
            // Update existing shop with social media details
            $update_query = "UPDATE shops SET 
                            shop_whatsapp = ?, 
                            shop_facebook = ?, 
                            shop_instagram = ?, 
                            shop_website = ? 
                            WHERE user_id = ?";
            $stmt = $db->prepare($update_query);
            $success = $stmt->execute([$whatsapp_number, $facebook_id, $instagram_id, $website, $user_id]);
        } else {
            // Create shop record if it doesn't exist
            $insert_query = "INSERT INTO shops (user_id, shop_whatsapp, shop_facebook, shop_instagram, shop_website, shop_name, shop_phone) 
                            VALUES (?, ?, ?, ?, ?, 'Default Shop', '')";
            $stmt = $db->prepare($insert_query);
            $success = $stmt->execute([$user_id, $whatsapp_number, $facebook_id, $instagram_id, $website]);
        }
        
        if ($success) {
            // Mark registration as complete
            $complete_query = "UPDATE users SET registration_step = 3, email_verified = 1 WHERE id = ?";
            $complete_stmt = $db->prepare($complete_query);
            $complete_stmt->execute([$user_id]);
            
            // Auto-login the user
            $_SESSION['last_activity'] = time();
            
            // Clear registration session data
            unset($_SESSION['registration_email']);
            unset($_SESSION['registration_otp']);
            unset($_SESSION['otp_generated_time']);
            unset($_SESSION['registration_step']);
            
            $success_message = 'Registration completed successfully! Redirecting to dashboard...';
            
            // Redirect to home page after 2 seconds
            echo "<script>
                setTimeout(function() {
                    window.location.href = 'home.php';
                }, 2000);
            </script>";
        } else {
            $error_message = 'Failed to save social media details. Please try again.';
        }
        
    } catch (Exception $e) {
        error_log("Social media registration error: " . $e->getMessage());
        $error_message = 'Database error: ' . $e->getMessage();
    }
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $error_message = 'Session expired. Please complete registration from the beginning.';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Social Media Registration - OPTI SLIP</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #1e40af 0%, #7c3aed 100%);
            min-height: 100vh;
            padding: 20px;
            margin: 0;
        }
        
        .page-header {
            color: #9ca3af;
            font-size: 18px;
            margin-bottom: 20px;
        }
        
        .main-container {
            max-width: 500px;
            margin: 0 auto;
        }
        
        .form-card {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }
        
        .form-header {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 30px;
        }
        
        .back-btn {
            background: #10b981;
            color: white;
            border: none;
            padding: 8px 12px;
            border-radius: 6px;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 14px;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            background: #059669;
            transform: translateY(-1px);
        }
        
        .logo-section {
            display: flex;
            flex-direction: column;
            align-items: center;
            flex: 1;
        }
        
        .opti-logo-container {
            display: flex;
            gap: 15px;
            margin-bottom: 10px;
        }
        
        .opti-logo {
            width: 50px;
            height: 50px;
            background: white;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            overflow: hidden;
            border: 2px solid #e5e7eb;
        }
        
        .opti-logo.phone {
            background: #f3f4f6;
        }
        
        .opti-logo.doc {
            background: #e5e7eb;
        }
        
        .glasses-icon {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: #10b981;
            font-size: 20px;
            z-index: 2;
        }
        
        .phone-screen {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 30px;
            height: 20px;
            background: #3b82f6;
            border-radius: 4px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 10px;
        }
        
        .doc-icon {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: #6b7280;
            font-size: 16px;
        }
        
        .opti-title {
            color: #374151;
            font-size: 24px;
            font-weight: bold;
        }
        
        .form-content {
            margin-bottom: 30px;
        }
        
        .form-group {
            margin-bottom: 25px;
        }
        
        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #374151;
            font-size: 14px;
        }
        
        .form-input {
            width: 100%;
            padding: 15px;
            border: 2px solid #e5e7eb;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
            background: white;
        }
        
        .form-input:focus {
            outline: none;
            border-color: #10b981;
            box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.1);
        }
        
        .form-input::placeholder {
            color: #9ca3af;
        }
        
        .form-footer {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 30px;
            padding: 0 10px;
            gap: 15px;
            flex-wrap: wrap;
        }
        
        .nav-btn {
            background: #10b981;
            border: none;
            color: white;
            font-weight: 600;
            font-size: 14px;
            cursor: pointer;
            padding: 12px 20px;
            border-radius: 8px;
            transition: all 0.3s ease;
            min-width: 90px;
            max-width: 120px;
            box-shadow: 0 2px 4px rgba(16, 185, 129, 0.3);
            white-space: nowrap;
        }
        
        .nav-btn:hover {
            background: #059669;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(16, 185, 129, 0.4);
        }
        
        .pagination {
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 12px;
            color: #6b7280;
            background: #f8fafc;
            padding: 6px 12px;
            border-radius: 20px;
            border: 1px solid #e5e7eb;
            flex-shrink: 0;
        }
        
        .pagination .page-number {
            padding: 4px 8px;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 500;
            font-size: 11px;
        }
        
        .pagination .page-number.active {
            background: #10b981;
            color: white;
            transform: scale(1.1);
            box-shadow: 0 2px 4px rgba(16, 185, 129, 0.3);
        }
        
        .pagination .page-number:hover:not(.active) {
            background: #e5e7eb;
            color: #374151;
        }
        
        @media (max-width: 768px) {
            .main-container {
                max-width: 100%;
                padding: 0 10px;
            }
            
            .form-card {
                padding: 20px;
                border-radius: 15px;
            }
            
            .form-header {
                flex-direction: column;
                gap: 10px;
                text-align: center;
            }
            
            .opti-title {
                font-size: 20px;
            }
            
            .opti-logo {
                width: 40px;
                height: 40px;
            }
            
            .glasses-icon {
                font-size: 16px;
            }
            
            .form-footer {
                flex-direction: column;
                gap: 15px;
                padding: 0 5px;
                align-items: stretch;
            }
            
            .nav-btn {
                width: 100%;
                margin: 5px 0;
                max-width: none;
            }
            
            .pagination {
                justify-content: center;
                flex-wrap: wrap;
                order: -1;
                margin-bottom: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="main-container">
        <!-- Page Header -->
        <div class="page-header">socail media reg</div>
        
        <!-- Form Card -->
        <div class="form-card">
            <!-- Header -->
            <div class="form-header">
                <button class="back-btn" onclick="goBack()">
                    <i class="fas fa-arrow-left"></i>
                </button>
                
                <div class="logo-section">
                    <div class="opti-logo-container">
                        <div class="opti-logo phone">
                            <div class="phone-screen">
                                <i class="fas fa-mobile-alt"></i>
                            </div>
                            <i class="fas fa-glasses glasses-icon"></i>
                        </div>
                        <div class="opti-logo doc">
                            <i class="fas fa-file-alt doc-icon"></i>
                            <i class="fas fa-glasses glasses-icon"></i>
                        </div>
                    </div>
                    <h2 class="opti-title">OPTI SLIP</h2>
                </div>
            </div>
            
            <!-- Form Content -->
            <div class="form-content">
                <?php if ($success_message): ?>
                    <div class="alert alert-success" style="background: #d1fae5; color: #065f46; padding: 15px; border-radius: 8px; margin-bottom: 20px;">
                        <i class="fas fa-check-circle me-2"></i>
                        <?php echo htmlspecialchars($success_message); ?>
                    </div>
                <?php endif; ?>
                
                <?php if ($error_message): ?>
                    <div class="alert alert-danger" style="background: #fee2e2; color: #991b1b; padding: 15px; border-radius: 8px; margin-bottom: 20px;">
                        <i class="fas fa-exclamation-triangle me-2"></i>
                        <?php echo htmlspecialchars($error_message); ?>
                    </div>
                <?php endif; ?>
                
                <form method="POST" id="social-media-form">
                    <div class="form-group">
                        <label class="form-label">WhatsApp Number</label>
                        <input type="tel" name="whatsapp_number" class="form-input" placeholder="XXXXXXXXXX">
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Facebook ID</label>
                        <input type="text" name="facebook_id" class="form-input" placeholder="XXXXXXXXXX">
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Instagram ID</label>
                        <input type="text" name="instagram_id" class="form-input" placeholder="XXXXXXXXXX">
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Website</label>
                        <input type="url" name="website" class="form-input" placeholder="XXXXXXXXXX">
                    </div>
                    
                </form>
            </div>
            
            <!-- Footer -->
            <div class="form-footer">
                <button class="nav-btn" onclick="goToPrevious()">PREVIOUS</button>
                
                <div class="pagination">
                    <span class="page-number">1</span>
                    <span class="page-number active">2</span>
                    <span class="page-number">3</span>
                    <span class="page-number">4</span>
                    <span class="page-number">5</span>
                    <span class="page-number">6</span>
                    <span>...</span>
                    <span class="page-number">10</span>
                </div>
                
                <button type="submit" form="social-media-form" class="nav-btn">
                    <i class="fas fa-save me-2"></i>SAVE
                </button>
            </div>
        </div>
    </div>
    
    <script>
        function goBack() {
            window.history.back();
        }
        
        function goToPrevious() {
            // Navigate to previous step
            window.location.href = 'shop-registration-form.php';
        }
        
        function goToNext() {
            // Submit form first, then redirect
            document.getElementById('social-media-form').submit();
        }
        
        
        // Form submission - Let PHP handle the submission
        // No preventDefault needed - form will submit normally to PHP
        
        // Auto-focus first input
        document.addEventListener('DOMContentLoaded', function() {
            document.querySelector('input[name="whatsapp_number"]').focus();
            
            // Add Enter key functionality to inputs
            document.querySelectorAll('.form-input').forEach(input => {
                input.addEventListener('keypress', function(e) {
                    if (e.key === 'Enter') {
                        e.preventDefault();
                        document.querySelector('form').submit();
                    }
                });
            });
        });
    </script>
</body>
</html>