<?php

require_once __DIR__ . '/../../includes/header.php';

$busca = sanitize($_GET['busca'] ?? '');

$where = "1=1";
$params = [];

if ($busca != '') {

    $where .= " AND (
        a.nome LIKE ?
        OR ev.responsavel LIKE ?
    )";

    $params[] = "%{$busca}%";
    $params[] = "%{$busca}%";
}

$sql = "

SELECT

    ev.*,

    a.nome AS aluno,

    e.nome AS evento

FROM evento_vendas ev

LEFT JOIN alunos a
    ON a.id = ev.aluno_id

LEFT JOIN eventos e
    ON e.id = ev.evento_id

WHERE {$where}

ORDER BY ev.id DESC

";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);

$vendas = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<div class="page-header">


<div>

    <h2>Vendas de Convites</h2>

    <div class="subtitle">
        Controle de vendas dos eventos
    </div>

</div>

<div>

    <a href="venda_form.php" class="btn btn-novo">
        + Nova Venda
    </a>

</div>


</div>

<form method="GET" class="filtros-bar">

<div class="form-group">

    <label>Buscar</label>

    <input
        type="text"
        name="busca"
        value="<?= sanitize($busca) ?>"
        placeholder="Aluno ou responsável">

</div>

<div class="form-group">

    <label>&nbsp;</label>

    <button class="btn btn-primary">
        Filtrar
    </button>

</div>


</form>

<div class="card-premium">


<div class="table-responsive">

    <table class="table-premium">

        <thead>

            <tr>

                <th>Evento</th>
                <th>Aluno</th>
                <th>Responsável</th>
                <th>Qtd</th>
                <th>Total</th>
                <th>Pago</th>
                <th width="180">Ações</th>

            </tr>

        </thead>

        <tbody>

        <?php if(empty($vendas)): ?>

            <tr>

                <td colspan="7"
                    class="text-center"
                    style="padding:30px;">

                    Nenhuma venda encontrada

                </td>

            </tr>

        <?php else: ?>

            <?php foreach($vendas as $v): ?>

            <tr>

                <td>
                    <?= sanitize($v['evento']) ?>
                </td>

                <td>
                    <?= sanitize($v['aluno']) ?>
                </td>

                <td>
                    <?= sanitize($v['responsavel']) ?>
                </td>

                <td>
                    <?= intval($v['quantidade']) ?>
                </td>

                <td>
                    <?= moeda($v['valor_total']) ?>
                </td>

                <td>

                    <span class="badge <?= $v['pago'] == 'S'
                        ? 'badge-ativo'
                        : 'badge-inativo' ?>">

                        <?= $v['pago'] == 'S'
                            ? '✔ Pago'
                            : '⏳ Pendente' ?>

                    </span>

                </td>

                <td>

                    <div style="display:flex;gap:6px;">

                        <a
                            href="../Convites/convites.php?venda_id=<?= $v['id'] ?>"
                            class="btn btn-sm btn-success"
                            title="Convites">

                            🎟

                        </a>

                        <?php if($v['pago'] != 'S'): ?>

                            <a
                                href="receber.php?id=<?= $v['id'] ?>"
                                class="btn btn-sm btn-primary"
                                title="Receber"
                                onclick="return confirm('Confirmar recebimento desta venda?');">

                                💰

                            </a>

                        <?php else: ?>

                            <span
                                class="btn btn-sm btn-secondary"
                                style="cursor:default;">

                                ✅

                            </span>

                        <?php endif; ?>

                    </div>

                </td>

            </tr>

            <?php endforeach; ?>

        <?php endif; ?>

        </tbody>

    </table>

</div>


</div>

<?php require_once __DIR__ . '/../../includes/footer.php'; ?>
