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

<section class="panel">
    <p class="eyebrow">Administracao</p>
    <h1>Promocoes e vendas</h1>
    <p>Gerir cupoes, campanhas promocionais e afiliados.</p>

    <div class="promo-grid">
        <form class="sub-panel form" method="post" action="/admin/promotions/coupons/store">
            <?= csrf_field() ?>
            <h2>Novo cupao</h2>
            <label>Codigo <input name="code" placeholder="BEMVINDO10" required></label>
            <label>Descricao <input name="description" placeholder="Desconto de lancamento"></label>
            <div class="grid two">
                <label>Tipo
                    <select name="discount_type">
                        <option value="percent">Percentagem</option>
                        <option value="fixed">Valor fixo</option>
                    </select>
                </label>
                <label>Desconto <input name="discount_value" type="number" min="1" step="0.01" required></label>
            </div>
            <label>Curso
                <select name="course_id">
                    <option value="0">Todos cursos pagos</option>
                    <?php foreach ($courses as $course): ?>
                        <?php if (empty($course['is_free'])): ?>
                            <option value="<?= e((string) $course['id']) ?>"><?= e($course['title']) ?></option>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </select>
            </label>
            <div class="grid two">
                <label>Limite de usos <input name="max_uses" type="number" min="0" value="0"></label>
                <label>Inicio <input name="starts_at" type="date"></label>
            </div>
            <label>Fim <input name="ends_at" type="date"></label>
            <button class="button" type="submit">Criar cupao</button>
        </form>

        <form class="sub-panel form" method="post" action="/admin/promotions/campaigns/store">
            <?= csrf_field() ?>
            <h2>Nova promocao</h2>
            <label>Nome <input name="name" placeholder="Oferta da semana" required></label>
            <label>Curso
                <select name="course_id" required>
                    <?php foreach ($courses as $course): ?>
                        <?php if (empty($course['is_free'])): ?>
                            <option value="<?= e((string) $course['id']) ?>"><?= e($course['title']) ?> - <?= e(money_mzn($course['price'])) ?></option>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </select>
            </label>
            <label>Preco promocional <input name="discount_price" type="number" min="1" step="0.01" required></label>
            <div class="grid two">
                <label>Inicio <input name="starts_at" type="date"></label>
                <label>Fim <input name="ends_at" type="date"></label>
            </div>
            <button class="button" type="submit">Criar promocao</button>
        </form>

        <form class="sub-panel form" method="post" action="/admin/promotions/affiliates/store">
            <?= csrf_field() ?>
            <h2>Novo afiliado</h2>
            <label>Nome <input name="name" required></label>
            <label>Email <input name="email" type="email" required></label>
            <label>Codigo <input name="code" placeholder="Automatico se vazio"></label>
            <label>Comissao (%) <input name="commission_percentage" type="number" min="1" max="50" step="0.01" value="10"></label>
            <button class="button" type="submit">Criar afiliado</button>
        </form>
    </div>
</section>

<section class="panel">
    <h2>Cupoes</h2>
    <div class="table-wrap">
        <table>
            <thead><tr><th>Codigo</th><th>Desconto</th><th>Curso</th><th>Uso</th><th>Periodo</th><th>Estado</th></tr></thead>
            <tbody>
                <?php if (!$coupons): ?><tr><td colspan="6">Nenhum cupao criado.</td></tr><?php endif; ?>
                <?php foreach ($coupons as $coupon): ?>
                    <tr>
                        <td><strong><?= e($coupon['code']) ?></strong><span class="muted"><?= e($coupon['description']) ?></span></td>
                        <td><?= $coupon['discount_type'] === 'percent' ? e((string) $coupon['discount_value']) . '%' : e(money_mzn($coupon['discount_value'])) ?></td>
                        <td><?= (int) $coupon['course_id'] > 0 ? '#' . e((string) $coupon['course_id']) : 'Todos' ?></td>
                        <td><?= e((string) $coupon['used_count']) ?><?= (int) $coupon['max_uses'] > 0 ? '/' . e((string) $coupon['max_uses']) : '' ?></td>
                        <td><?= e($coupon['starts_at'] ?: '-') ?> ate <?= e($coupon['ends_at'] ?: '-') ?></td>
                        <td><span class="badge active">Activo</span></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

<section class="detail-grid">
    <div class="panel">
        <h2>Campanhas</h2>
        <div class="table-wrap">
            <table>
                <thead><tr><th>Nome</th><th>Curso</th><th>Preco</th><th>Periodo</th></tr></thead>
                <tbody>
                    <?php if (!$campaigns): ?><tr><td colspan="4">Nenhuma promocao criada.</td></tr><?php endif; ?>
                    <?php foreach ($campaigns as $campaign): ?>
                        <tr>
                            <td><?= e($campaign['name']) ?></td>
                            <td><?= e($campaign['course_title']) ?></td>
                            <td><?= e(money_mzn($campaign['discount_price'])) ?></td>
                            <td><?= e($campaign['starts_at'] ?: '-') ?> ate <?= e($campaign['ends_at'] ?: '-') ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <div class="panel">
        <h2>Afiliados</h2>
        <div class="table-wrap">
            <table>
                <thead><tr><th>Afiliado</th><th>Codigo</th><th>Comissao</th><th>Link</th></tr></thead>
                <tbody>
                    <?php if (!$affiliates): ?><tr><td colspan="4">Nenhum afiliado criado.</td></tr><?php endif; ?>
                    <?php foreach ($affiliates as $affiliate): ?>
                        <tr>
                            <td><?= e($affiliate['name']) ?><span class="muted"><?= e($affiliate['email']) ?></span></td>
                            <td><strong><?= e($affiliate['code']) ?></strong></td>
                            <td><?= e((string) $affiliate['commission_percentage']) ?>%</td>
                            <td><span class="muted">/courses/show?id=1&ref=<?= e($affiliate['code']) ?></span></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

<section class="panel">
    <h2>Comissoes de afiliados</h2>
    <div class="table-wrap">
        <table>
            <thead><tr><th>Afiliado</th><th>Curso</th><th>Venda</th><th>%</th><th>Comissao</th><th>Estado</th></tr></thead>
            <tbody>
                <?php if (!$affiliateCommissions): ?><tr><td colspan="6">Nenhuma comissao registada.</td></tr><?php endif; ?>
                <?php foreach ($affiliateCommissions as $commission): ?>
                    <tr>
                        <td><?= e($commission['affiliate_name']) ?><span class="muted"><?= e($commission['affiliate_code']) ?></span></td>
                        <td><?= e($commission['course_title']) ?></td>
                        <td><?= e(money_mzn($commission['gross_amount'])) ?></td>
                        <td><?= e((string) $commission['commission_percentage']) ?>%</td>
                        <td><?= e(money_mzn($commission['commission_amount'])) ?></td>
                        <td><span class="badge active"><?= e($commission['status']) ?></span></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

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