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

$consult_id = (int)$_GET['id'];
$doctor_id = $_SESSION['doctor_id'];

$consult = $conn->query("SELECT c.*, p.first_name, p.last_name, p.email, p.phone 
                         FROM consultations c 
                         JOIN patients p ON c.patient_id = p.patient_id 
                         WHERE c.id = $consult_id AND c.doctor_id = $doctor_id")->fetch_assoc();

if (!$consult) {
    die("Consultation not found");
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Consultation Summary - 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">
</head>
<body>
<div class="container py-4">
    <div class="card shadow">
        <div class="card-header bg-success text-white">
            <h4 class="mb-0"><i class="fas fa-chart-line me-2"></i> Consultation Summary</h4>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <h5>Patient Information</h5>
                    <p><strong>Name:</strong> <?= htmlspecialchars($consult['first_name'] . ' ' . $consult['last_name']) ?></p>
                    <p><strong>Email:</strong> <?= htmlspecialchars($consult['email']) ?></p>
                    <p><strong>Phone:</strong> <?= htmlspecialchars($consult['phone'] ?? 'N/A') ?></p>
                </div>
                <div class="col-md-6">
                    <h5>Consultation Details</h5>
                    <p><strong>Scheduled:</strong> <?= date('M d, Y h:i A', strtotime($consult['scheduled_time'])) ?></p>
                    <p><strong>Status:</strong> <span class="badge bg-success"><?= ucfirst($consult['status']) ?></span></p>
                    <?php if ($consult['completed_at']): ?>
                        <p><strong>Completed:</strong> <?= date('M d, Y h:i A', strtotime($consult['completed_at'])) ?></p>
                    <?php endif; ?>
                </div>
            </div>
            
            <hr>
            
            <h5>Reason for Consultation</h5>
            <p><?= nl2br(htmlspecialchars($consult['reason'] ?? 'Not specified')) ?></p>
            
            <h5>Doctor's Notes</h5>
            <form method="POST" action="save_consultation_notes.php">
                <input type="hidden" name="consult_id" value="<?= $consult_id ?>">
                <textarea name="notes" class="form-control" rows="5" placeholder="Add consultation notes, diagnosis, recommendations..."><?= htmlspecialchars($consult['notes'] ?? '') ?></textarea>
                <div class="mt-3">
                    <button type="submit" class="btn btn-primary">Save Notes</button>
                    <a href="consultations.php" class="btn btn-outline-secondary">Back</a>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>