<?php $pageTitle = 'Question Bank'; ?>
<div class="mb-3"><a href="<?= base_url('admin/question-banks') ?>" class="text-secondary"><i class="fas fa-arrow-left me-1"></i> Back to Banks</a></div>
<div class="card-cix mb-4">
    <div class="card-cix-header"><h5 class="mb-0"><?= e($bank['name']) ?></h5></div>
    <p class="text-secondary mb-1"><?= e($bank['description'] ?? '') ?></p>
    <p class="small mb-0">Course: <?= e($bank['course_id'] ? 'Linked' : 'None') ?> | Difficulty: <?= ucfirst(e($bank['difficulty'])) ?> | Questions: <?= count($questions) ?></p>
</div>
<div class="card-cix mb-4">
    <div class="card-cix-header"><h5 class="mb-0"><i class="fas fa-plus-circle text-orange me-2"></i> Add Question</h5></div>
    <?php if (!empty($error)): ?><div class="alert-cix alert-cix-error"><?= $error ?></div><?php endif; ?>
    <form method="POST" action="<?= base_url('admin/question-create/' . $bank['id']) ?>">
        <?= csrf_field() ?>
        <div class="row g-3">
            <div class="col-md-6"><div class="form-group-cix"><label class="form-label-cix">Type</label><select name="question_type" class="form-control-cix" id="qType"><option value="multiple_choice">Multiple Choice</option><option value="true_false">True/False</option><option value="short_answer">Short Answer</option></select></div></div>
            <div class="col-md-3"><div class="form-group-cix"><label class="form-label-cix">Difficulty</label><select name="difficulty" class="form-control-cix"><option value="easy">Easy</option><option value="medium" selected>Medium</option><option value="hard">Hard</option></select></div></div>
            <div class="col-md-3"><div class="form-group-cix"><label class="form-label-cix">Points</label><input type="number" name="points" class="form-control-cix" value="1" min="1"></div></div>
            <div class="col-12"><div class="form-group-cix"><label class="form-label-cix">Question Text *</label><textarea name="question_text" class="form-control-cix" rows="3" required></textarea></div></div>
            <div id="optionsContainer">
                <div class="col-12 mb-2"><label class="form-label-cix">Options (select the correct one)</label></div>
                <div class="row g-2 mb-2"><div class="col-11"><input type="text" name="option_text[]" class="form-control-cix" placeholder="Option 1" required></div><div class="col-1"><input type="radio" name="correct_option" value="0" checked class="form-check-input mt-3"></div></div>
                <div class="row g-2 mb-2"><div class="col-11"><input type="text" name="option_text[]" class="form-control-cix" placeholder="Option 2" required></div><div class="col-1"><input type="radio" name="correct_option" value="1" class="form-check-input mt-3"></div></div>
                <div class="row g-2 mb-2"><div class="col-11"><input type="text" name="option_text[]" class="form-control-cix" placeholder="Option 3"></div><div class="col-1"><input type="radio" name="correct_option" value="2" class="form-check-input mt-3"></div></div>
                <div class="row g-2 mb-2"><div class="col-11"><input type="text" name="option_text[]" class="form-control-cix" placeholder="Option 4"></div><div class="col-1"><input type="radio" name="correct_option" value="3" class="form-check-input mt-3"></div></div>
            </div>
            <div class="col-12"><div class="form-group-cix"><label class="form-label-cix">Explanation (optional)</label><textarea name="explanation" class="form-control-cix" rows="2"></textarea></div></div>
            <div class="col-12"><div class="form-group-cix"><label class="form-label-cix">Tags</label><input type="text" name="tags" class="form-control-cix" placeholder="e.g., basics, prompt, ai"></div></div>
        </div>
        <button type="submit" class="btn-cix btn-cix-accent mt-2"><i class="fas fa-save"></i> Add Question</button>
    </form>
</div>
<div class="card-cix">
    <div class="card-cix-header"><h5 class="mb-0"><i class="fas fa-list text-orange me-2"></i> Questions (<?= count($questions) ?>)</h5></div>
    <?php if (empty($questions)): ?>
        <div class="empty-state"><i class="fas fa-question-circle"></i><h5>No Questions</h5><p>Add your first question above.</p></div>
    <?php else: ?>
        <?php foreach ($questions as $i => $q): ?>
        <div style="padding:1rem;border-bottom:1px solid var(--border);">
            <div class="d-flex align-items-center gap-2 mb-1">
                <span class="badge-cix badge-submitted" style="font-size:0.65rem;">Q<?= ($i + 1) ?></span>
                <span class="badge-cix badge-<?= $q['difficulty'] === 'easy' ? 'active' : ($q['difficulty'] === 'hard' ? 'rejected' : 'submitted') ?>" style="font-size:0.65rem;"><?= ucfirst($q['difficulty']) ?></span>
                <span class="badge-cix" style="font-size:0.65rem;background:var(--surface-2);color:var(--text-secondary);"><?= $q['points'] ?>pt</span>
                <span class="badge-cix" style="font-size:0.65rem;background:var(--surface-2);color:var(--text-secondary);"><?= str_replace('_', ' ', $q['question_type']) ?></span>
            </div>
            <p class="mb-1"><?= e($q['question_text']) ?></p>
            <?php if (!empty($q['options'])): ?>
                <div class="ms-3"><?php foreach ($q['options'] as $opt): ?>
                    <p class="small mb-0 <?= $opt['is_correct'] ? 'text-orange fw-bold' : 'text-secondary' ?>">
                        <?= $opt['is_correct'] ? '<i class="fas fa-check-circle"></i>' : '<i class="fas fa-circle" style="font-size:0.5rem;"></i>' ?>
                        <?= e($opt['option_text']) ?>
                    </p>
                <?php endforeach; ?></div>
            <?php endif; ?>
        </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>
