<?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'];
    $reason = $_POST['reason'];
    $scheduled_time = date('Y-m-d H:i:s', strtotime('+30 minutes'));
    
    $insert = $conn->query("
        INSERT INTO consultations (patient_id, doctor_id, scheduled_time, reason, status) 
        VALUES ('$user_id', '$doctor_id', '$scheduled_time', '$reason', 'scheduled')
    ");
    
    if ($insert) {
        $consult_id = $conn->insert_id;
        $room_name = "UHD_" . $consult_id . "_" . time();
        $meeting_link = "https://meet.jit.si/" . $room_name;
        
        $conn->query("UPDATE consultations SET meeting_link = '$meeting_link', room_name = '$room_name' WHERE id = '$consult_id'");
        
        $success = "Consultation requested! Redirecting to video call...";
        echo "<script>setTimeout(function(){ window.location.href = 'join_consultation.php?room=$room_name'; }, 2000);</script>";
    } else {
        $error = "Error: " . $conn->error;
    }
}

$page_title = 'Request Consultation | 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>Request Consultation</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; }
        .request-card { background: white; border-radius: 24px; padding: 35px; max-width: 600px; margin: 50px auto; }
        .btn-request { background: #d4a017; color: #0a3d2f; border: none; border-radius: 40px; padding: 12px; width: 100%; font-weight: 600; }
        .btn-request:hover { background: #b8860b; color: white; }
    </style>
</head>
<body>
<div class="container">
    <div class="request-card">
        <div class="text-center mb-4">
            <i class="fas fa-video fa-3x" style="color: #d4a017;"></i>
            <h2 class="mt-2">Request Video Consultation</h2>
            <p class="text-muted">Connect with a doctor via secure video call</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="mb-3">
                <label class="form-label">Reason for Consultation</label>
                <textarea name="reason" class="form-control" rows="4" placeholder="Describe your symptoms or reason for consultation..." required></textarea>
            </div>
            
            <div class="alert alert-info small">
                <i class="fas fa-info-circle me-2"></i>
                Your consultation will start immediately after request. A secure video room will be created for you.
            </div>
            
            <button type="submit" class="btn-request">
                <i class="fas fa-calendar-check me-2"></i> Request & Start Consultation
            </button>
        </form>
        
        <div class="text-center mt-3">
            <a href="index.php" class="text-decoration-none">← Back to Medical Hub</a>
        </div>
    </div>
</div>
<?php include '../../include/footer.php'; ?>