<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

// ================== CREATE RESERVATION ==================
if(isset($_POST['reserve'])){
    $guest_id = intval($_POST['guest_id']);
    $room_id  = intval($_POST['room_id']);
    $date_in  = $_POST['date_in'];
    $date_out = $_POST['date_out'];

    // Insert reservation
    $stmt=$conn->prepare("INSERT INTO reservations (guest_id,room_id,date_in,date_out,status) VALUES (?,?,?,?, 'Reserved')");
    $stmt->bind_param("iiss",$guest_id,$room_id,$date_in,$date_out);
    $stmt->execute();

    // Update room status
    $conn->query("UPDATE rooms SET status='Reserved' WHERE id=$room_id");

    echo "<script>alert('Reservation created');window.location='reservations.php';</script>";
}

// ================== CANCEL RESERVATION ==================
if(isset($_GET['cancel_id'])){
    $res_id = intval($_GET['cancel_id']);

    // Get room id for this reservation
    $res_data = $conn->query("SELECT room_id FROM reservations WHERE id=$res_id")->fetch_assoc();
    if($res_data){
        $room_id = $res_data['room_id'];
        $conn->query("UPDATE rooms SET status='Available' WHERE id=$room_id");
    }

    // Delete or mark reservation as canceled
    $conn->query("UPDATE reservations SET status='Canceled' WHERE id=$res_id");

    echo "<script>alert('Reservation canceled');window.location='reservations.php';</script>";
}

// ================== FETCH DATA ==================
$guests = $conn->query("SELECT * FROM guests ORDER BY name");
$rooms  = $conn->query("SELECT * FROM rooms WHERE status='Available'");
$res    = $conn->query("
    SELECT r.*, g.name AS guest_name, rm.room_number
    FROM reservations r
    JOIN guests g ON r.guest_id=g.id
    JOIN rooms rm ON r.room_id=rm.id
    ORDER BY r.id DESC
");
?>

<div class="container mt-4">
<h4>Reservations</h4><hr>

<!-- Create Reservation Form -->
<form method="POST" class="row g-2 mb-4">
    <div class="col-md-3">
        <select name="guest_id" class="form-control" required>
            <option>Select Guest</option>
            <?php while($g=$guests->fetch_assoc()): ?>
                <option value="<?= $g['id'] ?>"><?= htmlspecialchars($g['name']) ?></option>
            <?php endwhile; ?>
        </select>
    </div>

    <div class="col-md-3">
        <select name="room_id" class="form-control" required>
            <option>Select Room</option>
            <?php while($r=$rooms->fetch_assoc()): ?>
                <option value="<?= $r['id'] ?>">Room <?= $r['room_number'] ?></option>
            <?php endwhile; ?>
        </select>
    </div>

    <div class="col-md-2"><input type="date" name="date_in" class="form-control" required></div>
    <div class="col-md-2"><input type="date" name="date_out" class="form-control" required></div>
    <div class="col-md-2"><button name="reserve" class="btn btn-warning w-100">Reserve</button></div>
</form>

<!-- Reservations Table -->
<table class="table table-bordered table-sm">
<thead class="table-dark">
<tr>
    <th>Guest</th>
    <th>Room</th>
    <th>From</th>
    <th>To</th>
    <th>Status</th>
    <th>Actions</th>
</tr>
</thead>
<tbody>
<?php while($r=$res->fetch_assoc()): ?>
<tr <?= $r['status']=='Canceled' ? 'class="table-danger"' : '' ?>>
    <td><?= htmlspecialchars($r['guest_name']) ?></td>
    <td><?= htmlspecialchars($r['room_number']) ?></td>
    <td><?= $r['date_in'] ?></td>
    <td><?= $r['date_out'] ?></td>
    <td><?= $r['status'] ?></td>
    <td>
        <?php if($r['status']=='Reserved'): ?>
            <a href="reservations.php?cancel_id=<?= $r['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Cancel this reservation?')">
                <i class="fa fa-times"></i> Cancel
            </a>
        <?php else: ?>
            <span class="text-muted">N/A</span>
        <?php endif; ?>
    </td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>

<?php include 'includes/footer.php'; ?>
