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

$consultations = $conn->query("
    SELECT c.*, p.first_name, p.last_name, d.name as doctor_name 
    FROM consultations c 
    JOIN patients p ON c.patient_id = p.patient_id 
    JOIN doctors d ON c.doctor_id = d.doctor_id 
    ORDER BY c.scheduled_time DESC
");
?>
<!DOCTYPE html>
<html>
<head><title>Manage Consultations</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"></head>
<body>
<div class="container mt-4">
    <h2>Virtual Consultations</h2>
    <table class="table table-bordered">
        <thead><tr><th>Patient</th><th>Doctor</th><th>Scheduled</th><th>Status</th><th>Meeting Link</th></tr></thead>
        <tbody>
        <?php while($c = $consultations->fetch_assoc()): ?>
        <tr>
            <td><?= $c['first_name'].' '.$c['last_name'] ?></td>
            <td>Dr. <?= $c['doctor_name'] ?></td>
            <td><?= date('M d, Y H:i', strtotime($c['scheduled_time'])) ?></td>
            <td><?= $c['status'] ?></td>
            <td><?php if($c['meeting_link']): ?><a href="<?= $c['meeting_link'] ?>" target="_blank">Join</a><?php endif; ?></td>
        </tr>
        <?php endwhile; ?>
        </tbody>
    </table>
    <a href="dashboard.php" class="btn btn-secondary">Back</a>
</div>
</body>
</html>