<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (!isset($_SESSION['patient_id'])) {
    header("Location: ../login.php");
    exit();
}
include '../../include/config.php';

$user_id = $_SESSION['patient_id'];
$user_name = $_SESSION['patient_name'];
$error = '';
$success = '';

// Get available doctors
$doctors = $conn->query("SELECT doctor_id, name, specialization FROM doctors WHERE status = 'active'");

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $doctor_id = $_POST['doctor_id'];
    $appointment_date = $_POST['appointment_date'];
    $appointment_time = $_POST['appointment_time'];
    $reason = $_POST['reason'];
    
    $insert = $conn->query("
        INSERT INTO appointments (patient_id, doctor_id, appointment_date, appointment_time, reason, status) 
        VALUES ('$user_id', '$doctor_id', '$appointment_date', '$appointment_time', '$reason', 'scheduled')
    ");
    
    if ($insert) {
        $success = "Appointment booked successfully!";
        echo "<script>setTimeout(function(){ window.location.href = 'appointments.php'; }, 2000);</script>";
    } else {
        $error = "Error: " . $conn->error;
    }
}

$page_title = 'Book Appointment | UHD';
include '../../include/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Book Appointment - UHD</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>
        body { background: linear-gradient(135deg, #0a3d2f, #064e3b); font-family: 'Segoe UI', sans-serif; }
        .booking-card { background: white; border-radius: 24px; padding: 35px; max-width: 600px; margin: 50px auto; }
        .btn-book { background: #d4a017; color: #0a3d2f; border: none; border-radius: 40px; padding: 12px; width: 100%; font-weight: 600; }
        .btn-book:hover { background: #b8860b; color: white; }
    </style>
</head>
<body>
<div class="container">
    <div class="booking-card">
        <div class="text-center mb-4">
            <i class="fas fa-calendar-plus fa-3x" style="color: #d4a017;"></i>
            <h2 class="mt-2">Book an Appointment</h2>
            <p class="text-muted">Schedule a visit with a doctor</p>
        </div>
        
        <?php if($error): ?>
            <div class="alert alert-danger"><?= $error ?></div>
        <?php endif; ?>
        <?php if($success): ?>
            <div class="alert alert-success"><?= $success ?></div>
        <?php endif; ?>
        
        <form method="POST">
            <div class="mb-3">
                <label class="form-label">Select Doctor</label>
                <select name="doctor_id" class="form-select" required>
                    <option value="">-- Choose a Doctor --</option>
                    <?php while($doc = $doctors->fetch_assoc()): ?>
                        <option value="<?= $doc['doctor_id'] ?>">Dr. <?= htmlspecialchars($doc['name']) ?> (<?= htmlspecialchars($doc['specialization']) ?>)</option>
                    <?php endwhile; ?>
                </select>
            </div>
            
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Appointment Date</label>
                    <input type="date" name="appointment_date" class="form-control" min="<?= date('Y-m-d') ?>" required>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Appointment Time</label>
                    <select name="appointment_time" class="form-select" required>
                        <option value="">Select Time</option>
                        <option value="09:00:00">09:00 AM</option>
                        <option value="10:00:00">10:00 AM</option>
                        <option value="11:00:00">11:00 AM</option>
                        <option value="12:00:00">12:00 PM</option>
                        <option value="13:00:00">01:00 PM</option>
                        <option value="14:00:00">02:00 PM</option>
                        <option value="15:00:00">03:00 PM</option>
                        <option value="16:00:00">04:00 PM</option>
                    </select>
                </div>
            </div>
            
            <div class="mb-3">
                <label class="form-label">Reason for Visit</label>
                <textarea name="reason" class="form-control" rows="3" placeholder="Briefly describe your symptoms or reason for visit..." required></textarea>
            </div>
            
            <button type="submit" class="btn-book">
                <i class="fas fa-check-circle me-2"></i> Book Appointment
            </button>
        </form>
        
        <div class="text-center mt-3">
            <a href="appointments.php" class="text-decoration-none">← Back to Appointments</a>
        </div>
    </div>
</div>
<?php include '../../include/footer.php'; ?>