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

<section class="market-hero">
    <div>
        <p class="eyebrow">Catalogo</p>
        <h1>Cursos para aprender, vender e crescer em Mocambique</h1>
        <p>Escolha um curso, pague em MZN e continue as aulas quando quiser.</p>
    </div>
    <form method="get" action="/courses" class="market-search">
        <input name="search" placeholder="Pesquisar cursos, temas ou competencias" value="<?= e($filters['search']) ?>">
        <select name="category_id">
            <option value="0">Todas as categorias</option>
            <?php foreach ($categories as $category): ?>
                <option value="<?= e((string) $category['id']) ?>" <?= (int) $filters['category_id'] === (int) $category['id'] ? 'selected' : '' ?>>
                    <?= e($category['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
        <select name="pricing">
            <option value="">Todos os precos</option>
            <option value="free" <?= $filters['pricing'] === 'free' ? 'selected' : '' ?>>Gratuitos</option>
            <option value="paid" <?= $filters['pricing'] === 'paid' ? 'selected' : '' ?>>Pagos</option>
        </select>
        <button class="button" type="submit">Buscar</button>
    </form>
</section>

<div class="catalog-head">
    <div>
        <h2>Cursos em destaque</h2>
        <span><?= e((string) count($courses)) ?> curso(s) encontrado(s)</span>
    </div>
</div>

<section class="course-grid">
    <?php if (!$courses): ?>
        <div class="panel">
            <h2>Nenhum curso encontrado</h2>
            <p>Tente ajustar a pesquisa ou escolha outra categoria.</p>
        </div>
    <?php endif; ?>

    <?php foreach ($courses as $course): ?>
        <article class="course-card">
            <a class="course-cover" style="<?= !empty($course['cover_image_url']) ? 'background-image: linear-gradient(180deg, rgba(0,0,0,.08), rgba(0,0,0,.55)), url(' . e($course['cover_image_url']) . ')' : 'background: ' . e($course['cover_color']) ?>" href="/courses/show?id=<?= e((string) $course['id']) ?>">
                <span><?= e($course['category_name']) ?></span>
            </a>
            <div class="course-body">
                <a class="course-title" href="/courses/show?id=<?= e((string) $course['id']) ?>"><?= e($course['title']) ?></a>
                <p><?= e($course['subtitle']) ?></p>
                <span class="course-author"><?= e($course['owner_name']) ?></span>
                <div class="course-meta">
                    <span><?= e((string) $course['rating']) ?> de 5</span>
                    <span><?= e((string) $course['students_count']) ?> alunos</span>
                    <span><?= e(level_label($course['level'])) ?></span>
                </div>
                <div class="course-price">
                    <?php if ($course['is_free']): ?>
                        Gratis
                    <?php elseif (!empty($course['discount_price'])): ?>
                        <span><?= e(money_mzn(course_effective_price($course))) ?></span>
                        <del><?= e(money_mzn($course['price'])) ?></del>
                    <?php else: ?>
                        <?= e(money_mzn($course['price'])) ?>
                    <?php endif; ?>
                </div>
            </div>
        </article>
    <?php endforeach; ?>
</section>

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