<?php
// File: /artist/orders.php
include 'header.php'; // Includes security check and DB connection

$artist_id = $_SESSION['user_id'];
$sales = [];

// This query finds all completed order items that contain paintings by the logged-in artist.
$sql = "SELECT 
            o.id as order_id,
            o.order_date,
            o.shipping_name as customer_name,
            p.title as painting_title,
            p.image_path as painting_image,
            oi.quantity,
            oi.price_at_purchase
        FROM orders o
        JOIN order_items oi ON o.id = oi.order_id
        JOIN paintings p ON oi.painting_id = p.id
        WHERE p.artist_id = ? AND o.payment_status = 'completed'
        ORDER BY o.order_date DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $artist_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result) {
    $sales = $result->fetch_all(MYSQLI_ASSOC);
}
$stmt->close();
?>

<div class="p-6 md:p-8">
    <h1 class="text-3xl font-bold text-ucf-charcoal mb-6">My Sales</h1>

    <div class="bg-white p-6 rounded-lg shadow-md">
        <h2 class="text-2xl font-bold text-ucf-charcoal mb-4">Sales History</h2>
        
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Order ID</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Date</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Painting Sold</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Customer</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Amount</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <?php if (!empty($sales)): ?>
                        <?php foreach ($sales as $sale): ?>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">#<?php echo $sale['order_id']; ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo date('M d, Y', strtotime($sale['order_date'])); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 flex items-center">
                                    <img src="../<?php echo htmlspecialchars($sale['painting_image']); ?>" class="h-10 w-10 object-cover rounded-md mr-3">
                                    <?php echo htmlspecialchars($sale['painting_title']); ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo htmlspecialchars($sale['customer_name']); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-semibold text-ucf-green">
                                    ₹<?php echo number_format($sale['price_at_purchase'], 2); ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="5" class="px-6 py-12 text-center text-gray-500">
                                <i class="fas fa-receipt fa-2x text-gray-300 mb-2"></i>
                                <p>You have not made any sales yet.</p>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
