<?php
// File: /admin/paintings.php (Updated for Featured Paintings)
include 'header.php';

// Fetch all paintings, joining with artist profiles to get the artist's name
$paintings = [];
$sql = "SELECT p.*, CONCAT(ap.first_name, ' ', ap.last_name) as artist_name 
        FROM paintings p
        LEFT JOIN artist_profiles ap ON p.artist_id = ap.user_id
        ORDER BY p.uploaded_at DESC";
$result = $conn->query($sql);
if ($result) {
    $paintings = $result->fetch_all(MYSQLI_ASSOC);
}
?>

<div class="p-6 md:p-8">
    <h1 class="text-3xl font-bold text-ucf-charcoal mb-6">Manage Paintings</h1>

    <div class="bg-white p-6 rounded-lg shadow-md">
        <h2 class="text-2xl font-bold text-ucf-charcoal mb-4">All Uploaded Artwork</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">Image</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Title</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Artist</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Price</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Featured</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Actions</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <?php if (!empty($paintings)): ?>
                        <?php foreach ($paintings as $painting): ?>
                            <tr>
                                <td class="px-6 py-4"><img src="../<?php echo htmlspecialchars($painting['image_path']); ?>" class="h-12 w-12 object-cover rounded"></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><?php echo htmlspecialchars($painting['title']); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo htmlspecialchars(trim($painting['artist_name']) ?: 'N/A'); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">₹<?php echo number_format($painting['price'], 2); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm">
                                    <!-- Feature Toggle Form -->
                                    <form action="paintings_handler.php" method="POST">
                                        <input type="hidden" name="painting_id" value="<?php echo $painting['id']; ?>">
                                        <input type="hidden" name="current_status" value="<?php echo $painting['is_featured']; ?>">
                                        <button type="submit" name="toggle_featured" class="px-3 py-1 text-xs font-semibold rounded-full <?php echo $painting['is_featured'] ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800'; ?>">
                                            <?php echo $painting['is_featured'] ? 'Yes' : 'No'; ?>
                                        </button>
                                    </form>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                    <form action="paintings_handler.php" method="POST" onsubmit="return confirm('Are you sure you want to delete this painting?');">
                                        <input type="hidden" name="painting_id" value="<?php echo $painting['id']; ?>">
                                        <button type="submit" name="delete_painting" class="text-red-600 hover:text-red-900">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="6" class="text-center py-12">No paintings have been uploaded yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>

