<?php
$pageTitle = "Manage Bookings";
require_once 'sidebar.php';

if (isset($_GET['action']) && isset($_GET['id'])) {
    $id = (int)$_GET['id'];
    $action = $_GET['action'];
    
    $bookingRes = mysqli_query($conn, "SELECT * FROM bookings WHERE id=$id");
    $booking = mysqli_fetch_assoc($bookingRes);
    
    if ($booking) {
        if ($action === 'confirm') {
            mysqli_query($conn, "UPDATE bookings SET status='confirmed' WHERE id=$id");
            mysqli_query($conn, "UPDATE hall_dates SET status='booked' WHERE hall_id={$booking['hall_id']} AND available_date='{$booking['booking_date']}'");
            redirect(BASE_URL.'/admin/bookings.php', 'Booking confirmed. Date marked as booked.');
        } elseif ($action === 'cancel') {
            mysqli_query($conn, "UPDATE bookings SET status='cancelled' WHERE id=$id");
            mysqli_query($conn, "UPDATE hall_dates SET status='available' WHERE hall_id={$booking['hall_id']} AND available_date='{$booking['booking_date']}'");
            redirect(BASE_URL.'/admin/bookings.php', 'Booking cancelled. Date freed up.');
        } elseif ($action === 'delete') {
            mysqli_query($conn, "DELETE FROM bookings WHERE id=$id");
            mysqli_query($conn, "UPDATE hall_dates SET status='available' WHERE hall_id={$booking['hall_id']} AND available_date='{$booking['booking_date']}'");
            redirect(BASE_URL.'/admin/bookings.php', 'Booking deleted.');
        }
    }
}

$filter = $_GET['status'] ?? 'all';
$where = '';
if (in_array($filter, ['pending','confirmed','cancelled'])) {
    $where = "WHERE b.status='$filter'";
}

$bookings = mysqli_query($conn, "SELECT b.*, u.name as user_name, u.phone as user_phone, u.email as user_email, h.hall_name 
                                  FROM bookings b 
                                  JOIN users u ON b.user_id = u.id 
                                  JOIN halls h ON b.hall_id = h.id 
                                  $where
                                  ORDER BY CASE b.status WHEN 'pending' THEN 1 WHEN 'confirmed' THEN 2 ELSE 3 END, b.created_at DESC");
?>

<div class="page-header">
    <h1>Manage Bookings</h1>
    <div class="filter-group">
        <a href="?status=all" class="filter-btn <?php echo $filter=='all'?'active':''; ?>">All</a>
        <a href="?status=pending" class="filter-btn <?php echo $filter=='pending'?'active':''; ?>">Pending</a>
        <a href="?status=confirmed" class="filter-btn <?php echo $filter=='confirmed'?'active':''; ?>">Confirmed</a>
        <a href="?status=cancelled" class="filter-btn <?php echo $filter=='cancelled'?'active':''; ?>">Cancelled</a>
    </div>
</div>

<?php showFlash(); ?>

<div class="table-wrapper">
    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>User</th>
                <th>Contact</th>
                <th>Hall</th>
                <th>Date</th>
                <th>Guests</th>
                <th>Amount</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if (mysqli_num_rows($bookings) > 0): ?>
                <?php while ($b = mysqli_fetch_assoc($bookings)): ?>
                    <tr>
                        <td>#<?php echo $b['id']; ?></td>
                        <td><strong><?php echo htmlspecialchars($b['user_name']); ?></strong></td>
                        <td>
                            <small><?php echo htmlspecialchars($b['user_email']); ?><br>
                            <?php echo htmlspecialchars($b['user_phone']); ?></small>
                        </td>
                        <td><?php echo htmlspecialchars($b['hall_name']); ?></td>
                        <td style="font-family:var(--font-display); color:var(--maroon);"><?php echo date('d M Y', strtotime($b['booking_date'])); ?></td>
                        <td style="text-align:center;"><?php echo $b['guests']; ?></td>
                        <td style="color:var(--bronze); font-weight:600;">Rs. <?php echo number_format($b['total_amount']); ?></td>
                        <td><span class="badge badge-<?php echo $b['status']; ?>"><?php echo ucfirst($b['status']); ?></span></td>
                        <td>
                            <?php if ($b['status'] === 'pending'): ?>
                                <a href="?action=confirm&id=<?php echo $b['id']; ?>" class="btn btn-sm btn-success">Confirm</a>
                                <a href="?action=cancel&id=<?php echo $b['id']; ?>" class="btn btn-sm btn-danger">Cancel</a>
                            <?php elseif ($b['status'] === 'confirmed'): ?>
                                <a href="?action=cancel&id=<?php echo $b['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Cancel this confirmed booking?')">Cancel</a>
                            <?php endif; ?>
                            <a href="?action=delete&id=<?php echo $b['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete booking?')">🗑</a>
                        </td>
                    </tr>
                    <?php if (!empty($b['notes'])): ?>
                        <tr><td colspan="9" style="background:var(--cream); font-size:13px; padding:12px 20px; font-style:italic;">
                            <strong style="color:var(--maroon);">Notes:</strong> <?php echo htmlspecialchars($b['notes']); ?>
                        </td></tr>
                    <?php endif; ?>
                <?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 bookings found.
                </td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

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