<?php
require_once __DIR__ . '/includes/auth.php';
requireLogin();

if (!isset($_GET['id'])) {
    header('Location: book-services.php');
    exit();
}

$service_id = (int) $_GET['id'];
$conn = getDBConnection();
$stmt = $conn->prepare('SELECT * FROM services WHERE id = ? AND active = 1');
$stmt->bind_param('i', $service_id);
$stmt->execute();
$result = $stmt->get_result();
$service = $result->fetch_assoc();
$stmt->close();
$conn->close();

if (!$service) {
    header('Location: book-services.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Book Service - Oldage Home</title>
    <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'>
    <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css'>
    <link href='https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap' rel='stylesheet'>
    <link rel='stylesheet' href='css/style.css'>
</head>
<body>
    <?php include 'includes/header.php'; ?>

    <section class='py-5'>
        <div class='container'>
            <div class='row justify-content-center'>
                <div class='col-lg-8'>
                    <div class='card shadow'>
                        <div class='card-body p-5'>
                            <h2 class='mb-4'>Book <?php echo htmlspecialchars($service['name']); ?></h2>

                            <div class='alert alert-info mb-4'>
                                <h5>Service Details</h5>
                                <p><?php echo htmlspecialchars($service['description']); ?></p>
                                <p><strong>Duration:</strong> <?php echo htmlspecialchars($service['duration']); ?></p>
                                <p><strong>Price:</strong> $<?php echo number_format($service['price'], 2); ?></p>
                            </div>

                            <?php
                            if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                                $booking_date = $_POST['booking_date'];
                                $booking_time = $_POST['booking_time'];
                                $notes = $_POST['notes'];

                                $conn = getDBConnection();
                                $stmt = $conn->prepare("SELECT COUNT(*) AS count FROM bookings WHERE service_id = ? AND booking_date = ? AND booking_time = ? AND status != 'cancelled'");
                                $stmt->bind_param('iss', $service_id, $booking_date, $booking_time);
                                $stmt->execute();
                                $count = $stmt->get_result()->fetch_assoc()['count'];

                                if ($count > 0) {
                                    echo '<div class="alert alert-danger">This time slot is already booked. Please select a different time.</div>';
                                } else {
                                    $stmt = $conn->prepare('INSERT INTO bookings (user_id, service_id, booking_date, booking_time, notes, total_amount) VALUES (?, ?, ?, ?, ?, ?)');
                                    $stmt->bind_param('iisssd', $_SESSION['user_id'], $service_id, $booking_date, $booking_time, $notes, $service['price']);

                                    if ($stmt->execute()) {
                                        $booking_id = $conn->insert_id;
                                        header('Location: payment.php?booking_id=' . $booking_id);
                                        exit();
                                    } else {
                                        echo '<div class="alert alert-danger">Booking failed. Please try again.</div>';
                                    }
                                }

                                $stmt->close();
                                $conn->close();
                            }
                            ?>

                            <form method='post' action='<?php echo htmlspecialchars($_SERVER['PHP_SELF'] . '?id=' . $service_id); ?>'>
                                <div class='row'>
                                    <div class='col-md-6 mb-3'>
                                        <label for='booking_date' class='form-label'>Preferred Date *</label>
                                        <input type='date' class='form-control' id='booking_date' name='booking_date' min='<?php echo date('Y-m-d', strtotime('+1 day')); ?>' required>
                                    </div>
                                    <div class='col-md-6 mb-3'>
                                        <label for='booking_time' class='form-label'>Preferred Time *</label>
                                        <select class='form-control' id='booking_time' name='booking_time' required>
                                            <option value=''>Select Time</option>
                                            <option value='09:00:00'>9:00 AM</option>
                                            <option value='10:00:00'>10:00 AM</option>
                                            <option value='11:00:00'>11:00 AM</option>
                                            <option value='14:00:00'>2:00 PM</option>
                                            <option value='15:00:00'>3:00 PM</option>
                                            <option value='16:00:00'>4:00 PM</option>
                                        </select>
                                    </div>
                                </div>
                                <div class='mb-3'>
                                    <label for='notes' class='form-label'>Special Notes or Requirements</label>
                                    <textarea class='form-control' id='notes' name='notes' rows='3' placeholder='Any special instructions or requirements...'></textarea>
                                </div>
                                <div class='alert alert-warning'>
                                    <strong>Note:</strong> Payment will be processed after confirming your booking details.
                                </div>
                                <div class='d-grid'>
                                    <button type='submit' class='btn btn-primary btn-lg'>Proceed to Payment</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <?php include 'includes/footer.php'; ?>
    <script src='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js'></script>
    <script src='js/main.js'></script>
</body>
</html>

