<?php 
include 'config.php'; 

// Check if room_id is set
if (!isset($_GET['room_id'])) {
    header("Location: index.php");
    exit();
}

$room_id = intval($_GET['room_id']);

// Fetch Room Details
$stmt = $conn->prepare("SELECT * FROM rooms WHERE id = ?");
$stmt->bind_param("i", $room_id);
$stmt->execute();
$room = $stmt->get_result()->fetch_assoc();

if (!$room) {
    die("Room not found.");
}

// PRE-FILL DATA FROM URL (If coming from the Grid View)
$pre_date = isset($_GET['date']) ? $_GET['date'] : '';
$pre_start = isset($_GET['start']) ? $_GET['start'] : ''; // e.g. 09:00
$pre_end   = isset($_GET['end']) ? $_GET['end'] : '';     // e.g. 10:00

// Format the display string for the card header
$display_date = $pre_date ? date('d M Y', strtotime($pre_date)) : 'New Booking';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Book Room</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container mt-4" style="max-width: 600px;">
    
    <div class="card shadow-sm border-primary mb-3">
        <div class="card-header bg-primary text-white">
            <h5 class="mb-0"><i class="fas fa-calendar-plus"></i> Request Booking</h5>
        </div>
        <div class="card-body bg-white">
            <h4 class="card-title text-primary fw-bold mb-1"><?php echo $room['name']; ?></h4>
            <p class="text-muted mb-0">
                <i class="fas fa-map-marker-alt"></i> <?php echo $room['location']; ?> &nbsp;|&nbsp; 
                <i class="fas fa-users"></i> Capacity: <?php echo $room['capacity']; ?>
            </p>
        </div>
    </div>

    <div class="card shadow">
        <div class="card-body">
            <form action="process.php" method="POST">
                <input type="hidden" name="room_id" value="<?php echo $room_id; ?>">

                <div class="mb-3">
                    <label class="form-label fw-bold">Date</label>
                    <input type="date" name="booking_date" class="form-control" 
                           required 
                           min="<?php echo date('Y-m-d'); ?>"
                           value="<?php echo $pre_date; ?>">
                </div>

                <div class="row">
                    <div class="col-6 mb-3">
                        <label class="form-label fw-bold">Start Time</label>
                        <input type="time" name="start_time" class="form-control" 
                               required 
                               value="<?php echo $pre_start; ?>">
                    </div>
                    <div class="col-6 mb-3">
                        <label class="form-label fw-bold">End Time</label>
                        <input type="time" name="end_time" class="form-control" 
                               required 
                               value="<?php echo $pre_end; ?>">
                    </div>
                </div>

                <div class="mb-4">
                    <label class="form-label fw-bold">Purpose / Class</label>
                    <textarea name="purpose" class="form-control" rows="3" 
                              placeholder="e.g. Extra Class 5 Arif, Meeting with PIBG..." required></textarea>
                </div>

                <div class="d-grid gap-2">
                    <button type="submit" name="submit_booking" class="btn btn-success btn-lg shadow-sm">
                        <i class="fas fa-check-circle"></i> Confirm Booking
                    </button>
                    <a href="index.php?date=<?php echo $pre_date ? $pre_date : date('Y-m-d'); ?>" class="btn btn-light border">
                        Cancel
                    </a>
                </div>
            </form>
        </div>
    </div>
</div>

</body>
</html>