T.ME/BIBIL_0DAY
CasperSecurity


Server : Apache/2
System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64
User : gositeme ( 1004)
PHP Version : 8.2.29
Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Directory :  /home/gositeme/.cursor-server/data/User/History/-295c865b/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/gositeme/.cursor-server/data/User/History/-295c865b/SRQq.php
<?php
session_start();
require_once 'config/database.php';

// Debug session
error_log("šŸ›’ Session ID: " . session_id());
error_log("šŸ›’ Session status: " . session_status());
error_log("šŸ›’ Request method: " . $_SERVER['REQUEST_METHOD']);

// Initialize cart if not exists
if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = [];
    error_log("šŸ›’ Initialized new cart session");
} else {
    error_log("šŸ›’ Existing cart found with " . count($_SESSION['cart']) . " items");
}

$pdo = getDBConnection();

// Handle POST requests (add to cart, etc.)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    switch ($action) {
        case 'add':
            $track_id = intval($_POST['track_id'] ?? 0);
            $artist_plan = $_POST['artist_plan'] ?? 'free';
            
            if (!$track_id) {
                echo json_encode(['success' => false, 'message' => 'Invalid track ID']);
                exit;
            }
            
            // Get track information with artist details
            $stmt = $pdo->prepare("
                SELECT mt.*, u.name as artist_name, u.plan as user_plan, u.id as artist_id
                FROM music_tracks mt 
                JOIN users u ON mt.user_id = u.id 
                WHERE mt.id = ? AND mt.status = 'complete'
            ");
            $stmt->execute([$track_id]);
            $track = $stmt->fetch();
            
            if (!$track) {
                echo json_encode(['success' => false, 'message' => 'Track not found']);
                exit;
            }
            
            // Determine revenue recipient based on artist plan
            $is_free_user = (strtolower($track['user_plan']) === 'free');
            $revenue_recipient = $is_free_user ? 'platform' : 'artist';
            $recipient_id = $is_free_user ? 1 : $track['artist_id']; // Admin ID = 1
            
            // Check if track already in cart
            $found = false;
            foreach ($_SESSION['cart'] as &$item) {
                if ($item['track_id'] == $track_id) {
                    $item['quantity'] += 1;
                    $found = true;
                    break;
                }
            }
            
            if (!$found) {
                $_SESSION['cart'][] = [
                    'track_id' => $track_id,
                    'title' => $track['title'],
                    'artist_name' => $track['artist_name'],
                    'artist_id' => $track['artist_id'],
                    'price' => floatval($track['price']),
                    'quantity' => 1,
                    'audio_url' => $track['audio_url'],
                    'user_plan' => $track['user_plan'],
                    'revenue_recipient' => $revenue_recipient,
                    'recipient_id' => $recipient_id,
                    'is_free_user_track' => $is_free_user
                ];
            }
            
            // Log the cart addition and current cart state
            error_log("šŸ›’ Track added to cart: ID $track_id, Price: $" . $track['price'] . ", Revenue goes to: $revenue_recipient (ID: $recipient_id)");
            error_log("šŸ›’ Cart now contains: " . count($_SESSION['cart']) . " items");
            error_log("šŸ›’ Full cart: " . json_encode($_SESSION['cart']));
            
            // Explicitly save session
            session_write_close();
            session_start();
            
            echo json_encode([
                'success' => true, 
                'message' => 'Track added to cart',
                'cart_count' => count($_SESSION['cart']),
                'cart_total' => array_sum(array_map(function($item) {
                    return $item['price'] * $item['quantity'];
                }, $_SESSION['cart'])),
                'debug' => [
                    'session_id' => session_id(),
                    'cart_items' => count($_SESSION['cart']),
                    'track_added' => $track_id
                ],
                'revenue_info' => [
                    'recipient' => $revenue_recipient,
                    'is_free_user' => $is_free_user
                ]
            ]);
            exit;
            
        case 'remove':
            $track_id = intval($_POST['track_id'] ?? 0);
            
            $_SESSION['cart'] = array_filter($_SESSION['cart'], function($item) use ($track_id) {
                return $item['track_id'] != $track_id;
            });
            
            // Reindex array
            $_SESSION['cart'] = array_values($_SESSION['cart']);
            
            echo json_encode(['success' => true, 'message' => 'Item removed from cart']);
            exit;
            
        case 'clear':
            $_SESSION['cart'] = [];
            echo json_encode(['success' => true, 'message' => 'Cart cleared']);
            exit;
            
        case 'checkout':
            $user_id = $_SESSION['user_id'] ?? null;
            
            if (!$user_id) {
                echo json_encode(['success' => false, 'message' => 'Please log in to complete checkout']);
                exit;
            }
            
            if (empty($_SESSION['cart'])) {
                echo json_encode(['success' => false, 'message' => 'Cart is empty']);
                exit;
            }
            
            try {
                $pdo->beginTransaction();
                
                $total_cart_value = 0;
                $platform_revenue = 0;
                $artist_revenue = 0;
                
                // Process each cart item
                foreach ($_SESSION['cart'] as $item) {
                    $item_total = $item['price'] * $item['quantity'];
                    $total_cart_value += $item_total;
                    
                    if ($item['is_free_user_track']) {
                        $platform_revenue += $item_total;
                    } else {
                        $artist_revenue += $item_total;
                    }
                    
                    // Record sale
                    $stmt = $pdo->prepare("
                        INSERT INTO sales (
                            track_id, buyer_id, artist_id, amount, quantity, 
                            revenue_recipient, recipient_id, is_free_user_track, 
                            created_at
                        ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW())
                    ");
                    $stmt->execute([
                        $item['track_id'],
                        $user_id,
                        $item['artist_id'],
                        $item_total,
                        $item['quantity'],
                        $item['revenue_recipient'],
                        $item['recipient_id'],
                        $item['is_free_user_track'] ? 1 : 0
                    ]);
                    
                    // Add to user's library
                    $stmt = $pdo->prepare("
                        INSERT IGNORE INTO user_library (user_id, track_id, purchase_date)
                        VALUES (?, ?, NOW())
                    ");
                    $stmt->execute([$user_id, $item['track_id']]);
                }
                
                // Update platform revenue (add to admin account)
                if ($platform_revenue > 0) {
                    $stmt = $pdo->prepare("
                        UPDATE users SET credits = credits + ? WHERE id = 1
                    ");
                    $stmt->execute([$platform_revenue * 100]); // Convert to credits (assuming $1 = 100 credits)
                    
                    // Log platform revenue
                    $stmt = $pdo->prepare("
                        INSERT INTO credit_transactions (user_id, amount, type, description, created_at)
                        VALUES (1, ?, 'platform_revenue', 'Free user track sales revenue', NOW())
                    ");
                    $stmt->execute([$platform_revenue * 100]);
                }
                
                $pdo->commit();
                
                // Clear cart after successful checkout
                $_SESSION['cart'] = [];
                
                echo json_encode([
                    'success' => true, 
                    'message' => 'Purchase completed successfully!',
                    'total' => $total_cart_value,
                    'platform_revenue' => $platform_revenue,
                    'artist_revenue' => $artist_revenue
                ]);
                exit;
                
            } catch (Exception $e) {
                $pdo->rollback();
                error_log("Checkout error: " . $e->getMessage());
                echo json_encode(['success' => false, 'message' => 'Checkout failed. Please try again.']);
                exit;
            }
            break;
    }
}

// Handle GET requests (view cart)
$user_id = $_SESSION['user_id'] ?? null;
$cart_items = $_SESSION['cart'] ?? [];

// Debug logging for cart display
error_log("šŸ›’ Cart display - Session cart items: " . count($cart_items));
error_log("šŸ›’ Cart display - User ID: " . ($user_id ?: 'null'));
if (!empty($cart_items)) {
    error_log("šŸ›’ Cart display - First item: " . json_encode($cart_items[0]));
}

$cart_total = array_sum(array_map(function($item) {
    return $item['price'] * $item['quantity'];
}, $cart_items));

// Calculate revenue split for display
$platform_revenue = 0;
$artist_revenue = 0;
foreach ($cart_items as $item) {
    $item_total = $item['price'] * $item['quantity'];
    if ($item['is_free_user_track']) {
        $platform_revenue += $item_total;
    } else {
        $artist_revenue += $item_total;
    }
}

// Set page variables for header
$page_title = 'Shopping Cart - SoundStudioPro';
$page_description = 'Review your music purchases and complete your order.';
$current_page = 'cart';

include 'includes/header.php';
?>

<style>
    /* Cart page specific styles matching credits.php pattern */
    .cart-hero {
        padding: 8rem 0 6rem;
        text-align: center;
        color: white;
        background: linear-gradient(135deg, #0a0a0a 0%, #1a1a1a 50%, #0a0a0a 100%);
        position: relative;
        overflow: hidden;
        margin-bottom: 4rem;
        margin-top: 0;
    }
    
    .cart-hero::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grid" width="10" height="10" patternUnits="userSpaceOnUse"><path d="M 10 0 L 0 0 0 10" fill="none" stroke="rgba(102,126,234,0.1)" stroke-width="0.5"/></pattern></defs><rect width="100" height="100" fill="url(%23grid)"/></svg>');
        opacity: 0.3;
    }
    
    .cart-hero-content {
        max-width: 90rem;
        margin: 0 auto;
        position: relative;
        z-index: 2;
    }
    
    .cart-badge {
        display: inline-block;
        background: linear-gradient(135deg, rgba(102, 126, 234, 0.2), rgba(118, 75, 162, 0.2));
        color: #667eea;
        padding: 1.2rem 2.4rem;
        border-radius: 50px;
        font-size: 1.4rem;
        font-weight: 600;
        margin-bottom: 3rem;
        backdrop-filter: blur(10px);
        border: 1px solid rgba(102, 126, 234, 0.3);
    }
    
    .cart-title {
        font-size: 4rem;
        font-weight: 800;
        line-height: 1.2;
        margin-bottom: 1.5rem;
        background: linear-gradient(135deg, #ffffff, #667eea);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
    }
    
    .cart-subtitle {
        font-size: 1.6rem;
        color: #a0aec0;
        margin-bottom: 3rem;
        max-width: 50rem;
        margin-left: auto;
        margin-right: auto;
        line-height: 1.5;
    }
    
    .cart-container {
        max-width: 120rem;
        margin: 0 auto;
        padding: 0 2rem;
    }
    
    .cart-content {
        display: grid;
        grid-template-columns: 1fr 400px;
        gap: 4rem;
        align-items: start;
    }
    
    .cart-items-section {
        background: linear-gradient(135deg, rgba(20, 20, 20, 0.95) 0%, rgba(30, 30, 30, 0.95) 100%);
        border: 2px solid;
        border-image: linear-gradient(135deg, #667eea, #764ba2) 1;
        border-radius: 24px;
        padding: 3rem;
        backdrop-filter: blur(30px);
        box-shadow: 0 25px 50px rgba(0, 0, 0, 0.3);
        position: relative;
    }
    
    .cart-items-section::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grid" width="10" height="10" patternUnits="userSpaceOnUse"><path d="M 10 0 L 0 0 0 10" fill="none" stroke="rgba(102,126,234,0.1)" stroke-width="0.5"/></pattern></defs><rect width="100" height="100" fill="url(%23grid)"/></svg>');
        opacity: 0.3;
        pointer-events: none;
        border-radius: 24px;
    }
    
    .section-title {
        font-size: 2.4rem;
        font-weight: 700;
        margin-bottom: 2.5rem;
        display: flex;
        align-items: center;
        gap: 1rem;
        background: linear-gradient(135deg, #ffffff, #667eea);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
        position: relative;
        z-index: 2;
    }
    
    .section-title i {
        font-size: 2.2rem;
        background: linear-gradient(135deg, #667eea, #764ba2);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
    }
    
    .cart-item {
        background: rgba(255, 255, 255, 0.05);
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: 16px;
        padding: 2rem;
        margin-bottom: 2rem;
        position: relative;
        z-index: 2;
        transition: all 0.3s ease;
    }
    
    .cart-item:hover {
        background: rgba(255, 255, 255, 0.08);
        border-color: rgba(102, 126, 234, 0.3);
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(102, 126, 234, 0.15);
    }
    
    .item-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 1rem;
        gap: 2rem;
    }
    
    .item-info {
        flex: 1;
        min-width: 0;
    }
    
    .item-info h4 {
        font-size: 1.8rem;
        font-weight: 700;
        color: white;
        margin-bottom: 0.5rem;
        line-height: 1.3;
    }
    
    .item-info p {
        color: #a0aec0;
        font-size: 1.4rem;
        margin-bottom: 0.8rem;
        line-height: 1.4;
    }
    
    .item-price {
        font-size: 2rem;
        font-weight: 700;
        color: white;
        text-align: right;
        min-width: 120px;
        flex-shrink: 0;
    }
    
    .price-free {
        color: #48bb78;
        display: flex;
        align-items: center;
        gap: 0.5rem;
        flex-wrap: wrap;
        word-break: break-word;
        margin-bottom: 0.5rem;
    }
    
    .price-free i {
        font-size: 1.8rem;
        flex-shrink: 0;
    }
    
    .price-free span {
        font-size: 1.4rem;
        font-weight: 600;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    
    .price-info {
        color: #48bb78;
        font-size: 1.2rem;
        font-weight: 600;
        margin-top: 0.3rem;
        margin-bottom: 0.5rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }
    
    .price-details {
        color: #667eea;
        font-size: 1.4rem;
        font-weight: 600;
        margin-bottom: 0.5rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }
    
    .revenue-info {
        color: #ed8936;
        font-size: 1.2rem;
        margin-top: 0.3rem;
        margin-bottom: 0.5rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }
    
    .item-actions {
        display: flex;
        gap: 1rem;
        margin-top: 1rem;
    }
    
    .btn-remove {
        background: linear-gradient(135deg, #e53e3e, #c53030);
        color: white;
        border: none;
        padding: 1rem 2rem;
        border-radius: 12px;
        font-size: 1.4rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }
    
    .btn-remove:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(229, 62, 62, 0.3);
    }
    
    .cart-summary {
        background: linear-gradient(135deg, rgba(20, 20, 20, 0.95) 0%, rgba(30, 30, 30, 0.95) 100%);
        border: 2px solid;
        border-image: linear-gradient(135deg, #667eea, #764ba2) 1;
        border-radius: 24px;
        padding: 3rem;
        backdrop-filter: blur(30px);
        box-shadow: 0 25px 50px rgba(0, 0, 0, 0.3);
        position: sticky;
        top: 2rem;
        height: fit-content;
    }
    
    .revenue-split {
        margin-bottom: 2rem;
    }
    
    .revenue-split-free {
        background: rgba(72, 187, 120, 0.1);
        padding: 2rem;
        border-radius: 16px;
        border: 1px solid rgba(72, 187, 120, 0.3);
        text-align: center;
        margin-bottom: 2rem;
    }
    
    .free-ownership {
        color: #48bb78;
        font-size: 1.6rem;
        font-weight: bold;
        margin-bottom: 1rem;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        flex-wrap: wrap;
        text-align: center;
        word-break: break-word;
    }
    
    .ownership-details {
        display: flex;
        justify-content: space-between;
        margin-bottom: 1rem;
        font-size: 1.4rem;
    }
    
    .ownership-details span:first-child {
        color: #a0aec0;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }
    
    .ownership-details span:last-child {
        color: #48bb78;
        font-weight: bold;
    }
    
    .revenue-row {
        display: flex;
        justify-content: space-between;
        margin-bottom: 1rem;
        font-size: 1.6rem;
    }
    
    .revenue-row span:first-child {
        color: #a0aec0;
    }
    
    .revenue-row.artist span:last-child {
        color: #48bb78;
        font-weight: 600;
    }
    
    .revenue-row.platform span:last-child {
        color: #ed8936;
        font-weight: 600;
    }
    
    .total-section {
        border-top: 2px solid rgba(102, 126, 234, 0.2);
        padding-top: 2rem;
        margin-top: 2rem;
    }
    
    .total-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 2.2rem;
        font-weight: 700;
        color: white;
        gap: 1rem;
    }
    
    .total-row span:first-child {
        flex: 1;
        min-width: 0;
    }
    
    .total-row span:last-child {
        flex-shrink: 0;
        white-space: nowrap;
    }
    
    .total-free {
        color: #48bb78;
        font-size: 2.2rem;
        font-weight: 700;
        display: flex;
        align-items: center;
        gap: 0.5rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        min-width: 0;
        flex-shrink: 1;
    }
    
    .checkout-section {
        margin-top: 2rem;
        text-align: center;
    }
    
    .btn-checkout {
        width: 100%;
        background: linear-gradient(135deg, #667eea, #764ba2);
        color: white;
        border: none;
        padding: 2rem;
        border-radius: 16px;
        font-size: 1.6rem;
        font-weight: 700;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 1rem;
        text-decoration: none;
    }
    
    .btn-checkout:hover {
        transform: translateY(-2px);
        box-shadow: 0 12px 30px rgba(102, 126, 234, 0.4);
        color: white;
        text-decoration: none;
    }
    
    .btn-checkout-free {
        background: linear-gradient(135deg, #48bb78, #38a169);
    }
    
    .btn-checkout-free:hover {
        box-shadow: 0 12px 30px rgba(72, 187, 120, 0.4);
    }
    
    .checkout-info {
        margin-top: 1rem;
        color: #48bb78;
        font-size: 1.2rem;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
    }
    
    .empty-cart {
        text-align: center;
        padding: 6rem 2rem;
        position: relative;
        z-index: 2;
    }
    
    .empty-cart i {
        font-size: 6rem;
        color: rgba(102, 126, 234, 0.3);
        margin-bottom: 2rem;
    }
    
    .empty-cart h3 {
        font-size: 2.4rem;
        color: white;
        margin-bottom: 1rem;
    }
    
    .empty-cart p {
        color: #a0aec0;
        font-size: 1.6rem;
        margin-bottom: 3rem;
    }
    
    .empty-cart a {
        color: #48bb78;
        text-decoration: none;
        font-weight: 600;
    }
    
    .empty-cart a:hover {
        text-decoration: underline;
    }
    
    .btn-primary {
        background: linear-gradient(135deg, #667eea, #764ba2);
        color: white;
        border: none;
        padding: 1.5rem 3rem;
        border-radius: 12px;
        font-size: 1.4rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        display: inline-flex;
        align-items: center;
        gap: 0.8rem;
        text-decoration: none;
    }
    
    .btn-primary:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
    }
    
    .btn-secondary {
        background: rgba(255, 255, 255, 0.1);
        color: white;
        border: 1px solid rgba(255, 255, 255, 0.2);
        padding: 1rem 1.5rem;
        border-radius: 12px;
        font-size: 1.4rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
    }
    
    .btn-secondary:hover {
        background: rgba(255, 255, 255, 0.15);
        border-color: rgba(255, 255, 255, 0.3);
        transform: translateY(-2px);
        color: white;
        text-decoration: none;
    }
    
    .btn-outline {
        background: transparent;
        color: #a0aec0;
        border: 1px solid rgba(160, 174, 192, 0.3);
        padding: 1rem 1.5rem;
        border-radius: 12px;
        font-size: 1.4rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
    }
    
    .btn-outline:hover {
        background: rgba(160, 174, 192, 0.1);
        border-color: rgba(160, 174, 192, 0.5);
        color: white;
        transform: translateY(-2px);
    }
    
    @media (max-width: 768px) {
        .cart-container {
            padding: 0 1rem;
        }
        
        .cart-content {
            grid-template-columns: 1fr;
            gap: 2rem;
        }
        
        .cart-title {
            font-size: 2.5rem;
        }
        
        .cart-subtitle {
            font-size: 1.2rem;
        }
        
        .cart-summary {
            position: static;
            order: -1;
        }
        
        .cart-items-section {
            padding: 2rem;
        }
        
        .cart-item {
            padding: 1.5rem;
        }
        
        .item-header {
            flex-direction: column;
            gap: 1rem;
            align-items: flex-start;
        }
        
        .item-actions {
            flex-direction: column;
            gap: 0.5rem;
        }
        
        .item-actions button {
            width: 100%;
            justify-content: center;
        }
        
        .section-title {
            font-size: 2rem;
        }
        
        .item-info h4 {
            font-size: 1.6rem;
        }
        
        .item-info p {
            font-size: 1.2rem;
        }
    }
    
    @media (max-width: 480px) {
        .cart-container {
            padding: 0 0.5rem;
        }
        
        .cart-title {
            font-size: 2rem;
        }
        
        .cart-subtitle {
            font-size: 1rem;
        }
        
        .cart-items-section {
            padding: 1.5rem;
        }
        
        .cart-item {
            padding: 1rem;
        }
        
        .section-title {
            font-size: 1.8rem;
        }
        
        .item-info h4 {
            font-size: 1.4rem;
        }
        
        .item-info p {
            font-size: 1.1rem;
        }
        
        .btn-primary,
        .btn-secondary {
            padding: 1rem 1.5rem;
            font-size: 1rem;
            min-height: 44px;
            touch-action: manipulation;
        }
        
        /* Touch-friendly improvements */
        .cart-item {
            touch-action: manipulation;
        }
        
        .item-actions button {
            min-height: 44px;
            touch-action: manipulation;
        }
    }
</style>

<div class="cart-hero">
    <div class="cart-hero-content">
        <div class="cart-badge">
            <i class="fas fa-shopping-cart"></i>
            Your Music Collection
        </div>
        <h1 class="cart-title">Shopping Cart</h1>
        <p class="cart-subtitle">Review your music selections and complete your purchase to own these tracks forever</p>
    </div>
</div>

<div class="cart-container">
    <div class="cart-content">
        <!-- Cart Items -->
        <div class="cart-items-section">
            <h2 class="section-title">
                <i class="fas fa-music"></i>
                Your Tracks (<?= count($cart_items) ?>)
            </h2>
            
            <?php if (empty($cart_items)): ?>
                <div class="empty-cart">
                    <i class="fas fa-shopping-cart"></i>
                    <h3>Your cart is empty</h3>
                    <p>Browse amazing AI-generated music in our community and start building your collection!</p>
                    <a href="community_fixed.php" class="btn-primary">
                        <i class="fas fa-music"></i>
                        Browse Music Tracks
                    </a>
                </div>
            <?php else: ?>
                <?php foreach ($cart_items as $index => $item): ?>
                    <div class="cart-item">
                        <div class="item-header">
                            <div class="item-info">
                                <h4><?= htmlspecialchars($item['title']) ?></h4>
                                <p><i class="fas fa-user"></i> by <?= htmlspecialchars($item['artist_name']) ?></p>
                                
                                <?php if ($item['price'] == 0): ?>
                                    <div class="price-free">
                                        <i class="fas fa-gift"></i>
                                        <span>FREE Ɨ <?= $item['quantity'] ?></span>
                                    </div>
                                    <div class="price-info">
                                        <i class="fas fa-star"></i>
                                        Instant ownership included!
                                    </div>
                                <?php else: ?>
                                    <div class="price-details">
                                        $<?= number_format($item['price'], 2) ?> Ɨ <?= $item['quantity'] ?>
                                    </div>
                                    <?php if ($item['is_free_user_track']): ?>
                                        <div class="revenue-info">
                                            <i class="fas fa-info-circle"></i>
                                            Platform Revenue
                                        </div>
                                    <?php endif; ?>
                                <?php endif; ?>
                            </div>
                            <div class="item-price">
                                <?= $item['price'] == 0 ? 'FREE' : '$' . number_format($item['price'] * $item['quantity'], 2) ?>
                            </div>
                        </div>
                        
                        <div class="item-actions">
                            <button class="btn-remove" onclick="removeFromCart(<?= $item['track_id'] ?>)">
                                <i class="fas fa-trash"></i>
                                Remove
                            </button>
                            <button class="btn-secondary" onclick="playPreview('<?= htmlspecialchars($item['audio_url'] ?? '', ENT_QUOTES) ?>', '<?= htmlspecialchars($item['title'], ENT_QUOTES) ?>')" style="margin-left: 1rem;">
                                <i class="fas fa-play"></i>
                                Preview
                            </button>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
        
        <!-- Cart Summary -->
        <?php if (!empty($cart_items)): ?>
            <div class="cart-summary">
                <h2 class="section-title">
                    <i class="fas fa-receipt"></i>
                    Order Summary
                </h2>
                
                <?php if ($cart_total == 0): ?>
                    <div class="revenue-split-free">
                        <div class="free-ownership">
                            <i class="fas fa-star"></i>
                            100% FREE OWNERSHIP
                        </div>
                        <div class="ownership-details">
                            <span><i class="fas fa-music"></i> Tracks to Own:</span>
                            <span><?= count($cart_items) ?></span>
                        </div>
                        <div class="ownership-details">
                            <span><i class="fas fa-download"></i> Download Access:</span>
                            <span>Unlimited</span>
                        </div>
                        <div class="ownership-details">
                            <span><i class="fas fa-infinity"></i> Library Storage:</span>
                            <span>Forever</span>
                        </div>
                    </div>
                    
                    <div class="total-section">
                        <div class="total-row">
                            <span><i class="fas fa-gift"></i> Total Cost:</span>
                            <span class="total-free">FREE!</span>
                        </div>
                    </div>
                <?php else: ?>
                    <div class="revenue-split">
                        <h3 style="color: white; margin-bottom: 1.5rem;">Revenue Distribution</h3>
                        <div class="revenue-row artist">
                            <span>Artist Revenue:</span>
                            <span>$<?= number_format($artist_revenue, 2) ?></span>
                        </div>
                        <div class="revenue-row platform">
                            <span>Platform Revenue:</span>
                            <span>$<?= number_format($platform_revenue, 2) ?></span>
                        </div>
                    </div>
                    
                    <div class="total-section">
                        <div class="total-row">
                            <span>Total:</span>
                            <span>$<?= number_format($cart_total, 2) ?></span>
                        </div>
                    </div>
                <?php endif; ?>
                
                <div class="checkout-section">
                    <?php if ($user_id): ?>
                        <?php if ($cart_total == 0): ?>
                            <a href="checkout.php" class="btn-checkout btn-checkout-free">
                                <i class="fas fa-arrow-right"></i>
                                Proceed to FREE Checkout
                            </a>
                            <div class="checkout-info">
                                <i class="fas fa-star"></i>
                                Complete your free purchase securely!
                            </div>
                        <?php else: ?>
                            <a href="checkout.php" class="btn-checkout">
                                <i class="fas fa-arrow-right"></i>
                                Proceed to Checkout - $<?= number_format($cart_total, 2) ?>
                            </a>
                        <?php endif; ?>
                        
                        <!-- Cart Actions -->
                        <div style="display: flex; gap: 1rem; margin-top: 1.5rem;">
                            <button onclick="clearCart()" class="btn-secondary" style="flex: 1;">
                                <i class="fas fa-trash"></i>
                                Empty Cart
                            </button>
                            <a href="community_fixed.php" class="btn-secondary" style="flex: 1; text-decoration: none; display: flex; align-items: center; justify-content: center; gap: 0.5rem;">
                                <i class="fas fa-plus"></i>
                                Add More
                            </a>
                        </div>
                        
                        <!-- Save Cart -->
                        <div style="margin-top: 1rem;">
                            <button onclick="saveCart()" class="btn-outline" style="width: 100%;">
                                <i class="fas fa-bookmark"></i>
                                Save Cart for Later
                            </button>
                        </div>
                        
                    <?php else: ?>
                        <p style="color: #a0aec0; margin-bottom: 2rem;">Please log in to continue</p>
                        <a href="auth/login.php" class="btn-primary">
                            <i class="fas fa-sign-in-alt"></i>
                            Log In to Continue
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>

<script>
    function removeFromCart(trackId) {
        if (!confirm('Remove this track from your cart?')) {
            return;
        }
        
        fetch('cart.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: `action=remove&track_id=${trackId}`
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                location.reload();
            } else {
                alert('Failed to remove item from cart');
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('Error removing item from cart');
        });
    }
    
    
    function clearCart() {
        if (!confirm('Are you sure you want to empty your cart? This will remove all items.')) {
            return;
        }
        
        fetch('cart.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: 'action=clear'
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                location.reload();
            } else {
                alert('Failed to clear cart');
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('Error clearing cart');
        });
    }
    
    function saveCart() {
        // Simple implementation - just show a success message for now
        // You could implement actual cart saving to database later
        alert('šŸ”– Cart saved! You can continue shopping and come back to these items later.\n\nšŸ’” Pro tip: Your cart is automatically saved while you browse!');
    }
    
    function playPreview(audioUrl, title) {
        if (!audioUrl) {
            alert('No preview available for this track');
            return;
        }
        
        // Use the global player if available
        if (window.enhancedGlobalPlayer && window.enhancedGlobalPlayer.playTrack) {
            window.enhancedGlobalPlayer.playTrack(audioUrl, title, 'Cart Preview');
        } else if (window.playTrack) {
            window.playTrack(audioUrl, title);
        } else {
            // Fallback - simple audio element (no auto-play)
            const audio = new Audio(audioUrl);
            // Don't auto-play - let user control
            alert(`šŸŽµ Ready to play: "${title}" - Click play to start`);
        }
    }
    
    // Add some nice touches
    document.addEventListener('DOMContentLoaded', function() {
        // Auto-save cart every 30 seconds (visual feedback only)
        setInterval(function() {
            const saveButton = document.querySelector('.btn-outline');
            if (saveButton && saveButton.textContent.includes('Save Cart')) {
                const originalText = saveButton.innerHTML;
                saveButton.innerHTML = '<i class="fas fa-check"></i> Cart Auto-Saved';
                saveButton.style.color = '#48bb78';
                saveButton.style.borderColor = 'rgba(72, 187, 120, 0.3)';
                
                setTimeout(() => {
                    saveButton.innerHTML = originalText;
                    saveButton.style.color = '';
                    saveButton.style.borderColor = '';
                }, 2000);
            }
        }, 30000);
    });
</script>

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

CasperSecurity Mini