<?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'];

// Create CME registrations table if not exists
$conn->query("CREATE TABLE IF NOT EXISTS cme_registrations (
    id INT AUTO_INCREMENT PRIMARY KEY,
    doctor_id INT NOT NULL,
    cme_id INT NOT NULL,
    registered_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    attended TINYINT DEFAULT 0,
    certificate_issued TINYINT DEFAULT 0
)");

// Get all CME sessions with registration status
$cme_sessions = $conn->query("
    SELECT cs.*, 
           (SELECT COUNT(*) FROM cme_registrations WHERE cme_id = cs.id) as registrations,
           (SELECT COUNT(*) FROM cme_registrations WHERE cme_id = cs.id AND doctor_id = '$doctor_id') as registered
    FROM cme_sessions cs
    ORDER BY FIELD(cs.status, 'upcoming', 'ongoing', 'completed'), cs.scheduled_date ASC
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CME Sessions | 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">
    <style>
        :root {
            --uhd-green: #0a3d2f;
            --uhd-gold: #d4a017;
            --uhd-dark: #064e3b;
        }
        body { background: #f0f2f5; font-family: 'Segoe UI', sans-serif; }
        
        /* Sidebar */
        .sidebar {
            position: fixed;
            left: 0;
            top: 0;
            width: 280px;
            height: 100%;
            background: linear-gradient(135deg, #0a3d2f, #064e3b);
            color: white;
            z-index: 1000;
            overflow-y: auto;
            transition: 0.3s;
        }
        .sidebar-header {
            padding: 25px;
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.2);
        }
        .sidebar-header h4 { font-size: 1.2rem; margin: 10px 0 5px; }
        .sidebar-header p { font-size: 0.75rem; opacity: 0.8; }
        .sidebar-menu a {
            display: block;
            padding: 12px 20px;
            color: white;
            text-decoration: none;
            transition: 0.2s;
        }
        .sidebar-menu a:hover, .sidebar-menu a.active {
            background: rgba(255,255,255,0.15);
            border-left: 4px solid var(--uhd-gold);
        }
        .sidebar-menu i { width: 28px; margin-right: 10px; }
        
        .main-content {
            margin-left: 280px;
            padding: 25px;
            transition: 0.3s;
        }
        
        /* CME Cards */
        .cme-card {
            background: white;
            border-radius: 20px;
            padding: 0;
            margin-bottom: 25px;
            transition: 0.3s;
            overflow: hidden;
            box-shadow: 0 3px 12px rgba(0,0,0,0.08);
        }
        .cme-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.12);
        }
        .cme-header {
            background: linear-gradient(135deg, var(--uhd-green), var(--uhd-dark));
            padding: 15px 20px;
            color: white;
        }
        .cme-body { padding: 20px; }
        .credit-badge {
            background: var(--uhd-gold);
            color: white;
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 0.75rem;
            font-weight: 600;
        }
        .status-badge {
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 0.7rem;
            font-weight: 600;
        }
        .btn-gold {
            background: var(--uhd-gold);
            color: white;
            border-radius: 30px;
            padding: 8px 20px;
            font-weight: 500;
            border: none;
            transition: 0.2s;
        }
        .btn-gold:hover {
            background: #b8860b;
            color: white;
        }
        .btn-outline-gold {
            background: transparent;
            border: 1px solid var(--uhd-gold);
            color: var(--uhd-gold);
            border-radius: 30px;
            padding: 6px 16px;
        }
        .btn-outline-gold:hover {
            background: var(--uhd-gold);
            color: white;
        }
        .stat-box {
            background: #f8f9fa;
            border-radius: 12px;
            padding: 10px;
            text-align: center;
        }
        .menu-toggle {
            display: none;
            position: fixed;
            top: 15px;
            left: 15px;
            z-index: 1100;
            background: var(--uhd-green);
            color: white;
            border: none;
            padding: 8px 14px;
            border-radius: 8px;
        }
        
        @media (max-width: 768px) {
            .sidebar { transform: translateX(-100%); width: 260px; }
            .sidebar.active { transform: translateX(0); }
            .main-content { margin-left: 0; }
            .menu-toggle { display: block; }
        }
    </style>
</head>
<body>

<button class="menu-toggle" onclick="document.getElementById('sidebar').classList.toggle('active')">
    <i class="fas fa-bars"></i> Menu
</button>

<div class="sidebar" id="sidebar">
    <div class="sidebar-header">
        <i class="fas fa-graduation-cap fa-3x mb-2" style="color: var(--uhd-gold);"></i>
        <h4>Dr. <?= htmlspecialchars($doctor_name) ?></h4>
        <p>UHD CME Portal</p>
    </div>
    <div class="sidebar-menu">
        <a href="dashboard.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a>
        <a href="my_patients.php"><i class="fas fa-users"></i> My Patients</a>
        <a href="consultations.php"><i class="fas fa-video"></i> Video Consult</a>
        <a href="appointments.php"><i class="fas fa-calendar-alt"></i> Appointments</a>
        <a href="doctor_engagement.php"><i class="fas fa-handshake"></i> Engagement Hub</a>
        <a href="cme_sessions.php" class="active"><i class="fas fa-graduation-cap"></i> CME Sessions</a>
        <a href="ai_assistant.php"><i class="fas fa-robot"></i> AI Assistant</a>
        <a href="doctor_payments.php"><i class="fas fa-money-bill-wave"></i> Payments</a>
        <a href="referral.php"><i class="fas fa-share-alt"></i> Refer & Earn</a>
        <a href="profile.php"><i class="fas fa-user-edit"></i> Profile</a>
        <a href="logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a>
    </div>
</div>

<div class="main-content">
    <div class="container-fluid">
        <!-- Header -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2 class="text-success"><i class="fas fa-graduation-cap me-2" style="color: var(--uhd-gold);"></i> CME Sessions</h2>
                <p class="text-muted">Continuing Medical Education for UHD Doctors</p>
            </div>
            <div>
                <span class="badge bg-success p-2"><i class="fas fa-calendar-alt me-2"></i> Upcoming Sessions</span>
            </div>
        </div>

        <!-- CME Grid -->
        <div class="row">
            <?php if ($cme_sessions && $cme_sessions->num_rows > 0): ?>
                <?php while ($cme = $cme_sessions->fetch_assoc()): 
                    $status_color = $cme['status'] == 'upcoming' ? 'success' : ($cme['status'] == 'ongoing' ? 'warning' : 'secondary');
                ?>
                    <div class="col-lg-6 mb-4">
                        <div class="cme-card">
                            <div class="cme-header">
                                <div class="d-flex justify-content-between align-items-center">
                                    <h5 class="mb-0"><?= htmlspecialchars($cme['title']) ?></h5>
                                    <span class="credit-badge"><i class="fas fa-star me-1"></i> <?= $cme['credits'] ?> Credits</span>
                                </div>
                            </div>
                            <div class="cme-body">
                                <div class="row mb-3">
                                    <div class="col-md-6">
                                        <p class="mb-1"><i class="fas fa-calendar-alt me-2" style="color: var(--uhd-gold);"></i> <?= date('l, F j, Y', strtotime($cme['scheduled_date'])) ?></p>
                                        <p class="mb-0"><i class="fas fa-clock me-2" style="color: var(--uhd-gold);"></i> <?= date('h:i A', strtotime($cme['scheduled_time'])) ?></p>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="stat-box">
                                            <small class="text-muted">Registered Doctors</small>
                                            <h6 class="mb-0"><?= $cme['registrations'] ?> participants</h6>
                                        </div>
                                    </div>
                                </div>
                                
                                <p class="text-muted small mb-3">
                                    <?= htmlspecialchars(substr($cme['description'] ?? '', 0, 120)) ?>...
                                </p>
                                
                                <div class="d-flex justify-content-between align-items-center">
                                    <div>
                                        <span class="status-badge bg-<?= $status_color ?>">
                                            <i class="fas fa-<?= $cme['status'] == 'upcoming' ? 'hourglass-half' : ($cme['status'] == 'ongoing' ? 'play' : 'check-circle') ?> me-1"></i>
                                            <?= ucfirst($cme['status']) ?>
                                        </span>
                                        <?php if ($cme['registered'] > 0): ?>
                                            <span class="badge bg-success ms-2"><i class="fas fa-check-circle me-1"></i> Registered</span>
                                        <?php endif; ?>
                                    </div>
                                    <a href="cme_detail.php?id=<?= $cme['id'] ?>" class="btn btn-gold btn-sm">
                                        View Details <i class="fas fa-arrow-right ms-1"></i>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php else: ?>
                <div class="col-12">
                    <div class="alert alert-info text-center py-5">
                        <i class="fas fa-info-circle fa-3x mb-3"></i>
                        <h5>No CME Sessions Available</h5>
                        <p>Check back later for upcoming CME sessions.</p>
                    </div>
                </div>
            <?php endif; ?>
        </div>
        
        <!-- Stats Summary -->
        <div class="row mt-4">
            <div class="col-md-4">
                <div class="stat-box p-3">
                    <i class="fas fa-calendar-check fa-2x mb-2" style="color: var(--uhd-gold);"></i>
                    <h5>Upcoming</h5>
                    <?php
                    $upcoming_count = $conn->query("SELECT COUNT(*) as cnt FROM cme_sessions WHERE status = 'upcoming'")->fetch_assoc()['cnt'];
                    ?>
                    <h3 class="text-success"><?= $upcoming_count ?></h3>
                </div>
            </div>
            <div class="col-md-4">
                <div class="stat-box p-3">
                    <i class="fas fa-certificate fa-2x mb-2" style="color: var(--uhd-gold);"></i>
                    <h5>Your Credits</h5>
                    <?php
                    $my_credits = $conn->query("SELECT SUM(cs.credits) as total FROM cme_registrations cr JOIN cme_sessions cs ON cr.cme_id = cs.id WHERE cr.doctor_id = '$doctor_id' AND cr.attended = 1")->fetch_assoc()['total'] ?? 0;
                    ?>
                    <h3 class="text-success"><?= $my_credits ?></h3>
                </div>
            </div>
            <div class="col-md-4">
                <div class="stat-box p-3">
                    <i class="fas fa-users fa-2x mb-2" style="color: var(--uhd-gold);"></i>
                    <h5>Registered</h5>
                    <?php
                    $my_registrations = $conn->query("SELECT COUNT(*) as cnt FROM cme_registrations WHERE doctor_id = '$doctor_id'")->fetch_assoc()['cnt'];
                    ?>
                    <h3 class="text-success"><?= $my_registrations ?></h3>
                </div>
            </div>
        </div>
        
        <!-- Back Button -->
        <div class="mt-4">
            <a href="doctor_engagement.php" class="btn btn-outline-secondary">
                <i class="fas fa-arrow-left me-2"></i> Back to Engagement Hub
            </a>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>