<?php require "../includes/header.php"; ?>
<?php require "../../config/config.php"; ?>

<?php
if(!isset($_SESSION['admin_username'])){ header("location: ../login.php"); exit; }

// Filter by status
$filter = isset($_GET['filter']) ? $_GET['filter'] : 'all';
if($filter === 'paid'){
    $bookings = $conn->query("SELECT * FROM bookings WHERE payment_status='paid' ORDER BY id DESC")->fetchAll(PDO::FETCH_OBJ);
} elseif($filter === 'pending'){
    $bookings = $conn->query("SELECT * FROM bookings WHERE payment_status='pending' ORDER BY id DESC")->fetchAll(PDO::FETCH_OBJ);
} else {
    $bookings = $conn->query("SELECT * FROM bookings ORDER BY id DESC")->fetchAll(PDO::FETCH_OBJ);
}
?>

<div class="topbar">
    <h4><i class="fa fa-calendar mr-2"></i> Bookings</h4>
    <div class="admin-badge">
        <div class="avatar"><?php echo strtoupper(substr($_SESSION['admin_username'],0,1)); ?></div>
        <span><?php echo htmlspecialchars($_SESSION['admin_username']); ?></span>
        <a href="../logout.php"><i class="fa fa-sign-out"></i> Logout</a>
    </div>
</div>

<div class="main-content">
    <?php if(isset($_GET['success'])): ?><div class="alert alert-success mb-3"><?php echo htmlspecialchars($_GET['success']); ?></div><?php endif; ?>

    <!-- Filter Tabs -->
    <div class="mb-3">
        <a href="index.php?filter=all"     class="btn btn-sm <?php echo $filter==='all'     ? 'btn-admin-primary' : 'btn-outline-secondary'; ?> mr-1">All</a>
        <a href="index.php?filter=paid"    class="btn btn-sm <?php echo $filter==='paid'    ? 'btn-success' : 'btn-outline-success'; ?> mr-1">Paid</a>
        <a href="index.php?filter=pending" class="btn btn-sm <?php echo $filter==='pending' ? 'btn-warning' : 'btn-outline-warning'; ?>">Pending</a>
    </div>

    <div class="admin-table">
        <div class="table-section-header">
            <h5><i class="fa fa-list mr-2"></i> All Bookings (<?php echo count($bookings); ?>)</h5>
        </div>
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>#</th><th>Guest</th><th>Email</th><th>Phone</th>
                        <th>Hotel</th><th>Room</th><th>Check-In</th><th>Check-Out</th>
                        <th>Amount</th><th>Payment</th><th>Ref</th><th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($bookings as $b): ?>
                    <tr>
                        <td><?php echo $b->id; ?></td>
                        <td><strong><?php echo htmlspecialchars($b->full_name); ?></strong></td>
                        <td><?php echo htmlspecialchars($b->email); ?></td>
                        <td><?php echo htmlspecialchars($b->phone_number); ?></td>
                        <td><?php echo htmlspecialchars($b->hotel_name); ?></td>
                        <td><?php echo htmlspecialchars($b->room_name); ?></td>
                        <td><?php echo htmlspecialchars($b->check_in); ?></td>
                        <td><?php echo htmlspecialchars($b->check_out); ?></td>
                        <td>GH&#8373;<?php echo number_format($b->price, 2); ?></td>
                        <td>
                            <?php if($b->payment_status === 'paid'): ?>
                                <span class="badge-paid">PAID</span>
                            <?php else: ?>
                                <span class="badge-pending">PENDING</span>
                            <?php endif; ?>
                        </td>
                        <td style="max-width:100px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">
                            <?php echo $b->transaction_ref ? htmlspecialchars(substr($b->transaction_ref, 0, 15)) . '...' : 'N/A'; ?>
                        </td>
                        <td>
                            <?php if($b->payment_status !== 'paid'): ?>
                            <a href="update-status.php?id=<?php echo $b->id; ?>&status=paid" class="btn btn-sm btn-action btn-success" onclick="return confirm('Mark as paid?')">Mark Paid</a>
                            <?php else: ?>
                            <a href="update-status.php?id=<?php echo $b->id; ?>&status=pending" class="btn btn-sm btn-action btn-warning" onclick="return confirm('Mark as pending?')">Mark Pending</a>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if(count($bookings) === 0): ?>
                    <tr><td colspan="12" class="text-center text-muted py-4">No bookings found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require "../includes/footer.php"; ?>
