<?php
$pageTitle = "All Halls";
require_once 'sidebar.php';

if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    mysqli_query($conn, "DELETE FROM halls WHERE id=$id");
    redirect(BASE_URL.'/admin/halls.php', 'Hall deleted.');
}

$halls = mysqli_query($conn, "SELECT h.*, u.name as vendor_name,
                              (SELECT COUNT(*) FROM bookings WHERE hall_id=h.id) as bookings
                              FROM halls h LEFT JOIN users u ON h.vendor_id=u.id 
                              ORDER BY h.id DESC");
?>

<div class="page-header">
    <h1>All Shadi Halls</h1>
    <p style="color:#888; font-size:14px;"><?php echo mysqli_num_rows($halls); ?> halls listed</p>
</div>

<?php showFlash(); ?>

<div class="table-wrapper">
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Image</th>
                <th>Hall Name</th>
                <th>Vendor</th>
                <th>Location</th>
                <th>Price</th>
                <th>Capacity</th>
                <th>Bookings</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if (mysqli_num_rows($halls) > 0): ?>
                <?php while ($h = mysqli_fetch_assoc($halls)): ?>
                    <tr>
                        <td>#<?php echo $h['id']; ?></td>
                        <td>
                            <img src="<?php echo $h['main_image'] ? BASE_URL.'/assets/uploads/halls/'.$h['main_image'] : 'https://via.placeholder.com/60'; ?>" 
                                 style="width:55px; height:45px; object-fit:cover; border-radius:4px;">
                        </td>
                        <td><strong style="color:var(--maroon-dark);"><?php echo htmlspecialchars($h['hall_name']); ?></strong></td>
                        <td><?php echo htmlspecialchars($h['vendor_name']); ?></td>
                        <td style="font-family:var(--font-serif); font-style:italic; color:var(--bronze);"><?php echo htmlspecialchars($h['location']); ?></td>
                        <td style="color:var(--bronze); font-weight:600;">Rs. <?php echo number_format($h['price_per_day']); ?></td>
                        <td><?php echo $h['capacity']; ?></td>
                        <td style="text-align:center;"><strong><?php echo $h['bookings']; ?></strong></td>
                        <td>
                            <a href="<?php echo BASE_URL; ?>/hall-details.php?id=<?php echo $h['id']; ?>" class="btn btn-sm" target="_blank">View</a>
                            <a href="?delete=<?php echo $h['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this hall?')">Delete</a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr><td colspan="9" style="text-align:center; padding:40px; color:#888;">
                    <div style="font-size:40px; color:var(--bronze); margin-bottom:10px;">❦</div>
                    No halls added yet.
                </td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

</main></div></body></html>
