<?php
session_start();
include 'layouts/main.php';
include 'layouts/dbconn.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Fetch departments/services
$dept_sql = "SELECT * FROM departments ORDER BY department_name ASC";
$dept_result = $conn->query($dept_sql);

// Handle form submission
$success_msg = "";
$error_msg = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Basic validation and insertion logic
    $department_id = $_POST['department_id'];
    $doctor_id = $_POST['doctor_id'];
    $appointment_date = $_POST['appointment_date'];
    $appointment_time = $_POST['appointment_time'];
    $reason = $_POST['reason'];

    // In a real scenario, we'd lookup the patient_id associated with the logged in user_id
    // For now, assuming session has patient_id or we query it. 
    // Let's assume we find patient_id from users table or session.
    // $patient_id = $_SESSION['patient_id']; // This would be ideal

    // Placeholder success
    $success_msg = "Appointment request submitted successfully. We will confirm shortly.";
}
?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Book Appointment')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/flatpickr/flatpickr.min.css" rel="stylesheet">
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Page Title -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Book Appointment</h4>
                                <div class="page-title-right">
                                    <ol class="breadcrumb m-0">
                                        <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                                        <li class="breadcrumb-item active">Book Appointment</li>
                                    </ol>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row justify-content-center">
                        <div class="col-lg-8">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">New Appointment Request</h5>
                                </div>
                                <div class="card-body">
                                    <?php if ($success_msg): ?>
                                        <div class="alert alert-success"><?php echo $success_msg; ?></div>
                                    <?php endif; ?>

                                    <form method="POST">
                                        <div class="row mb-3">
                                            <div class="col-md-6">
                                                <label class="form-label">Department / Specialty</label>
                                                <select class="form-select" name="department_id" required>
                                                    <option value="">Select Department</option>
                                                    <?php if ($dept_result->num_rows > 0): ?>
                                                        <?php while ($dept = $dept_result->fetch_assoc()): ?>
                                                            <option value="<?php echo $dept['department_id']; ?>">
                                                                <?php echo htmlspecialchars($dept['department_name']); ?>
                                                            </option>
                                                        <?php endwhile; ?>
                                                    <?php else: ?>
                                                        <option value="1">General ENT</option>
                                                        <option value="2">Pediatric ENT</option>
                                                    <?php endif; ?>
                                                </select>
                                            </div>
                                            <div class="col-md-6">
                                                <label class="form-label">Doctor (Optional)</label>
                                                <select class="form-select" name="doctor_id">
                                                    <option value="">Any Available Doctor</option>
                                                    <!-- Populate via AJAX based on department usually -->
                                                </select>
                                            </div>
                                        </div>

                                        <div class="row mb-3">
                                            <div class="col-md-6">
                                                <label class="form-label">Preferred Date</label>
                                                <input type="text" class="form-control" id="date-picker"
                                                    name="appointment_date" required>
                                            </div>
                                            <div class="col-md-6">
                                                <label class="form-label">Preferred Time</label>
                                                <select class="form-select" name="appointment_time" required>
                                                    <option value="">Select Time</option>
                                                    <option value="09:00">09:00 AM</option>
                                                    <option value="10:00">10:00 AM</option>
                                                    <option value="11:00">11:00 AM</option>
                                                    <option value="14:00">02:00 PM</option>
                                                    <option value="15:00">03:00 PM</option>
                                                    <option value="16:00">04:00 PM</option>
                                                </select>
                                            </div>
                                        </div>

                                        <div class="mb-3">
                                            <label class="form-label">Reason for Visit</label>
                                            <textarea class="form-control" name="reason" rows="3"
                                                placeholder="Briefly describe your symptoms or reason for visit"></textarea>
                                        </div>

                                        <div class="text-end">
                                            <button type="submit" class="btn btn-primary">Book Appointment</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/flatpickr/flatpickr.min.js"></script>
    <script>
        flatpickr("#date-picker", {
            minDate: "today",
            dateFormat: "Y-m-d"
        });
    </script>
</body>

</html>