<?php
include 'auth.php';
include_once '../config/database.php';

if (isset($_POST['action']) && $_POST['action'] == 'update_status') {
    $stmt = $conn->prepare("UPDATE bookings SET status = ? WHERE id = ?");
    $stmt->execute([$_POST['status'], $_POST['id']]);
    header("Location: bookings.php");
}

if (isset($_GET['delete'])) {
    $stmt = $conn->prepare("DELETE FROM bookings WHERE id = ?");
    $stmt->execute([$_GET['delete']]);
    header("Location: bookings.php");
}

$bookings = $conn->query("SELECT b.*, u.name as user_name, u.email as user_email, g.name as game_name 
                        FROM bookings b 
                        JOIN users u ON b.user_id = u.id 
                        JOIN games g ON b.game_id = g.id 
                        ORDER BY b.id DESC")->fetchAll();

ob_start();
?>

<div class="card p-4">
    <h5 class="fw-bold mb-4">إدارة الحجوزات</h5>
    <table class="table table-hover align-middle">
        <thead>
            <tr>
                <th>رقم الحجز</th>
                <th>العميل</th>
                <th>اللعبة</th>
                <th>الأفراد</th>
                <th>التاريخ</th>
                <th>الحالة</th>
                <th>الإجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($bookings as $booking): ?>
            <tr>
                <td>#<?= $booking['id'] ?></td>
                <td>
                    <div class="fw-bold"><?= htmlspecialchars($booking['user_name']) ?></div>
                    <small class="text-muted"><?= htmlspecialchars($booking['user_email']) ?></small>
                </td>
                <td><?= htmlspecialchars($booking['game_name']) ?></td>
                <td><?= $booking['persons'] ?></td>
                <td><?= $booking['date'] ?></td>
                <td>
                    <span class="badge badge-<?= strtolower($booking['status']) ?>">
                        <?= $booking['status'] ?>
                    </span>
                </td>
                <td>
                    <div class="btn-group">
                        <form method="POST" class="d-inline">
                            <input type="hidden" name="id" value="<?= $booking['id'] ?>">
                            <input type="hidden" name="action" value="update_status">
                            <input type="hidden" name="status" value="Approved">
                            <button type="submit" class="btn btn-sm btn-light text-success" title="Approve"><i class="fas fa-check"></i></button>
                        </form>
                        <form method="POST" class="d-inline">
                            <input type="hidden" name="id" value="<?= $booking['id'] ?>">
                            <input type="hidden" name="action" value="update_status">
                            <input type="hidden" name="status" value="Cancelled">
                            <button type="submit" class="btn btn-sm btn-light text-danger" title="Cancel"><i class="fas fa-times"></i></button>
                        </form>
                        <a href="?delete=<?= $booking['id'] ?>" class="btn btn-sm btn-light text-muted" onclick="return confirm('حذف؟')"><i class="fas fa-trash"></i></a>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php
$content = ob_get_clean();
$page_title = 'إدارة الحجوزات';
include 'layout.php';
?>
