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

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

include '../include/config.php';

$doctor_id = $_SESSION['doctor_id'];
$doctor_name = $_SESSION['doctor_name'];
$specialization = $_SESSION['doctor_specialization'];
$uhd_id = $_SESSION['doctor_uhd_id'] ?? '';

// Fetch doctor details safely
$doctor = [];
$docQuery = $conn->query("SELECT * FROM doctors WHERE doctor_id = '$doctor_id'");
if ($docQuery && $docQuery->num_rows > 0) {
    $doctor = $docQuery->fetch_assoc();
}
$specialization = $doctor['specialization'] ?? 'General Medicine';
$consultation_fee = $doctor['consultation_fee'] ?? 5000;
$license_number = $doctor['license_number'] ?? '';

// --- Statistics with error prevention ---
$patient_count = 0;
$res = $conn->query("SELECT COUNT(DISTINCT patient_id) as cnt FROM consultations WHERE doctor_id = '$doctor_id'");
if ($res) { $row = $res->fetch_assoc(); $patient_count = $row['cnt'] ?? 0; }

$today_appointments = 0;
$res = $conn->query("SELECT COUNT(*) as cnt FROM appointments WHERE doctor_id = '$doctor_id' AND appointment_date = CURDATE()");
if ($res) { $row = $res->fetch_assoc(); $today_appointments = $row['cnt'] ?? 0; }

$pending_consults = 0;
$res = $conn->query("SELECT COUNT(*) as cnt FROM consultations WHERE doctor_id = '$doctor_id' AND status = 'scheduled'");
if ($res) { $row = $res->fetch_assoc(); $pending_consults = $row['cnt'] ?? 0; }

$ongoing_consults = 0;
$res = $conn->query("SELECT COUNT(*) as cnt FROM consultations WHERE doctor_id = '$doctor_id' AND status = 'ongoing'");
if ($res) { $row = $res->fetch_assoc(); $ongoing_consults = $row['cnt'] ?? 0; }

$completed_consults = 0;
$res = $conn->query("SELECT COUNT(*) as cnt FROM consultations WHERE doctor_id = '$doctor_id' AND status = 'completed'");
if ($res) { $row = $res->fetch_assoc(); $completed_consults = $row['cnt'] ?? 0; }

// Unread messages
$unread_patient_messages = 0;
$res = $conn->query("SELECT COUNT(*) as cnt FROM patient_doctor_messages WHERE doctor_id = '$doctor_id' AND sender = 'patient' AND is_read = 0");
if ($res) { $row = $res->fetch_assoc(); $unread_patient_messages = $row['cnt'] ?? 0; }

$unread_doctor_messages = 0;
$res = $conn->query("SELECT COUNT(*) as cnt FROM doctor_doctor_chat WHERE receiver_id = '$doctor_id' AND is_read = 0");
if ($res) { $row = $res->fetch_assoc(); $unread_doctor_messages = $row['cnt'] ?? 0; }

$unread_notifications = 0;
$res = $conn->query("SELECT COUNT(*) as cnt FROM doctor_notifications WHERE doctor_id = '$doctor_id' AND is_read = 0");
if ($res) { $row = $res->fetch_assoc(); $unread_notifications = $row['cnt'] ?? 0; }

// --- Fetch consultation lists ---
$pendingList = $conn->query("
    SELECT c.*, CONCAT(p.first_name, ' ', p.last_name) as patient_name, p.patient_id 
    FROM consultations c
    JOIN patients p ON c.patient_id = p.patient_id
    WHERE c.doctor_id = '$doctor_id' AND c.status = 'scheduled'
    ORDER BY c.scheduled_time ASC
");

$ongoingList = $conn->query("
    SELECT c.*, CONCAT(p.first_name, ' ', p.last_name) as patient_name 
    FROM consultations c
    JOIN patients p ON c.patient_id = p.patient_id
    WHERE c.doctor_id = '$doctor_id' AND c.status = 'ongoing'
    ORDER BY c.scheduled_time ASC
");

$completedList = $conn->query("
    SELECT c.*, CONCAT(p.first_name, ' ', p.last_name) as patient_name 
    FROM consultations c
    JOIN patients p ON c.patient_id = p.patient_id
    WHERE c.doctor_id = '$doctor_id' AND c.status = 'completed'
    ORDER BY c.scheduled_time DESC LIMIT 10
");

// --- Handle POST actions ---
$success_msg = '';
$error_msg = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['start_consultation'])) {
        $consult_id = intval($_POST['consult_id']);
        $room_name = "uhd_" . uniqid() . "_" . time();
        $meeting_link = "https://meet.jit.si/" . $room_name;
        $update = $conn->query("UPDATE consultations SET status = 'ongoing', meeting_link = '$meeting_link', started_at = NOW() WHERE id = '$consult_id' AND doctor_id = '$doctor_id'");
        if ($update) {
            echo "<script>window.open('$meeting_link', '_blank'); window.location.href='dashboard.php?success=started';</script>";
            exit();
        } else {
            $error_msg = "Failed to start consultation.";
        }
    }
    if (isset($_POST['cancel_consultation'])) {
        $consult_id = intval($_POST['consult_id']);
        $conn->query("UPDATE consultations SET status = 'cancelled' WHERE id = '$consult_id' AND doctor_id = '$doctor_id'");
        $success_msg = "Consultation cancelled.";
    }
    if (isset($_POST['complete_consultation'])) {
        $consult_id = intval($_POST['consult_id']);
        $notes = $conn->real_escape_string($_POST['notes'] ?? '');
        $prescription = $conn->real_escape_string($_POST['prescription'] ?? '');
        $update = $conn->query("UPDATE consultations SET status = 'completed', notes = '$notes', prescription = '$prescription', completed_at = NOW(), ended_at = NOW() WHERE id = '$consult_id'");
        if ($update) $success_msg = "Consultation completed!";
        else $error_msg = "Error completing consultation.";
    }
}

if (isset($_GET['success']) && $_GET['success'] == 'started') $success_msg = "Consultation started! Video call opened in new tab.";

// Set page title for header
$page_title = 'Doctor Dashboard | UHD';

// Include header from include folder
include '../include/header.php';
?>

<style>
    :root {
        --gold: #d4a017;
        --gold-dark: #b8860b;
        --teal: #0a3d2f;
    }
    
    .notification-bar {
        background: white;
        border-radius: 16px;
        padding: 12px 20px;
        margin-bottom: 25px;
        display: flex;
        gap: 20px;
        flex-wrap: wrap;
        box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        border: 1px solid #e0e0e0;
    }
    .notification-item {
        display: flex;
        align-items: center;
        gap: 10px;
        padding: 8px 16px;
        border-radius: 40px;
        background: #f8f9fa;
        cursor: pointer;
        transition: 0.2s;
        text-decoration: none;
    }
    .notification-item:hover {
        background: #fff3e0;
        transform: translateY(-2px);
    }
    .notification-icon {
        position: relative;
        width: 36px;
        height: 36px;
        background: linear-gradient(135deg, var(--gold), var(--gold-dark));
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .notification-icon i { color: white; font-size: 16px; }
    .notification-badge {
        position: absolute;
        top: -5px;
        right: -5px;
        background: #dc3545;
        color: white;
        border-radius: 50%;
        padding: 2px 6px;
        font-size: 10px;
        font-weight: bold;
    }
    .notification-info h6 { margin: 0; font-size: 13px; font-weight: 600; color: #333; }
    .notification-info p { margin: 0; font-size: 11px; color: #6c757d; }
    
    .stat-card {
        background: linear-gradient(135deg, var(--gold), var(--gold-dark));
        border-radius: 20px;
        padding: 20px;
        text-align: center;
        color: white;
        cursor: pointer;
        transition: transform 0.2s;
        box-shadow: 0 8px 16px rgba(0,0,0,0.1);
    }
    .stat-card:hover { transform: translateY(-5px); }
    .stat-card i { font-size: 2.2rem; }
    .stat-card h3 { font-size: 2rem; margin: 12px 0; font-weight: 700; }
    
    .feature-card {
        background: white;
        border-radius: 18px;
        padding: 18px 8px;
        text-align: center;
        cursor: pointer;
        transition: 0.2s;
        border: 1px solid #e0e0e0;
        height: 100%;
    }
    .feature-card:hover {
        transform: translateY(-5px);
        background: var(--gold);
        color: white;
    }
    .feature-card i { font-size: 2rem; color: var(--gold); margin-bottom: 10px; }
    .feature-card:hover i { color: white; }
    .feature-card h6 { font-size: 0.9rem; font-weight: 600; margin-top: 8px; }
    
    .consultation-card {
        background: white;
        border-left: 5px solid;
        border-radius: 14px;
        padding: 15px 18px;
        margin-bottom: 15px;
    }
    .pending { border-left-color: #ffc107; background: #fffbf0; }
    .ongoing { border-left-color: #dc3545; background: #fff5f5; animation: pulse 1.5s infinite; }
    
    @keyframes pulse {
        0% { box-shadow: 0 0 0 0 rgba(220,53,69,0.2); }
        70% { box-shadow: 0 0 0 8px rgba(220,53,69,0); }
        100% { box-shadow: 0 0 0 0 rgba(220,53,69,0); }
    }
    
    .shiny-card {
        background: white;
        border-radius: 24px;
        box-shadow: 0 8px 24px rgba(0,0,0,0.08);
        overflow: hidden;
        margin-bottom: 28px;
    }
    .shiny-card .card-header {
        background: linear-gradient(135deg, var(--teal), #064e3b);
        color: white;
        padding: 16px 24px;
        font-weight: 600;
        font-size: 1.1rem;
    }
    .btn-gold {
        background: var(--gold);
        color: white;
        border-radius: 40px;
        padding: 6px 18px;
        font-weight: 500;
        border: none;
    }
    .btn-gold:hover { background: var(--gold-dark); transform: scale(1.02); }
    .badge-unread {
        background: #dc3545;
        color: white;
        border-radius: 30px;
        padding: 2px 8px;
        font-size: 0.7rem;
        margin-left: 8px;
    }
    .text-gold { color: var(--gold); }
    .profile-banner {
        background: white;
        border-radius: 24px;
        padding: 20px;
        margin-bottom: 20px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.05);
        border: 1px solid #e0e0e0;
    }
    .referral-banner {
        background: linear-gradient(135deg, var(--gold), var(--gold-dark));
        border-radius: 24px;
        padding: 20px;
        text-align: center;
        color: white;
        margin-bottom: 28px;
    }
    .floating-ai-btn {
        position: fixed;
        bottom: 30px;
        right: 30px;
        width: 60px;
        height: 60px;
        background: linear-gradient(135deg, #d4a017, #b8860b);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        z-index: 999;
        box-shadow: 0 4px 15px rgba(0,0,0,0.2);
        animation: pulse 2s infinite;
    }
    .floating-ai-btn:hover { transform: scale(1.1); }
    .floating-ai-btn i { font-size: 28px; color: white; }
    
    @media (max-width: 768px) {
        .stat-card h3 { font-size: 1.5rem; }
        .feature-card { margin-bottom: 15px; }
        .consultation-card .row > div { margin-bottom: 8px; text-align: center; }
        .notification-bar { justify-content: center; }
        .floating-ai-btn { width: 50px; height: 50px; bottom: 20px; right: 20px; }
        .floating-ai-btn i { font-size: 24px; }
    }
</style>

<div class="container mt-4">
    <!-- Alerts -->
    <?php if ($success_msg): ?>
        <div class="alert alert-success alert-dismissible fade show"><i class="fas fa-check-circle me-2"></i> <?= htmlspecialchars($success_msg) ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
    <?php endif; ?>
    <?php if ($error_msg): ?>
        <div class="alert alert-danger alert-dismissible fade show"><i class="fas fa-exclamation-triangle me-2"></i> <?= htmlspecialchars($error_msg) ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
    <?php endif; ?>

    <!-- Referral Banner -->
    <div class="referral-banner">
        <i class="fas fa-gift fa-3x mb-2"></i>
        <h4>Refer a Colleague & Earn ₦10,000!</h4>
        <p>Share UHD with fellow doctors and earn a bonus when they complete 10 consultations.</p>
        <button class="btn btn-light mt-2" onclick="location.href='referral.php'"><i class="fas fa-share-alt"></i> Refer Now</button>
    </div>

    <!-- Doctor Profile Card -->
    <div class="profile-banner">
        <div class="row align-items-center">
            <div class="col-md-8">
                <h3><i class="fas fa-user-md me-2" style="color: var(--gold);"></i> Dr. <?= htmlspecialchars($doctor_name) ?></h3>
                <p><i class="fas fa-stethoscope me-2 text-gold"></i> <?= htmlspecialchars($specialization) ?> | <i class="fas fa-id-card me-2 text-gold"></i> UHD ID: <?= htmlspecialchars($uhd_id ?: 'Pending') ?></p>
                <p><i class="fas fa-money-bill-wave me-2 text-gold"></i> Consultation Fee: ₦<?= number_format($consultation_fee, 2) ?></p>
            </div>
            <div class="col-md-4 text-md-end">
                <a href="profile.php" class="btn btn-gold"><i class="fas fa-user-edit"></i> Edit Profile</a>
            </div>
        </div>
    </div>

    <!-- Notification Bar -->
    <div class="notification-bar">
        <a href="patient_messages.php" class="notification-item">
            <div class="notification-icon"><i class="fas fa-comments"></i><?php if($unread_patient_messages > 0): ?><span class="notification-badge"><?= $unread_patient_messages ?></span><?php endif; ?></div>
            <div class="notification-info"><h6>Patient Messages</h6><p><?= $unread_patient_messages ?> unread</p></div>
        </a>
        <a href="doctor_chat.php" class="notification-item">
            <div class="notification-icon"><i class="fas fa-user-md"></i><?php if($unread_doctor_messages > 0): ?><span class="notification-badge"><?= $unread_doctor_messages ?></span><?php endif; ?></div>
            <div class="notification-info"><h6>Doctor Chat</h6><p><?= $unread_doctor_messages ?> unread</p></div>
        </a>
        <a href="notifications.php" class="notification-item">
            <div class="notification-icon"><i class="fas fa-bell"></i><?php if($unread_notifications > 0): ?><span class="notification-badge"><?= $unread_notifications ?></span><?php endif; ?></div>
            <div class="notification-info"><h6>Notifications</h6><p><?= $unread_notifications ?> new</p></div>
        </a>
    </div>

    <!-- Stats Row -->
    <div class="row g-3 mb-4">
        <div class="col-md-3 col-6"><div class="stat-card" onclick="location.href='my_patients.php'"><i class="fas fa-users"></i><h3><?= $patient_count ?></h3><p>My Patients</p></div></div>
        <div class="col-md-3 col-6"><div class="stat-card" onclick="location.href='appointments.php'"><i class="fas fa-calendar-day"></i><h3><?= $today_appointments ?></h3><p>Today's Appointments</p></div></div>
        <div class="col-md-3 col-6"><div class="stat-card" onclick="location.href='consultations.php?status=pending'"><i class="fas fa-hourglass-half"></i><h3><?= $pending_consults ?></h3><p>Pending</p></div></div>
        <div class="col-md-3 col-6"><div class="stat-card" onclick="location.href='consultations.php?status=completed'"><i class="fas fa-check-circle"></i><h3><?= $completed_consults ?></h3><p>Completed</p></div></div>
    </div>

    <!-- Quick Actions -->
    <div class="row g-3 mb-5">
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='my_patients.php'"><i class="fas fa-users"></i><h6>My Patients</h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='patient_messages.php'"><i class="fas fa-comments"></i><h6>Patient Messages <?= $unread_patient_messages ? "<span class='badge-unread'>$unread_patient_messages</span>" : '' ?></h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='doctor_chat.php'"><i class="fas fa-user-md"></i><h6>Doctor Chat <?= $unread_doctor_messages ? "<span class='badge-unread'>$unread_doctor_messages</span>" : '' ?></h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='add_prescription.php'"><i class="fas fa-prescription-bottle"></i><h6>Prescribe</h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='add_vital.php'"><i class="fas fa-heartbeat"></i><h6>Add Vitals</h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='nurse_chat.php'"><i class="fas fa-user-nurse"></i><h6>Nurse Chat</h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='staff_chat.php'"><i class="fas fa-user-tie"></i><h6>Staff Chat</h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='ai_assistant.php'"><i class="fas fa-robot"></i><h6>AI Assistant</h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='doctor_engagement.php'"><i class="fas fa-handshake"></i><h6>Engagement Hub</h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='doctor_payments.php'"><i class="fas fa-money-bill-wave"></i><h6>Payments</h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='consultations.php'"><i class="fas fa-video"></i><h6>Video Consult</h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='referral.php'"><i class="fas fa-share-alt"></i><h6>Refer & Earn</h6></div></div>
        <div class="col-md-3 col-6"><div class="feature-card" onclick="location.href='notifications.php'"><i class="fas fa-bell"></i><h6>Notifications <?= $unread_notifications ? "<span class='badge-unread'>$unread_notifications</span>" : '' ?></h6></div></div>
    </div>

    <!-- Pending Consultations -->
    <div class="shiny-card">
        <div class="card-header"><i class="fas fa-clock me-2"></i> Pending Consultation Requests <span class="badge bg-light text-dark ms-2"><?= $pending_consults ?></span></div>
        <div class="card-body">
            <?php if ($pendingList && $pendingList->num_rows > 0): ?>
                <?php while ($c = $pendingList->fetch_assoc()): ?>
                    <div class="consultation-card pending">
                        <div class="row align-items-center">
                            <div class="col-md-4"><strong><i class="fas fa-user me-2"></i><?= htmlspecialchars($c['patient_name']) ?></strong><br><small class="text-muted">ID: <?= substr($c['patient_id'], 0, 8) ?>...</small></div>
                            <div class="col-md-3"><i class="fas fa-calendar-alt me-1"></i> <?= date('M d, Y', strtotime($c['scheduled_time'])) ?><br><i class="fas fa-clock me-1"></i> <?= date('h:i A', strtotime($c['scheduled_time'])) ?></div>
                            <div class="col-md-3"><small class="text-muted"><?= htmlspecialchars(substr($c['reason'] ?? '', 0, 60)) ?>...</small></div>
                            <div class="col-md-2 text-end">
                                <form method="POST" class="d-inline"><input type="hidden" name="consult_id" value="<?= $c['id'] ?>"><button type="submit" name="start_consultation" class="btn btn-gold btn-sm"><i class="fas fa-video"></i> Start</button></form>
                                <form method="POST" class="d-inline ms-1" onsubmit="return confirm('Cancel this consultation?')"><input type="hidden" name="consult_id" value="<?= $c['id'] ?>"><button type="submit" name="cancel_consultation" class="btn btn-secondary btn-sm"><i class="fas fa-times"></i> Cancel</button></form>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php else: ?>
                <p class="text-muted text-center mb-0">No pending consultations.</p>
            <?php endif; ?>
        </div>
    </div>

    <!-- Ongoing Consultations -->
    <?php if ($ongoingList && $ongoingList->num_rows > 0): ?>
    <div class="shiny-card">
        <div class="card-header bg-danger text-white"><i class="fas fa-video me-2"></i> Ongoing Consultations <span class="badge bg-light text-dark ms-2"><?= $ongoing_consults ?></span></div>
        <div class="card-body">
            <?php while ($c = $ongoingList->fetch_assoc()): ?>
                <div class="consultation-card ongoing">
                    <div class="row align-items-center">
                        <div class="col-md-4"><strong><i class="fas fa-user me-2"></i><?= htmlspecialchars($c['patient_name']) ?></strong></div>
                        <div class="col-md-4"><i class="fas fa-calendar-alt me-1"></i> <?= date('M d, Y h:i A', strtotime($c['scheduled_time'])) ?></div>
                        <div class="col-md-4 text-end">
                            <?php if ($c['meeting_link']): ?><a href="<?= $c['meeting_link'] ?>" target="_blank" class="btn btn-danger btn-sm me-2"><i class="fas fa-video"></i> Join Call</a><?php endif; ?>
                            <button class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#completeModal<?= $c['id'] ?>"><i class="fas fa-check"></i> Complete</button>
                        </div>
                    </div>
                </div>

                <!-- Complete Modal -->
                <div class="modal fade" id="completeModal<?= $c['id'] ?>" tabindex="-1">
                    <div class="modal-dialog modal-dialog-centered">
                        <div class="modal-content">
                            <div class="modal-header bg-success text-white">
                                <h5><i class="fas fa-notes-medical me-2"></i> Complete Consultation</h5>
                                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                            </div>
                            <form method="POST">
                                <div class="modal-body">
                                    <p><strong>Patient:</strong> <?= htmlspecialchars($c['patient_name']) ?></p>
                                    <div class="mb-3"><label class="form-label fw-bold">Consultation Notes</label><textarea name="notes" class="form-control" rows="3" placeholder="Diagnosis, observations, recommendations..." required></textarea></div>
                                    <div class="mb-3"><label class="form-label fw-bold">Prescription</label><textarea name="prescription" class="form-control" rows="2" placeholder="Medication, dosage, duration..."></textarea></div>
                                    <input type="hidden" name="consult_id" value="<?= $c['id'] ?>">
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                                    <button type="submit" name="complete_consultation" class="btn btn-success">Save & Complete</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Recent Completed Consultations -->
    <div class="shiny-card">
        <div class="card-header"><i class="fas fa-history me-2"></i> Recent Completed Consultations</div>
        <div class="card-body">
            <?php if ($completedList && $completedList->num_rows > 0): ?>
                <div class="table-responsive">
                    <table class="table table-hover align-middle">
                        <thead class="table-light"><tr><th>Patient</th><th>Date</th><th>Notes</th><th>Action</th></tr></thead>
                        <tbody>
                        <?php while ($c = $completedList->fetch_assoc()): ?>
                            <tr>
                                <td><strong><?= htmlspecialchars($c['patient_name']) ?></strong></td>
                                <td><?= date('M d, Y', strtotime($c['scheduled_time'])) ?></td>
                                <td><?= htmlspecialchars(substr($c['notes'] ?? '', 0, 40)) ?>...</td>
                                <td><button class="btn btn-sm btn-info text-white" onclick="viewDetails('<?= $c['id'] ?>')"><i class="fas fa-eye"></i> View</button>
                                    <button class="btn btn-sm btn-secondary" onclick="printSummary('<?= $c['id'] ?>')"><i class="fas fa-print"></i> Print</button>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            <?php else: ?>
                <p class="text-muted text-center mb-0">No completed consultations yet.</p>
            <?php endif; ?>
        </div>
    </div>

    <!-- Earnings Summary -->
    <div class="shiny-card">
        <div class="card-header"><i class="fas fa-chart-line me-2"></i> Earnings Summary (This Month)</div>
        <div class="card-body text-center">
            <h2 class="text-gold display-5 fw-bold">₦<?= number_format($completed_consults * $consultation_fee, 2) ?></h2>
            <p class="text-muted">From <?= $completed_consults ?> completed consultations</p>
            <a href="doctor_payments.php" class="btn btn-gold"><i class="fas fa-arrow-right"></i> View Payment Details</a>
        </div>
    </div>
</div>

<!-- Floating AI Button -->
<div class="floating-ai-btn" onclick="location.href='ai_assistant.php'">
    <i class="fas fa-robot"></i>
</div>

<script>
function viewDetails(id) { window.location.href = 'consultation_details.php?id=' + id; }
function printSummary(id) { window.open('print_summary.php?id=' + id, '_blank'); }
setTimeout(() => {
    document.querySelectorAll('.alert').forEach(alert => {
        alert.style.transition = 'opacity 0.5s';
        alert.style.opacity = '0';
        setTimeout(() => alert.remove(), 500);
    });
}, 6000);
</script>

<?php
// Include footer from include folder
include '../include/footer.php';
?>