<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($assignment->subject_name) ?> - RUSO LMS</title>
    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <!-- Tailwind CSS CDN -->
    <script src="https://cdn.tailwindcss.com"></script>
    
    <!-- Quill Editor CDN -->
    <link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
    <script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>

    <script>
        tailwind.config = {
            theme: {
                extend: { fontFamily: { sans: ['Inter', 'sans-serif'] }, colors: { primary: '#1a56db' } }
            }
        }
        
        function switchTab(tabId) {
            document.querySelectorAll('.tab-content').forEach(el => el.classList.add('hidden'));
            document.querySelectorAll('.tab-btn').forEach(el => {
                el.classList.remove('border-blue-600', 'text-blue-600');
                el.classList.add('border-transparent', 'text-gray-500');
            });
            document.getElementById(tabId).classList.remove('hidden');
            event.currentTarget.classList.remove('border-transparent', 'text-gray-500');
            event.currentTarget.classList.add('border-blue-600', 'text-blue-600');
        }
        
        function toggleModal(modalId) {
            const modal = document.getElementById(modalId);
            modal.classList.toggle('hidden');
        }
    </script>
</head>
<body class="bg-gray-50 text-gray-900 font-sans antialiased flex">

    <!-- ================= DESKTOP SIDEBAR ================= -->
    <aside class="hidden md:flex flex-col w-64 bg-gray-900 text-white min-h-screen fixed left-0 top-0 z-50">
        <div class="p-6 border-b border-gray-800">
            <img src="<?= base_url('assets/images/logo.png') ?>" alt="CSO Logo" class="h-12 w-auto bg-white rounded p-1">
            <div class="text-xs text-gray-400 mt-1 uppercase tracking-wider font-semibold">Teacher Portal</div>
        </div>
        
        <nav class="flex-1 px-4 py-6 space-y-2">
            <a href="<?= site_url('teacher/dashboard') ?>" class="flex items-center gap-3 px-4 py-3 text-gray-400 hover:bg-gray-800 hover:text-white rounded-lg transition-colors">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><rect x="3" y="3" width="7" height="7"></rect><rect x="14" y="3" width="7" height="7"></rect><rect x="14" y="14" width="7" height="7"></rect><rect x="3" y="14" width="7" height="7"></rect></svg>
                <span class="font-medium">Dashboard</span>
            </a>
            
            <a href="#" class="flex items-center gap-3 px-4 py-3 bg-gray-800 text-white rounded-lg border-l-4 border-blue-500 transition-colors">
                <svg class="w-5 h-5 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path></svg>
                <span class="font-medium">My Classes</span>
            </a>
        </nav>
    </aside>

    <!-- ================= MAIN CONTENT ================= -->
    <main class="flex-1 ml-0 md:ml-64 p-6 lg:p-8">
        
        <!-- Header / Back button -->
        <div class="mb-6 flex items-center gap-4">
            <a href="<?= site_url('teacher/dashboard') ?>" class="w-10 h-10 rounded-full bg-white border border-gray-200 flex items-center justify-center text-gray-500 hover:text-blue-600 hover:border-blue-600 transition-colors shadow-sm">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
            </a>
            <div>
                <h1 class="text-2xl font-bold text-gray-900"><?= htmlspecialchars($assignment->subject_name) ?></h1>
                <div class="flex items-center gap-2 mt-1">
                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-bold bg-indigo-100 text-indigo-800">
                        <?= htmlspecialchars($assignment->class_name) ?>
                    </span>
                </div>
            </div>
        </div>

        <!-- Tabs -->
        <div class="border-b border-gray-200 mb-8">
            <nav class="-mb-px flex space-x-8">
                <button onclick="switchTab('tab-curriculum')" class="tab-btn border-blue-600 text-blue-600 whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm transition-colors">
                    Curriculum & Topics
                </button>
                <button onclick="switchTab('tab-students')" class="tab-btn border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm transition-colors">
                    Enrolled Students (<?= count($students) ?>)
                </button>
            </nav>
        </div>

        <!-- ================= TAB: CURRICULUM ================= -->
        <div id="tab-curriculum" class="tab-content block space-y-6">
            
            <div class="flex justify-between items-center">
                <h2 class="text-xl font-bold text-gray-900">Course Materials</h2>
                <button onclick="toggleModal('modal-topic')" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors shadow-sm flex items-center gap-2">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
                    New Topic
                </button>
            </div>

            <?php if(empty($topics)): ?>
                <div class="bg-white rounded-xl border border-gray-200 border-dashed p-12 text-center">
                    <svg class="mx-auto h-12 w-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path></svg>
                    <h3 class="mt-2 text-sm font-bold text-gray-900">No topics created</h3>
                    <p class="mt-1 text-sm text-gray-500">Get started by creating a new topic for your students.</p>
                </div>
            <?php else: ?>
                <div class="space-y-4">
                    <?php foreach($topics as $topic): ?>
                        <div class="bg-white border border-gray-200 rounded-xl shadow-sm overflow-hidden">
                            <div class="p-5 bg-gray-50 border-b border-gray-200 flex justify-between items-start">
                                <div>
                                    <h3 class="text-lg font-bold text-gray-900"><?= htmlspecialchars($topic->title) ?></h3>
                                    <p class="text-sm text-gray-600 mt-1"><?= htmlspecialchars($topic->description) ?></p>
                                </div>
                                <div class="flex gap-2">
                                    <button onclick="document.getElementById('form_content_topic_id').value='<?= $topic->id ?>'; toggleModal('modal-content');" class="text-sm bg-blue-50 border border-blue-200 text-blue-700 hover:bg-blue-100 px-3 py-1.5 rounded-md font-medium transition-colors">
                                        + Add Content
                                    </button>
                                    <button onclick="document.getElementById('form_topic_id').value='<?= $topic->id ?>'; toggleModal('modal-exercise');" class="text-sm bg-white border border-gray-300 text-gray-700 hover:bg-gray-50 px-3 py-1.5 rounded-md font-medium transition-colors">
                                        + Add Exercise
                                    </button>
                                </div>
                            </div>
                            
                            <div class="p-5">
                                <?php if(empty($topic->exercises) && empty($topic->content)): ?>
                                    <p class="text-sm text-gray-500 italic">No content or exercises added to this topic yet.</p>
                                <?php else: ?>
                                    <ul class="space-y-3">
                                        <!-- Display Content -->
                                        <?php if(!empty($topic->content)): ?>
                                            <?php foreach($topic->content as $content): ?>
                                                <li class="flex items-start gap-3 p-3 rounded-lg bg-gray-50 border border-gray-100">
                                                    <div class="mt-0.5 w-8 h-8 rounded bg-blue-100 text-blue-600 flex items-center justify-center shrink-0">
                                                        <?php if($content->content_type == 'youtube' || $content->content_type == 'video'): ?>
                                                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                                                        <?php elseif($content->content_type == 'audio'): ?>
                                                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19V6l12-3v13M9 19c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zm12-3c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zM9 10l12-3"></path></svg>
                                                        <?php elseif($content->content_type == 'pdf'): ?>
                                                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"></path></svg>
                                                        <?php else: ?>
                                                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path></svg>
                                                        <?php endif; ?>
                                                    </div>
                                                    <div class="flex-1 min-w-0">
                                                        <div class="flex justify-between items-start">
                                                            <div class="flex items-center gap-2">
                                                                <h4 class="text-sm font-bold text-gray-900"><?= htmlspecialchars($content->title) ?></h4>
                                                                <?php if($content->term): ?>
                                                                    <span class="px-2 py-0.5 rounded text-[10px] font-bold uppercase tracking-wider bg-purple-100 text-purple-700 border border-purple-200">
                                                                        <?= htmlspecialchars($content->term) ?>
                                                                    </span>
                                                                <?php endif; ?>
                                                            </div>
                                                            <div class="flex gap-2">
                                                                <button onclick="openEditContentModal('<?= $content->id ?>', '<?= htmlspecialchars(addslashes($content->title)) ?>', '<?= htmlspecialchars(addslashes($content->content_text)) ?>', '<?= $content->content_type ?>', '<?= $content->content_type == 'youtube' ? htmlspecialchars(addslashes($content->file_path)) : '' ?>', '<?= htmlspecialchars(addslashes($content->term ?? '')) ?>')" class="text-gray-400 hover:text-blue-600 transition-colors">
                                                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path></svg>
                                                                </button>
                                                                <a href="<?= site_url('teacher/delete_content/' . $content->id . '/' . $assignment->assignment_id) ?>" onclick="return confirm('Are you sure you want to delete this material?');" class="text-gray-400 hover:text-red-600 transition-colors">
                                                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
                                                                </a>
                                                            </div>
                                                        </div>

                                                        <?php if($content->content_type == 'text' && trim($content->content_text)): ?>
                                                            <div class="text-xs text-gray-600 mt-2 p-2 bg-white rounded border border-gray-100 prose prose-sm max-w-none"><?= nl2br($content->content_text) ?></div>
                                                        
                                                        <?php elseif($content->content_type == 'youtube'): ?>
                                                            <?php 
                                                                preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/\s]{11})%i', $content->content_text, $match);
                                                                $youtube_id = $match[1] ?? null;
                                                            ?>
                                                            <?php if($youtube_id): ?>
                                                                <div class="mt-2 aspect-w-16 aspect-h-9 rounded overflow-hidden border border-gray-200" style="max-width: 400px;">
                                                                    <iframe class="w-full h-full" style="min-height: 225px;" src="https://www.youtube.com/embed/<?= $youtube_id ?>" title="YouTube video" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
                                                                </div>
                                                            <?php else: ?>
                                                                <a href="<?= htmlspecialchars($content->content_text) ?>" target="_blank" class="text-xs text-blue-600 hover:underline mt-1 inline-block">Watch on YouTube</a>
                                                            <?php endif; ?>
                                                            
                                                        <?php elseif($content->content_type == 'video' && $content->file_path): ?>
                                                            <div class="mt-2 rounded overflow-hidden border border-gray-200" style="max-width: 400px;">
                                                                <video controls class="w-full" style="max-height: 225px;">
                                                                    <source src="<?= base_url($content->file_path) ?>" type="video/mp4">
                                                                    Your browser does not support the video tag.
                                                                </video>
                                                            </div>
                                                            
                                                        <?php elseif($content->content_type == 'audio' && $content->file_path): ?>
                                                            <div class="mt-2">
                                                                <audio controls class="w-full" style="max-width: 400px;">
                                                                    <source src="<?= base_url($content->file_path) ?>" type="audio/mpeg">
                                                                    Your browser does not support the audio element.
                                                                </audio>
                                                            </div>
                                                            
                                                        <?php elseif($content->file_path): ?>
                                                            <a href="<?= base_url($content->file_path) ?>" target="_blank" class="text-xs font-medium text-blue-600 hover:underline mt-1 inline-block">View / Download File</a>
                                                        <?php endif; ?>
                                                        
                                                        <?php if($content->content_type != 'text' && trim($content->content_text)): ?>
                                                            <div class="text-xs text-gray-600 mt-3 p-3 bg-white rounded-lg border border-gray-100 prose prose-sm max-w-none"><?= nl2br($content->content_text) ?></div>
                                                        <?php endif; ?>

                                                        <div class="mt-2">
                                                            <span class="inline-flex items-center gap-1 text-[10px] font-medium text-blue-600 bg-blue-100 px-2 py-0.5 rounded uppercase tracking-wide">
                                                                <?= htmlspecialchars($content->content_type) ?>
                                                            </span>
                                                        </div>
                                                    </div>
                                                </li>
                                            <?php endforeach; ?>
                                        <?php endif; ?>

                                        <!-- Display Exercises -->
                                        <?php if(!empty($topic->exercises)): ?>
                                            <?php foreach($topic->exercises as $ex): ?>
                                            <li class="flex items-start gap-3 p-3 rounded-lg hover:bg-gray-50 transition-colors border border-transparent hover:border-gray-100">
                                                <div class="mt-0.5 w-8 h-8 rounded bg-indigo-100 text-indigo-600 flex items-center justify-center shrink-0">
                                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01"></path></svg>
                                                </div>
                                                <div>
                                                    <h4 class="text-sm font-bold text-gray-900"><?= htmlspecialchars($ex->title) ?></h4>
                                                    <p class="text-xs text-gray-500 mt-0.5"><?= htmlspecialchars($ex->description) ?></p>
                                                    <?php if($ex->due_date): ?>
                                                        <span class="inline-flex items-center gap-1 text-[10px] font-medium text-red-600 bg-red-50 px-2 py-0.5 rounded mt-2">
                                                            <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                                                            Due: <?= date('M d, Y', strtotime($ex->due_date)) ?>
                                                        </span>
                                                    <?php endif; ?>
                                                </div>
                                            </li>
                                        <?php endforeach; ?>
                                        <?php endif; ?>
                                    </ul>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>

        <!-- ================= TAB: STUDENTS ================= -->
        <div id="tab-students" class="tab-content hidden">
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                <div class="p-6 border-b border-gray-100 flex justify-between items-center bg-gray-50">
                    <h2 class="text-lg font-bold text-gray-900">Enrolled Students</h2>
                </div>
                
                <div class="overflow-x-auto">
                    <table class="w-full text-left border-collapse">
                        <thead>
                            <tr class="bg-gray-50 text-gray-500 text-xs uppercase tracking-wider">
                                <th class="p-4 font-semibold border-b border-gray-200">Name</th>
                                <th class="p-4 font-semibold border-b border-gray-200">Admission No.</th>
                                <th class="p-4 font-semibold border-b border-gray-200">Contact</th>
                                <th class="p-4 font-semibold border-b border-gray-200 text-right">Status</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-100">
                            <?php if(!empty($students)): ?>
                                <?php foreach($students as $student): ?>
                                <tr class="hover:bg-gray-50 transition-colors">
                                    <td class="p-4">
                                        <div class="font-medium text-gray-900"><?= htmlspecialchars($student->full_name) ?></div>
                                    </td>
                                    <td class="p-4">
                                        <span class="text-sm font-mono text-gray-600"><?= htmlspecialchars($student->admission_number) ?></span>
                                    </td>
                                    <td class="p-4">
                                        <div class="text-sm text-gray-500"><?= htmlspecialchars($student->email) ?></div>
                                    </td>
                                    <td class="p-4 text-right">
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800 capitalize">
                                            <?= htmlspecialchars($student->status) ?>
                                        </span>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="4" class="p-8 text-center text-gray-500">
                                        No students have been enrolled in this class yet.
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

    </main>

    <!-- MODAL: ADD TOPIC -->
    <div id="modal-topic" class="hidden fixed inset-0 z-50 overflow-y-auto">
        <div class="flex items-center justify-center min-h-screen px-4 pt-4 pb-20 text-center sm:p-0">
            <div class="fixed inset-0 transition-opacity" aria-hidden="true">
                <div class="absolute inset-0 bg-gray-900 opacity-75"></div>
            </div>
            <div class="inline-block align-bottom bg-white rounded-xl text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full">
                <form action="<?= site_url('teacher/create_topic') ?>" method="POST">
                    <input type="hidden" name="assignment_id" value="<?= $assignment->assignment_id ?>">
                    <input type="hidden" name="subject_id" value="<?= $assignment->subject_id ?>">
                    <input type="hidden" name="class_id" value="<?= $assignment->class_id ?>">
                    
                    <div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
                        <div class="sm:flex sm:items-start">
                            <div class="mt-3 text-center sm:mt-0 sm:text-left w-full">
                                <h3 class="text-lg leading-6 font-bold text-gray-900" id="modal-title">Create New Topic</h3>
                                <div class="mt-4 space-y-4">
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Topic Title</label>
                                        <input type="text" name="title" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
                                    </div>
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Description (Optional)</label>
                                        <textarea name="description" rows="3" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
                        <button type="submit" class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 sm:ml-3 sm:w-auto sm:text-sm">Save Topic</button>
                        <button type="button" onclick="toggleModal('modal-topic')" class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm">Cancel</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- MODAL: ADD EXERCISE -->
    <div id="modal-exercise" class="hidden fixed inset-0 z-50 overflow-y-auto">
        <div class="flex items-center justify-center min-h-screen px-4 pt-4 pb-20 text-center sm:p-0">
            <div class="fixed inset-0 transition-opacity" aria-hidden="true">
                <div class="absolute inset-0 bg-gray-900 opacity-75"></div>
            </div>
            <div class="inline-block align-bottom bg-white rounded-xl text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full">
                <form action="<?= site_url('teacher/create_exercise') ?>" method="POST">
                    <input type="hidden" name="assignment_id" value="<?= $assignment->assignment_id ?>">
                    <input type="hidden" name="topic_id" id="form_topic_id" value="">
                    
                    <div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
                        <div class="sm:flex sm:items-start">
                            <div class="mt-3 text-center sm:mt-0 sm:text-left w-full">
                                <h3 class="text-lg leading-6 font-bold text-gray-900" id="modal-title">Create Exercise</h3>
                                <div class="mt-4 space-y-4">
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Exercise Title</label>
                                        <input type="text" name="title" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
                                    </div>
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Description / Instructions</label>
                                        <textarea name="description" rows="3" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"></textarea>
                                    </div>
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Due Date</label>
                                        <input type="date" name="due_date" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
                        <button type="submit" class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-indigo-600 text-base font-medium text-white hover:bg-indigo-700 sm:ml-3 sm:w-auto sm:text-sm">Save Exercise</button>
                        <button type="button" onclick="toggleModal('modal-exercise')" class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm">Cancel</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- MODAL: ADD CONTENT -->
    <div id="modal-content" class="hidden fixed inset-0 z-50 overflow-y-auto">
        <div class="flex items-center justify-center min-h-screen px-4 pt-4 pb-20 text-center sm:p-0">
            <div class="fixed inset-0 transition-opacity" aria-hidden="true">
                <div class="absolute inset-0 bg-gray-900 opacity-75"></div>
            </div>
            <div class="inline-block align-bottom bg-white rounded-xl text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full">
                <form id="content-form" action="<?= site_url('teacher/create_content') ?>" method="POST" enctype="multipart/form-data">
                    <input type="hidden" name="assignment_id" value="<?= $assignment->assignment_id ?>">
                    <input type="hidden" name="topic_id" id="form_content_topic_id" value="">
                    
                    <div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
                        <div class="sm:flex sm:items-start">
                            <div class="mt-3 text-center sm:mt-0 sm:text-left w-full">
                                <h3 class="text-lg leading-6 font-bold text-gray-900" id="modal-title">Add Course Material</h3>
                                <div class="mt-4 space-y-4">
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Material Title</label>
                                        <input type="text" name="title" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
                                    </div>
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Academic Term</label>
                                        <select name="term" id="add_term" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
                                            <option value="">-- Select Term --</option>
                                            <option value="Term 1">Term 1</option>
                                            <option value="Term 2">Term 2</option>
                                            <option value="Term 3">Term 3</option>
                                        </select>
                                    </div>
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Content Type</label>
                                        <select name="content_type" id="content_type_select" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500" onchange="toggleContentFields(this.value)">
                                            <option value="">-- Select Type --</option>
                                            <option value="text">Notes Only</option>
                                            <option value="youtube">YouTube Video</option>
                                            <option value="pdf">PDF Document</option>
                                            <option value="presentation">PowerPoint (PPTX)</option>
                                            <option value="video">Video File</option>
                                            <option value="audio">Audio File</option>
                                        </select>
                                    </div>
                                    
                                    <div id="field_content_text" class="hidden">
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Text Notes <span class="text-gray-400 font-normal">(Optional)</span></label>
                                        <input type="hidden" name="content_text" id="input_content_text">
                                        <div id="quill-editor" class="h-48 bg-white border border-gray-300 rounded-b-md"></div>
                                    </div>

                                    <div id="field_youtube_url" class="hidden">
                                        <label class="block text-sm font-medium text-gray-700 mb-1">YouTube URL</label>
                                        <input type="url" name="youtube_url" id="input_youtube_url" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
                                    </div>
                                    
                                    <div id="field_content_file" class="hidden">
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Upload File</label>
                                        <input type="file" name="file" id="input_content_file" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
                        <button type="submit" class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 sm:ml-3 sm:w-auto sm:text-sm">Save Material</button>
                        <button type="button" onclick="toggleModal('modal-content')" class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm">Cancel</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- MODAL: EDIT CONTENT -->
    <div id="modal-edit-content" class="hidden fixed inset-0 z-50 overflow-y-auto">
        <div class="flex items-center justify-center min-h-screen px-4 pt-4 pb-20 text-center sm:p-0">
            <div class="fixed inset-0 transition-opacity" aria-hidden="true">
                <div class="absolute inset-0 bg-gray-900 opacity-75"></div>
            </div>
            <div class="inline-block align-bottom bg-white rounded-xl text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full">
                <form id="edit-content-form" action="<?= site_url('teacher/update_content') ?>" method="POST">
                    <input type="hidden" name="assignment_id" value="<?= $assignment->assignment_id ?>">
                    <input type="hidden" name="content_id" id="edit_content_id" value="">
                    
                    <div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
                        <div class="sm:flex sm:items-start">
                            <div class="mt-3 text-center sm:mt-0 sm:text-left w-full">
                                <h3 class="text-lg leading-6 font-bold text-gray-900">Edit Course Material</h3>
                                <div class="mt-4 space-y-4">
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Material Title</label>
                                        <input type="text" name="title" id="edit_content_title" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
                                    </div>
                                    
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Academic Term</label>
                                        <select name="term" id="edit_term" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
                                            <option value="">-- Select Term --</option>
                                            <option value="Term 1">Term 1</option>
                                            <option value="Term 2">Term 2</option>
                                            <option value="Term 3">Term 3</option>
                                        </select>
                                    </div>

                                    <div id="edit_field_youtube" class="hidden">
                                        <label class="block text-sm font-medium text-gray-700 mb-1">YouTube URL</label>
                                        <input type="url" name="youtube_url" id="edit_youtube_url" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
                                    </div>

                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-1">Text Notes</label>
                                        <input type="hidden" name="content_text" id="edit_input_content_text">
                                        <div id="quill-editor-edit" class="h-48 bg-white border border-gray-300 rounded-b-md"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
                        <button type="submit" class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 sm:ml-3 sm:w-auto sm:text-sm">Save Changes</button>
                        <button type="button" onclick="toggleModal('modal-edit-content')" class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm">Cancel</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        // Initialize Quill Editors
        var quill = new Quill('#quill-editor', { theme: 'snow' });
        var quillEdit = new Quill('#quill-editor-edit', { theme: 'snow' });

        // Form Submissions
        document.getElementById('content-form').onsubmit = function() {
            document.getElementById('input_content_text').value = quill.root.innerHTML;
        };
        
        document.getElementById('edit-content-form').onsubmit = function() {
            document.getElementById('edit_input_content_text').value = quillEdit.root.innerHTML;
        };

        function toggleContentFields(type) {
            const fieldText = document.getElementById('field_content_text');
            const fieldYoutube = document.getElementById('field_youtube_url');
            const inputYoutube = document.getElementById('input_youtube_url');
            const fieldFile = document.getElementById('field_content_file');
            const inputFile = document.getElementById('input_content_file');
            
            fieldText.classList.add('hidden');
            fieldYoutube.classList.add('hidden');
            fieldFile.classList.add('hidden');
            inputYoutube.removeAttribute('required');
            inputFile.removeAttribute('required');

            if (type !== '') {
                fieldText.classList.remove('hidden'); // Notes are universal
            }

            if (type === 'youtube') {
                fieldYoutube.classList.remove('hidden');
                inputYoutube.setAttribute('required', 'required');
            } else if (type !== '' && type !== 'text') {
                fieldFile.classList.remove('hidden');
                inputFile.setAttribute('required', 'required');
            }
        }

        function openEditContentModal(id, title, text, type, youtube_url, term) {
            document.getElementById('edit_content_id').value = id;
            document.getElementById('edit_content_title').value = title;
            document.getElementById('edit_term').value = term;
            
            // Set Quill content safely
            quillEdit.root.innerHTML = text;
            
            if (type === 'youtube') {
                document.getElementById('edit_field_youtube').classList.remove('hidden');
                document.getElementById('edit_youtube_url').value = youtube_url;
            } else {
                document.getElementById('edit_field_youtube').classList.add('hidden');
            }
            
            toggleModal('modal-edit-content');
        }
    </script>

</body>
</html>
