<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

// ==================== FILTERS ====================
$status_filter = $_GET['status'] ?? '';
$room_filter   = $_GET['room'] ?? '';

// Fetch bookings first
$query = "
    SELECT cb.*, g.name AS guest_name, cr.name AS room_name, cr.price AS room_price
    FROM conference_bookings cb
    LEFT JOIN guests g ON cb.guest_id = g.id
    LEFT JOIN conference_rooms cr ON cb.conference_room_id = cr.id
    WHERE 1
";
if ($status_filter) {
    $safe_status = $conn->real_escape_string($status_filter);
    $query .= " AND cb.status='$safe_status'";
}
if ($room_filter) {
    $safe_room = intval($room_filter);
    $query .= " AND cb.conference_room_id=$safe_room";
}
$bookings = $conn->query($query);

// Aggregate data
$total_attendees = 0;
$food_totals = []; // food_name => quantity needed

while ($b = $bookings->fetch_assoc()) {
    $total_attendees += $b['attendees'];

    // Fetch food items per booking
    $stmt = $conn->prepare("
        SELECT rm.name AS food_name, cbr.quantity
        FROM conference_booking_resources cbr
        LEFT JOIN restaurant_menu rm ON cbr.resource_id = rm.id
        WHERE cbr.booking_id=? AND cbr.resource_type='FOOD'
    ");
    $stmt->bind_param("i", $b['id']);
    $stmt->execute();
    $res = $stmt->get_result();

    while ($f = $res->fetch_assoc()) {
        $qty_needed = ($f['quantity'] ?: 1) * ($b['attendees'] ?: 1);
        if(isset($food_totals[$f['food_name']])) {
            $food_totals[$f['food_name']] += $qty_needed;
        } else {
            $food_totals[$f['food_name']] = $qty_needed;
        }
    }
}
?>

<div class="container mt-4">
    <h3 class="text-center mb-4">📊 Conference Bookings Report</h3>
    <hr>

    <div class="mb-3">
        <strong>Total Bookings:</strong> <?= $bookings->num_rows ?><br>
        <strong>Total Attendees:</strong> <?= $total_attendees ?>
    </div>

    <!-- Food Items Needed -->
    <h5>🍽 Food Items Needed</h5>
    <?php if(count($food_totals) > 0): ?>
    <table class="table table-bordered">
        <thead class="table-light">
            <tr>
                <th>#</th>
                <th>Food Item</th>
                <th>Total Quantity Needed</th>
            </tr>
        </thead>
        <tbody>
            <?php $count=1; foreach($food_totals as $food=>$qty): ?>
            <tr>
                <td><?= $count++ ?></td>
                <td><?= htmlspecialchars($food) ?></td>
                <td><?= $qty ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php else: ?>
        <p class="text-muted">No food items found for selected bookings.</p>
    <?php endif; ?>

    <!-- Print / Export -->
    <div class="mt-3">
        <button class="btn btn-dark" onclick="window.print()"><i class="fa fa-print"></i> Print Report</button>
        <a href="export_conference_report.php?status=<?= $status_filter ?>&room=<?= $room_filter ?>" class="btn btn-success">Export CSV</a>
    </div>
</div>

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