<?php
// my_sales.php
require 'config/db.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['user_id'])) { 
    header("Location: login.php"); 
    exit; 
}

$current_user_id = $_SESSION['user_id']; 
$admin_commission = 0.10; // 10% de comisión

try {
    $stmt = $pdo->prepare("
        SELECT o.*, p.title as product_title, b.username as buyer_username
        FROM orders o
        JOIN products p ON o.product_id = p.id
        JOIN users b ON o.buyer_id = b.id
        WHERE o.seller_id = ? 
        ORDER BY o.created_at DESC
    ");
    $stmt->execute([$current_user_id]);
    $sales_list = $stmt->fetchAll();

} catch (PDOException $e) {
    die("Database Error: " . $e->getMessage());
}

include 'includes/header.php';
?>

<div style="max-width: 1000px; margin: 40px auto; padding: 0 20px; font-family: 'Inter', sans-serif;">
    <h2 style="color: var(--primary); font-family: 'Syncopate', sans-serif; margin-bottom: 30px;">
        <i class="fa-solid fa-sack-dollar"></i> MY SALES
    </h2>

    <?php if ($sales_list): ?>
        <div style="display: flex; flex-direction: column; gap: 15px;">
            <?php foreach ($sales_list as $sale): 
                // CÁLCULO DE COMISIÓN PARA MOSTRAR EN VISTA
                $net_payout = $sale['total_price'] * (1 - $admin_commission);
            ?>
                <div style="background: var(--nav-bg); border: 1px solid var(--border); padding: 20px; border-radius: 15px; display: flex; justify-content: space-between; align-items: center;">
                    <div style="display: flex; gap: 20px; align-items: center;">
                        <div style="width: 50px; height: 50px; background: rgba(0, 255, 136, 0.1); border-radius: 8px; border: 1px solid var(--primary); display: flex; align-items: center; justify-content: center;">
                            <i class="fa-solid fa-hand-holding-dollar" style="color: var(--primary);"></i>
                        </div>
                        <div>
                            <h4 style="margin: 0; color: white;"><?php echo htmlspecialchars($sale['product_title']); ?></h4>
                            <span style="font-size: 12px; color: var(--text-gray);">Buyer: @<?php echo htmlspecialchars($sale['buyer_username']); ?></span>
                        </div>
                    </div>
                    
                    <div style="text-align: right; display: flex; flex-direction: column; gap: 5px;">
                        <div style="font-size: 10px; color: var(--text-gray); text-transform: uppercase; font-weight: 700;">Net Payout</div>
                        <div style="font-weight: 800; color: var(--primary); font-size: 18px;">
                            +$<?php echo number_format($net_payout, 2); ?>
                        </div>
                        <div style="font-size: 9px; color: #555;">(Fee: 10% applied)</div>
                        
                        <div style="margin-top: 5px;">
                            <?php if($sale['status'] === 'dispute'): ?>
                                <a href="dispute_chat.php?id=<?php echo $sale['id']; ?>" style="text-decoration: none; background: #ff4d4d; color: white; padding: 5px 12px; border-radius: 6px; font-size: 11px; font-weight: bold;">
                                    <i class="fa-solid fa-gavel"></i> UNDER DISPUTE
                                </a>
                            <?php else: ?>
                                <span style="font-size: 10px; padding: 4px 8px; border-radius: 4px; background: rgba(255,255,255,0.05); color: var(--text-gray); text-transform: uppercase; font-weight: bold;">
                                    <?php echo htmlspecialchars($sale['status']); ?>
                                </span>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div style="text-align: center; padding: 60px; color: var(--text-gray);">
            <p>No sales records found.</p>
        </div>
    <?php endif; ?>
</div>

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