<main class="container py-4 py-md-5">
    <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3 mb-4">
        <div><span class="badge text-bg-warning text-dark rounded-pill mb-2">Formação do Instrutor</span><h1 class="h3 fw-bold mb-1">Olá, <?= e($beginner['name']) ?></h1><p class="text-muted mb-0">Estude métodos para ensinar o aluno do zero, com segurança e organização.</p></div>
        <a class="btn btn-outline-danger rounded-pill" href="<?= e(url('beginner/logout')) ?>"><i class="bi bi-box-arrow-right"></i> Sair</a>
    </div>

    <div class="row g-4 mb-4">
        <?php if(($videoCount??0)>0): ?><div class="col-md-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('portal/videos')) ?>"><span class="action-icon text-bg-success"><i class="bi bi-play-btn"></i></span><strong>Videoaulas liberadas</strong><small><?= (int)$videoCount ?> conteúdo(s) para assistir no site</small></a></div><?php endif; ?>
        <?php if(!empty($portalCharges)): ?><div class="col-md-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('portal/payments')) ?>"><span class="action-icon text-bg-warning"><i class="bi bi-bank"></i></span><strong>Bancos / cobranças</strong><small><?= count($portalCharges) ?> cobrança(s) ativa(s)</small></a></div><?php endif; ?>
        <div class="col-md-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('portal/messages')) ?>"><span class="action-icon text-bg-danger"><i class="bi bi-chat-dots"></i></span><strong>Mensagens</strong><small><?= (int)($unreadMessages??0) ?> nova(s) · código <?= e($beginner['internal_code']??'---------') ?></small></a></div>
        <div class="col-md-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('portal/contacts')) ?>"><span class="action-icon text-bg-info"><i class="bi bi-person-lines-fill"></i></span><strong>Contatos</strong><small>Localize pessoas pelo nome e converse</small></a></div>
    </div>

    <div class="card border-0 shadow-soft rounded-4 mb-4"><div class="card-body p-4">
        <h2 class="h5 fw-bold mb-3"><i class="bi bi-shop"></i> Catálogo de conteúdos</h2>
        <div class="row g-3">
            <?php foreach ($contents as $content): ?>
                <div class="col-md-6 col-xl-4"><div class="border rounded-4 h-100 d-flex flex-column overflow-hidden">
                    <?php if (!empty($content['cover_path'])): ?><img class="content-cover-card" src="<?= e(url('media/training-cover', ['id'=>(int)$content['id']])) ?>" alt="Imagem de <?= e($content['title']) ?>"><?php endif; ?>
                    <div class="p-3 d-flex flex-column flex-grow-1">
                    <div class="d-flex justify-content-between gap-2 mb-2"><span class="badge text-bg-primary"><?= e(training_content_type_label($content['content_type'])) ?></span><strong><?= e(format_money_br($content['price'])) ?></strong></div>
                    <h3 class="h6 fw-bold"><?= e($content['title']) ?></h3>
                    <p class="small text-muted flex-grow-1"><?= nl2br(e($content['description'] ?? '')) ?></p>
                    <?php if (!empty($orderedIds[(int)$content['id']])): ?><button class="btn btn-outline-secondary rounded-pill" disabled>Já solicitado</button><?php else: ?>
                    <form method="post" action="<?= e(url('beginner/request')) ?>"><?= CSRF::input() ?><input type="hidden" name="content_id" value="<?= (int)$content['id'] ?>"><button class="btn btn-success rounded-pill w-100"><i class="bi bi-cart-check"></i> Solicitar conteúdo</button></form>
                    <?php endif; ?>
                    </div>
                </div></div>
            <?php endforeach; ?>
            <?php if (!$contents): ?><div class="col-12"><div class="alert alert-info rounded-4 mb-0">Nenhum conteúdo publicado no momento.</div></div><?php endif; ?>
        </div>
    </div></div>

    <div class="card border-0 shadow-soft rounded-4"><div class="card-body p-4">
        <h2 class="h5 fw-bold mb-3"><i class="bi bi-journal-check"></i> Minhas solicitações e conteúdos</h2>
        <?php if (!$orders): ?><div class="alert alert-warning rounded-4 mb-0">Você ainda não solicitou nenhum conteúdo.</div><?php else: ?>
        <div class="row g-3">
            <?php foreach ($orders as $order): ?>
                <?php $released = in_array($order['status'], ['approved','granted'], true); ?>
                <div class="col-lg-6"><div class="border rounded-4 h-100 overflow-hidden">
                    <?php if (!empty($order['cover_path'])): ?><img class="content-cover-card" src="<?= e(url('media/training-cover', ['id'=>(int)$order['content_id']])) ?>" alt="Imagem de <?= e($order['title']) ?>"><?php endif; ?>
                    <div class="p-3">
                    <div class="d-flex justify-content-between gap-2 align-items-start"><div><span class="badge text-bg-primary mb-2"><?= e(training_content_type_label($order['content_type'])) ?></span><h3 class="h6 fw-bold mb-1"><?= e($order['title']) ?></h3></div><span class="badge <?= e(training_order_status_badge($order['status'])) ?>"><?= e(training_order_status_label($order['status'])) ?></span></div>
                    <p class="small text-muted"><?= nl2br(e($order['description'] ?? '')) ?></p>
                    <?php if (!empty($order['admin_message'])): ?><div class="alert alert-light border rounded-3 py-2"><strong>Mensagem do administrador:</strong><br><?= nl2br(e($order['admin_message'])) ?></div><?php endif; ?>
                    <?php if (!$released): ?>
                        <div class="d-flex flex-wrap gap-2 mb-2">
                            <?php if (!empty($order['payment_link'])): ?><a target="_blank" rel="noopener" class="btn btn-primary btn-sm rounded-pill" href="<?= e($order['payment_link']) ?>"><i class="bi bi-credit-card"></i> Abrir cobrança</a><?php endif; ?>
                            <?php if (!empty($order['pix_code'])): ?><button type="button" class="btn btn-success btn-sm rounded-pill" data-copy="<?= e($order['pix_code']) ?>"><i class="bi bi-copy"></i> Copiar PIX</button><?php endif; ?>
                            <span class="badge text-bg-warning text-dark align-self-center"><?= e(format_money_br($order['amount'])) ?></span>
                        </div>
                        <?php if (!in_array($order['status'], ['proof_sent'], true)): ?>
                        <form method="post" action="<?= e(url('beginner/proof')) ?>" enctype="multipart/form-data" class="row g-2">
                            <?= CSRF::input() ?><input type="hidden" name="order_id" value="<?= (int)$order['id'] ?>">
                            <div class="col-sm-8"><input type="file" name="proof_file" class="form-control form-control-sm" accept=".pdf,image/*" required></div><div class="col-sm-4"><button class="btn btn-outline-primary btn-sm rounded-pill w-100"><i class="bi bi-upload"></i> Comprovante</button></div>
                        </form>
                        <?php endif; ?>
                    <?php else: ?>
                        <?php if ($order['content_type'] === 'video'): ?><a target="_blank" rel="noopener" class="btn btn-success rounded-pill" href="<?= e($order['video_url']) ?>"><i class="bi bi-play-circle"></i> Assistir videoaula</a><?php else: ?><a target="_blank" class="btn btn-success rounded-pill" href="<?= e(url('beginner/download', ['id'=>(int)$order['content_id']])) ?>"><i class="bi bi-book"></i> Abrir material</a><?php endif; ?>
                    <?php endif; ?>
                    </div>
                </div></div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div></div>
</main>
<script>document.querySelectorAll('[data-copy]').forEach(function(b){b.addEventListener('click',function(){navigator.clipboard.writeText(b.dataset.copy||'');b.innerHTML='<i class="bi bi-check2"></i> Copiado';});});</script>
