/home/awneajlw/public_html/codestechvista.com/appointment.php
<?php
require_once 'config/database.php';
require_once 'includes/auth.php';

$database = new Database();
$db = $database->getConnection();

$error = '';
$success = '';

// Get services for dropdown
$query = "SELECT * FROM services WHERE status = 'active' ORDER BY title";
$stmt = $db->prepare($query);
$stmt->execute();
$services = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Get doctors for dropdown
$query = "SELECT * FROM doctors WHERE status = 'active' ORDER BY name";
$stmt = $db->prepare($query);
$stmt->execute();
$doctors = $stmt->fetchAll(PDO::FETCH_ASSOC);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (!isLoggedIn()) {
        $error = 'Please login to book an appointment.';
    } else {
        $service_id = sanitizeInput($_POST['service_id']);
        $appointment_date = sanitizeInput($_POST['appointment_date']);
        $appointment_time = sanitizeInput($_POST['appointment_time']);
        $notes = sanitizeInput($_POST['notes']);
        
        if (empty($service_id) || empty($appointment_date) || empty($appointment_time)) {
            $error = 'Please fill in all required fields.';
        } else {
            // Check if appointment date is not in the past
            if (strtotime($appointment_date) < strtotime('today')) {
                $error = 'Appointment date cannot be in the past.';
            } else {
                // Check if appointment slot is available
                $query = "SELECT id FROM appointments WHERE appointment_date = ? AND appointment_time = ? AND status != 'cancelled'";
                $stmt = $db->prepare($query);
                $stmt->execute([$appointment_date, $appointment_time]);
                
                if ($stmt->fetch()) {
                    $error = 'This time slot is already booked. Please choose another time.';
                } else {
                    // Book appointment
                    $query = "INSERT INTO appointments (user_id, service_id, appointment_date, appointment_time, notes) VALUES (?, ?, ?, ?, ?)";
                    $stmt = $db->prepare($query);
                    
                    if ($stmt->execute([$_SESSION['user_id'], $service_id, $appointment_date, $appointment_time, $notes])) {
                        $success = 'Appointment booked successfully! We will contact you soon to confirm.';
                        // Clear form data
                        $service_id = $appointment_date = $appointment_time = $notes = '';
                    } else {
                        $error = 'Failed to book appointment. Please try again.';
                    }
                }
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Book Appointment - Opti-Vision Eye Clinic</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">
    <link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm fixed-top">
        <div class="container">
            <a class="navbar-brand fw-bold text-primary" href="index.php">
                <i class="fas fa-eye me-2"></i>Opti-Vision
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="index.php">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="about.php">About</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="services.php">Services</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="doctors.php">Doctors</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="contact.php">Contact</a>
                    </li>
                    <?php if (isLoggedIn()): ?>
                        <li class="nav-item">
                            <a class="nav-link" href="user/dashboard.php">Dashboard</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="logout.php">Logout</a>
                        </li>
                    <?php else: ?>
                        <li class="nav-item">
                            <a class="nav-link" href="login.php">Login</a>
                        </li>
                        <li class="nav-item">
                            <a class="btn btn-primary ms-2" href="register.php">Register</a>
                        </li>
                    <?php endif; ?>
                </ul>
            </div>
        </div>
    </nav>

    <!-- Hero Section -->
    <section class="py-5 mt-5" style="background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-lg-8 text-center text-white">
                    <h1 class="display-5 fw-bold mb-3">Book Your Appointment</h1>
                    <p class="lead">Schedule your eye care consultation with our expert doctors</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Appointment Form -->
    <section class="py-5">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-lg-8">
                    <div class="card shadow-lg border-0">
                        <div class="card-body p-5">
                            <?php if ($error): ?>
                                <div class="alert alert-danger"><?php echo $error; ?></div>
                            <?php endif; ?>
                            
                            <?php if ($success): ?>
                                <div class="alert alert-success"><?php echo $success; ?></div>
                            <?php endif; ?>
                            
                            <?php if (!isLoggedIn()): ?>
                                <div class="alert alert-info">
                                    <i class="fas fa-info-circle me-2"></i>
                                    Please <a href="login.php" class="alert-link">login</a> or <a href="register.php" class="alert-link">register</a> to book an appointment.
                                </div>
                            <?php endif; ?>
                            
                            <form method="POST" <?php echo !isLoggedIn() ? 'onsubmit="return false;"' : ''; ?>>
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label for="service_id" class="form-label">Select Service <span class="text-danger">*</span></label>
                                        <select class="form-select" id="service_id" name="service_id" required <?php echo !isLoggedIn() ? 'disabled' : ''; ?>>
                                            <option value="">Choose a service...</option>
                                            <?php foreach($services as $service): ?>
                                                <option value="<?php echo $service['id']; ?>" 
                                                        <?php echo (isset($_POST['service_id']) && $_POST['service_id'] == $service['id']) ? 'selected' : ''; ?>>
                                                    <?php echo htmlspecialchars($service['title']); ?> - PKR <?php echo number_format($service['price']); ?>
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    
                                    <div class="col-md-6 mb-3">
                                        <label for="appointment_date" class="form-label">Preferred Date <span class="text-danger">*</span></label>
                                        <input type="date" class="form-control" id="appointment_date" name="appointment_date" 
                                               value="<?php echo isset($_POST['appointment_date']) ? htmlspecialchars($_POST['appointment_date']) : ''; ?>" 
                                               min="<?php echo date('Y-m-d'); ?>" required <?php echo !isLoggedIn() ? 'disabled' : ''; ?>>
                                    </div>
                                </div>
                                
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label for="appointment_time" class="form-label">Preferred Time <span class="text-danger">*</span></label>
                                        <select class="form-select" id="appointment_time" name="appointment_time" required <?php echo !isLoggedIn() ? 'disabled' : ''; ?>>
                                            <option value="">Select time...</option>
                                            <option value="09:00" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '09:00') ? 'selected' : ''; ?>>9:00 AM</option>
                                            <option value="09:30" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '09:30') ? 'selected' : ''; ?>>9:30 AM</option>
                                            <option value="10:00" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '10:00') ? 'selected' : ''; ?>>10:00 AM</option>
                                            <option value="10:30" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '10:30') ? 'selected' : ''; ?>>10:30 AM</option>
                                            <option value="11:00" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '11:00') ? 'selected' : ''; ?>>11:00 AM</option>
                                            <option value="11:30" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '11:30') ? 'selected' : ''; ?>>11:30 AM</option>
                                            <option value="12:00" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '12:00') ? 'selected' : ''; ?>>12:00 PM</option>
                                            <option value="12:30" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '12:30') ? 'selected' : ''; ?>>12:30 PM</option>
                                            <option value="14:00" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '14:00') ? 'selected' : ''; ?>>2:00 PM</option>
                                            <option value="14:30" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '14:30') ? 'selected' : ''; ?>>2:30 PM</option>
                                            <option value="15:00" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '15:00') ? 'selected' : ''; ?>>3:00 PM</option>
                                            <option value="15:30" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '15:30') ? 'selected' : ''; ?>>3:30 PM</option>
                                            <option value="16:00" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '16:00') ? 'selected' : ''; ?>>4:00 PM</option>
                                            <option value="16:30" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '16:30') ? 'selected' : ''; ?>>4:30 PM</option>
                                            <option value="17:00" <?php echo (isset($_POST['appointment_time']) && $_POST['appointment_time'] == '17:00') ? 'selected' : ''; ?>>5:00 PM</option>
                                        </select>
                                    </div>
                                    
                                    <div class="col-md-6 mb-3">
                                        <label for="notes" class="form-label">Additional Notes</label>
                                        <textarea class="form-control" id="notes" name="notes" rows="3" 
                                                  placeholder="Any specific concerns or requirements..." <?php echo !isLoggedIn() ? 'disabled' : ''; ?>><?php echo isset($_POST['notes']) ? htmlspecialchars($_POST['notes']) : ''; ?></textarea>
                                    </div>
                                </div>
                                
                                <div class="text-center mt-4">
                                    <?php if (isLoggedIn()): ?>
                                        <button type="submit" class="btn btn-primary btn-lg px-5">
                                            <i class="fas fa-calendar-check me-2"></i>Book Appointment
                                        </button>
                                    <?php else: ?>
                                        <a href="login.php" class="btn btn-primary btn-lg px-5">
                                            <i class="fas fa-sign-in-alt me-2"></i>Login to Book
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Footer -->
    <footer class="bg-dark text-white py-5">
        <div class="container">
            <div class="row g-4">
                <div class="col-lg-4">
                    <h5 class="fw-bold mb-3">
                        <i class="fas fa-eye me-2"></i>Opti-Vision
                    </h5>
                    <p class="text-muted">Professional eye care services with modern technology and experienced specialists.</p>
                </div>
                <div class="col-lg-2">
                    <h6 class="fw-bold mb-3">Quick Links</h6>
                    <ul class="list-unstyled">
                        <li><a href="about.php" class="text-muted text-decoration-none">About Us</a></li>
                        <li><a href="services.php" class="text-muted text-decoration-none">Services</a></li>
                        <li><a href="doctors.php" class="text-muted text-decoration-none">Doctors</a></li>
                        <li><a href="contact.php" class="text-muted text-decoration-none">Contact</a></li>
                    </ul>
                </div>
                <div class="col-lg-3">
                    <h6 class="fw-bold mb-3">Contact Info</h6>
                    <ul class="list-unstyled text-muted">
                        <li><i class="fas fa-map-marker-alt me-2"></i>123 Medical Center, Karachi</li>
                        <li><i class="fas fa-phone me-2"></i>+92 300 1234567</li>
                        <li><i class="fas fa-envelope me-2"></i>info@optivision.com</li>
                        <li><i class="fas fa-clock me-2"></i>Mon-Fri: 9AM-6PM</li>
                    </ul>
                </div>
            </div>
            <hr class="my-4">
            <div class="text-center text-muted">
                <p>&copy; 2024 Opti-Vision Eye Clinic. All rights reserved.</p>
            </div>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>