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

$uid = $_SESSION['user_id'];

if (isset($_GET['cancel'])) {
    $id = (int)$_GET['cancel'];
    $check = mysqli_query($conn, "SELECT * FROM bookings WHERE id=$id AND user_id=$uid AND status='pending'");
    if (mysqli_num_rows($check) > 0) {
        $b = mysqli_fetch_assoc($check);
        mysqli_query($conn, "UPDATE bookings SET status='cancelled' WHERE id=$id");
        mysqli_query($conn, "UPDATE hall_dates SET status='available' WHERE hall_id={$b['hall_id']} AND available_date='{$b['booking_date']}'");
        redirect(BASE_URL.'/user/my-bookings.php', 'Booking cancelled.');
    } else {
        redirect(BASE_URL.'/user/my-bookings.php', 'Cannot cancel. Contact admin for confirmed bookings.', 'danger');
    }
}

$bookings = mysqli_query($conn, "SELECT b.*, h.hall_name, h.location, h.main_image 
                                  FROM bookings b JOIN halls h ON b.hall_id=h.id 
                                  WHERE b.user_id=$uid ORDER BY b.created_at DESC");
?>

<div class="page-header">
    <h1>My Bookings</h1>
    <p style="color:#888; font-size:14px;"><?php echo mysqli_num_rows($bookings); ?> bookings</p>
</div>

<?php showFlash(); ?>

<div class="table-wrapper">
    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Hall</th>
                <th>Location</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>
                            <img src="<?php echo $b['main_image'] ? BASE_URL.'/assets/uploads/halls/'.$b['main_image'] : 'https://via.placeholder.com/50'; ?>" 
                                 style="width:45px; height:40px; object-fit:cover; border-radius:4px; vertical-align:middle; margin-right:10px;">
                            <strong><?php echo htmlspecialchars($b['hall_name']); ?></strong>
                        </td>
                        <td style="font-family:var(--font-serif); font-style:italic; color:var(--bronze);"><?php echo htmlspecialchars($b['location']); ?></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>
                            <a href="<?php echo BASE_URL; ?>/hall-details.php?id=<?php echo $b['hall_id']; ?>" target="_blank" class="btn btn-sm">View</a>
                            <?php if ($b['status'] === 'pending'): ?>
                                <a href="?cancel=<?php echo $b['id']; ?>" onclick="return confirm('Cancel this booking?')" class="btn btn-sm btn-danger">Cancel</a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr><td colspan="8" style="text-align:center; padding:40px; color:#888;">
                    <div style="font-size:40px; color:var(--bronze); margin-bottom:10px;">❦</div>
                    No bookings yet. <a href="<?php echo BASE_URL; ?>/halls.php" style="color:var(--maroon); font-weight:600;">Browse halls</a> to make your first booking.
                </td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

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