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

<section class="payment-layout">
    <div class="panel">
        <p class="eyebrow">Carrinho</p>
        <h1>Finalizar compra</h1>
        <p>Junte cursos pagos, aplique cupao e crie pagamentos manuais para enviar comprovativos.</p>

        <div class="cart-list">
            <?php if (!$cart['items']): ?>
                <div class="sub-panel">
                    <h2>Carrinho vazio</h2>
                    <p>Explore cursos e adicione os que pretende comprar.</p>
                    <a class="button" href="/courses">Explorar cursos</a>
                </div>
            <?php endif; ?>

            <?php foreach ($cart['items'] as $item): ?>
                <?php $course = $item['course']; $pricing = $item['pricing']; ?>
                <article class="cart-item">
                    <a class="cart-thumb" href="/courses/show?id=<?= e((string) $course['id']) ?>" style="background-image: linear-gradient(180deg, rgba(0,0,0,.08), rgba(0,0,0,.52)), url(<?= e($course['cover_image_url']) ?>)"></a>
                    <div>
                        <strong><?= e($course['title']) ?></strong>
                        <span><?= e($course['owner_name']) ?> - <?= e($course['category_name']) ?></span>
                        <?php if (!empty($item['already_owned'])): ?>
                            <span class="badge active">Ja tem acesso</span>
                        <?php endif; ?>
                    </div>
                    <div class="cart-price">
                        <strong><?= e(money_mzn($pricing['total'])) ?></strong>
                        <?php if ((float) $pricing['total'] < (float) $pricing['base_price']): ?>
                            <del><?= e(money_mzn($pricing['base_price'])) ?></del>
                        <?php endif; ?>
                    </div>
                    <form method="post" action="/cart/remove">
                        <?= csrf_field() ?>
                        <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                        <button class="mini-button danger" type="submit">Remover</button>
                    </form>
                </article>
            <?php endforeach; ?>
        </div>
    </div>

    <aside class="panel">
        <p class="eyebrow">Resumo</p>
        <h2>Total do pedido</h2>

        <form class="form" method="post" action="/cart/update">
            <?= csrf_field() ?>
            <label>
                Cupao
                <input name="coupon_code" value="<?= e($cart['coupon_code']) ?>" placeholder="Ex: BEMVINDO10">
            </label>
            <label>
                Codigo de afiliado
                <input name="affiliate_code" value="<?= e($cart['affiliate_code']) ?>" placeholder="Opcional">
            </label>
            <button class="ghost-button" type="submit">Aplicar</button>
        </form>

        <div class="detail-list one cart-summary">
            <div><span>Subtotal</span><strong><?= e(money_mzn($cart['subtotal'])) ?></strong></div>
            <div><span>Descontos</span><strong><?= e(money_mzn($cart['discount_total'])) ?></strong></div>
            <div><span>Total</span><strong><?= e(money_mzn($cart['total'])) ?></strong></div>
        </div>

        <?php if (auth_check() && has_role('student')): ?>
            <form method="post" action="/cart/checkout">
                <?= csrf_field() ?>
                <button class="button full" type="submit" <?= !$cart['items'] ? 'disabled' : '' ?>>Criar pagamentos manuais</button>
            </form>
        <?php else: ?>
            <a class="button full" href="/login">Entrar para finalizar</a>
        <?php endif; ?>

        <?php if ($cart['items']): ?>
            <form method="post" action="/cart/clear">
                <?= csrf_field() ?>
                <button class="ghost-button full" type="submit">Limpar carrinho</button>
            </form>
        <?php endif; ?>
    </aside>
</section>

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