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

$consult_id = $_GET['id'] ?? 0;
$user_id = $_SESSION['patient_id'];

$consult = $conn->query("
    SELECT c.*, d.name as doctor_name, d.specialization 
    FROM consultations c
    JOIN doctors d ON c.doctor_id = d.doctor_id
    WHERE c.id = '$consult_id' AND c.patient_id = '$user_id'
")->fetch_assoc();

$page_title = 'Consultation Details | 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>Consultation Details</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">
</head>
<body>
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="text-white"><i class="fas fa-file-alt me-2"></i> Consultation Details</h2>
        <a href="index.php" class="btn btn-outline-light rounded-pill">← Back</a>
    </div>

    <?php if($consult): ?>
        <div class="bg-white rounded-4 p-4">
            <div class="d-flex justify-content-between">
                <h4>Dr. <?= htmlspecialchars($consult['doctor_name']) ?></h4>
                <span class="badge bg-<?= $consult['status'] == 'completed' ? 'success' : 'warning' ?> fs-6"><?= ucfirst($consult['status']) ?></span>
            </div>
            <p class="text-muted"><?= htmlspecialchars($consult['specialization']) ?></p>
            <hr>
            <div class="row">
                <div class="col-md-6">
                    <p><strong><i class="far fa-calendar-alt me-2"></i> Date:</strong> <?= date('F j, Y', strtotime($consult['scheduled_time'])) ?></p>
                    <p><strong><i class="far fa-clock me-2"></i> Time:</strong> <?= date('h:i A', strtotime($consult['scheduled_time'])) ?></p>
                </div>
                <div class="col-md-6">
                    <p><strong><i class="fas fa-stethoscope me-2"></i> Reason:</strong> <?= nl2br(htmlspecialchars($consult['reason'] ?? 'Not specified')) ?></p>
                </div>
            </div>
            <?php if($consult['notes']): ?>
                <div class="mt-3">
                    <strong><i class="fas fa-notes-medical me-2"></i> Doctor's Notes:</strong>
                    <p class="mt-2 p-3 bg-light rounded"><?= nl2br(htmlspecialchars($consult['notes'])) ?></p>
                </div>
            <?php endif; ?>
            <?php if($consult['prescription']): ?>
                <div class="mt-3">
                    <strong><i class="fas fa-prescription-bottle me-2"></i> Prescription:</strong>
                    <p class="mt-2 p-3 bg-light rounded"><?= nl2br(htmlspecialchars($consult['prescription'])) ?></p>
                </div>
            <?php endif; ?>
        </div>
    <?php else: ?>
        <div class="alert alert-danger">Consultation not found</div>
    <?php endif; ?>
</div>
<?php include '../../include/footer.php'; ?>