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

<?php
if(!isset($_SESSION['admin_username'])){ header("location: ../login.php"); exit; }
$hotels = $conn->query("SELECT * FROM hotels ORDER BY id DESC")->fetchAll(PDO::FETCH_OBJ);
?>

<div class="topbar">
    <h4><i class="fa fa-building mr-2"></i> Hotels</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; ?>

    <div class="admin-table">
        <div class="table-section-header">
            <h5><i class="fa fa-building mr-2"></i> All Hotels</h5>
            <a href="create.php" class="btn btn-admin-primary"><i class="fa fa-plus mr-1"></i> Add Hotel</a>
        </div>
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr><th>#</th><th>Image</th><th>Name</th><th>Location</th><th>Status</th><th>Actions</th></tr>
                </thead>
                <tbody>
                    <?php foreach($hotels as $h): ?>
                    <tr>
                        <td><?php echo $h->id; ?></td>
                        <td><img src="<?php echo APPURL; ?>/images/<?php echo htmlspecialchars($h->image); ?>" style="width:60px;height:45px;object-fit:cover;border-radius:6px;"></td>
                        <td><strong><?php echo htmlspecialchars($h->name); ?></strong></td>
                        <td><?php echo htmlspecialchars($h->location); ?></td>
                        <td>
                            <?php if($h->status == 1): ?>
                                <span class="badge-active">Active</span>
                            <?php else: ?>
                                <span class="badge-inactive">Inactive</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <a href="update-status.php?id=<?php echo $h->id; ?>&status=<?php echo $h->status == 1 ? 0 : 1; ?>" class="btn btn-sm btn-action <?php echo $h->status == 1 ? 'btn-warning' : 'btn-success'; ?>">
                                <?php echo $h->status == 1 ? 'Deactivate' : 'Activate'; ?>
                            </a>
                            <a href="update.php?id=<?php echo $h->id; ?>" class="btn btn-sm btn-action btn-primary">Edit</a>
                            <a href="delete.php?id=<?php echo $h->id; ?>" class="btn btn-sm btn-action btn-danger" onclick="return confirm('Delete this hotel?')">Delete</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if(count($hotels) === 0): ?>
                    <tr><td colspan="6" class="text-center text-muted py-4">No hotels found. <a href="create.php">Add one</a></td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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