<?php 
include 'includes/header.php'; 
include 'includes/sidebar.php'; 

$teacher_id = $_SESSION['teacher_id'];

// Fetch Teacher's Classes
try {
    $stmt = $pdo->prepare("SELECT l.*, c.title as course_name FROM live_classes l 
                           JOIN courses c ON l.course_id = c.id 
                           WHERE l.teacher_id = ? ORDER BY l.class_date DESC, l.class_time DESC");
    $stmt->execute([$teacher_id]);
    $classes = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) { $classes = []; }
?>

<style>
    .dashboard-header { background: #fff; padding: 20px 30px; border-radius: 15px; box-shadow: 0 5px 20px rgba(0,0,0,0.03); margin-bottom: 25px; border-left: 6px solid var(--theme-green); }
    .custom-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 18px; box-shadow: 0 10px 40px rgba(0,0,0,0.03); overflow: hidden; border-top: 5px solid var(--theme-green); }
    
    .table thead th { border: none; padding: 18px 20px; color: #64748b; font-weight: 800; text-transform: uppercase; font-size: 11px; letter-spacing: 0.5px; background-color: var(--bg-soft); border-bottom: 1px solid #e2e8f0; }
    .table tbody tr:hover { background-color: rgba(6, 78, 59, 0.02); }
    
    .btn-theme { background: var(--theme-green); color: #fff; border: none; transition: 0.3s; padding: 10px 25px; border-radius: 50px; font-weight: 700; text-decoration: none; display: inline-flex; align-items: center; }
    .btn-theme:hover { background: var(--theme-dark); color: var(--theme-gold); transform: translateY(-2px); }
    
    .status-pill { padding: 5px 12px; border-radius: 50px; font-size: 10px; font-weight: 800; text-transform: uppercase; }
    .pill-active { background: #ecfdf5; color: #059669; border: 1px solid #d1fae5; }
    
    .btn-join { background: #fff; color: var(--theme-green); border: 1px solid var(--theme-green); font-weight: 700; font-size: 12px; padding: 5px 15px; border-radius: 50px; transition: 0.3s; }
    .btn-join:hover { background: var(--theme-green); color: #fff; }

    #sidebarToggle { width: 42px !important; height: 42px !important; padding: 0; display: inline-flex !important; align-items: center; justify-content: center; flex-shrink: 0; border-radius: 8px; border: 1px solid #e2e8f0; background: #fff; }

    @media (max-width: 991px) {
        .dashboard-header { flex-direction: column; align-items: flex-start !important; gap: 15px; padding: 15px 20px; }
        .dashboard-header .d-flex { width: 100%; align-items: center; }
        .dashboard-header .btn-theme { width: 100%; justify-content: center; }
    }
</style>

<div class="main-content">
    <div class="dashboard-header d-flex justify-content-between align-items-center fade-up">
        <div class="d-flex align-items-center">
            <button class="btn btn-light border d-lg-none me-3 shadow-sm" id="sidebarToggle"><i class="fas fa-bars text-dark"></i></button>
            <h4 class="mb-0 fw-bold text-dark"><i class="fas fa-video me-2" style="color: var(--theme-green);"></i> Manage Live Classes</h4>
        </div>
        <a href="add_class.php" class="btn-theme shadow-sm"><i class="fas fa-plus-circle me-2"></i> Schedule New Class</a>
    </div>

    <div class="custom-card fade-up" style="animation-delay: 0.1s;">
        <div class="table-responsive">
            <table class="table align-middle border-0 mb-0">
                <thead>
                    <tr>
                        <th class="ps-4">Class Details</th>
                        <th>Course</th>
                        <th>Schedule</th>
                        <th>Status</th>
                        <th class="text-end pe-4">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($classes)): foreach($classes as $row): ?>
                    <tr>
                        <td class="ps-4 py-3">
                            <div class="fw-bold text-dark" style="font-size: 15px;"><?= htmlspecialchars($row['class_title']) ?></div>
                            <small class="text-muted fw-bold">Topic: <?= htmlspecialchars($row['topic'] ?? 'General') ?></small>
                        </td>
                        <td>
                            <span class="badge bg-light text-muted border px-3 py-2 rounded-pill fw-bold" style="font-size: 11px;">
                                <i class="fas fa-book me-1" style="color: var(--theme-gold);"></i> <?= htmlspecialchars($row['course_name']) ?>
                            </span>
                        </td>
                        <td>
                            <div class="small fw-bold text-dark"><i class="far fa-calendar-alt me-1 text-success"></i> <?= date('d M, Y', strtotime($row['class_date'])) ?></div>
                            <div class="small text-muted fw-bold mt-1"><i class="far fa-clock me-1 text-warning"></i> <?= date('h:i A', strtotime($row['class_time'])) ?></div>
                        </td>
                        <td><span class="status-pill pill-active">Scheduled</span></td>
                        <td class="text-end pe-4">
                            <div class="d-flex justify-content-end gap-2">
                                <a href="<?= $row['meeting_link'] ?>" target="_blank" class="btn-join"><i class="fas fa-external-link-alt me-1"></i> Start</a>
                                <a href="edit_class.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-light border shadow-sm"><i class="fas fa-pen text-warning"></i></a>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; else: ?>
                    <tr><td colspan="5" class="text-center py-5 fw-bold text-muted">No classes found. Start by scheduling one!</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>