<?php include app()->basePath('app/Views/partials/header.php'); ?>

<section class="panel">
    <p class="eyebrow">Comunicacao</p>
    <h1>Perguntas dos alunos</h1>
    <p>Responda duvidas feitas nas aulas e acompanhe o estado das conversas.</p>

    <form class="filterbar simple" method="get" action="/communication/questions">
        <input name="search" placeholder="Pesquisar por curso, aluno ou pergunta" value="<?= e($filters['search']) ?>">
        <select name="status">
            <option value="">Todos estados</option>
            <option value="open" <?= $filters['status'] === 'open' ? 'selected' : '' ?>>Abertas</option>
            <option value="answered" <?= $filters['status'] === 'answered' ? 'selected' : '' ?>>Respondidas</option>
        </select>
        <button class="button" type="submit">Filtrar</button>
    </form>

    <div class="question-list">
        <?php if (!$questions): ?>
            <div class="sub-panel"><p>Nenhuma pergunta encontrada.</p></div>
        <?php endif; ?>

        <?php foreach ($questions as $question): ?>
            <article class="question-card">
                <div class="question-head">
                    <div>
                        <strong><?= e($question['student_name']) ?></strong>
                        <span class="muted"><?= e($question['course_title']) ?><?= $question['lesson_title'] ? ' · ' . e($question['lesson_title']) : '' ?></span>
                    </div>
                    <span class="badge <?= e($question['status']) ?>"><?= $question['status'] === 'answered' ? 'Respondida' : 'Aberta' ?></span>
                </div>
                <p><?= nl2br(e($question['body'])) ?></p>

                <?php if (!empty($question['answer'])): ?>
                    <div class="answer-box">
                        <strong><?= e($question['answered_by_name']) ?></strong>
                        <p><?= nl2br(e($question['answer'])) ?></p>
                        <span class="muted"><?= e(format_date($question['answered_at'])) ?></span>
                    </div>
                <?php endif; ?>

                <form class="form compact-form" method="post" action="/communication/questions/answer">
                    <?= csrf_field() ?>
                    <input type="hidden" name="id" value="<?= e((string) $question['id']) ?>">
                    <label>
                        Resposta
                        <textarea name="answer" rows="3" maxlength="2000"><?= e($question['answer'] ?? '') ?></textarea>
                    </label>
                    <button class="button" type="submit"><?= $question['status'] === 'answered' ? 'Actualizar resposta' : 'Responder' ?></button>
                </form>
            </article>
        <?php endforeach; ?>
    </div>
</section>

<?php include app()->basePath('app/Views/partials/footer.php'); ?>
