<?php

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

$venda_id = intval($_GET['venda_id'] ?? 0);
$busca = trim($_GET['busca'] ?? '');

if($venda_id <= 0){
    die('Venda não informada');
}

$sql = "

SELECT

    ec.*,

    a.nome,
    a.mae_nome,

    ev.quantidade

FROM evento_convites ec

INNER JOIN alunos a
    ON a.id = ec.aluno_id

INNER JOIN evento_vendas ev
    ON ev.id = ec.venda_id

WHERE ec.venda_id = :venda_id

";

$params = [
    'venda_id' => $venda_id
];

if($busca != ''){

    $sql .= "

    AND (

        a.nome LIKE :busca
        OR a.mae_nome LIKE :busca

    )

    ";

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

$sql .= "

ORDER BY ec.numero_familia

";

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

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

?>

<style>

.badge-codigo{

    background:#1e293b;
    color:#60a5fa;

    padding:6px 12px;

    border-radius:20px;

    font-weight:600;

    display:inline-block;

}

.numero-geral{

    font-weight:bold;

    color:#22c55e;

    font-size:15px;

}

.numero-familia{

    font-weight:bold;

    color:#f59e0b;

    font-size:15px;

}

.table-hover tbody tr:hover{

    background:rgba(255,255,255,.03);

}

.acoes{

    display:flex;
    gap:5px;

}

.card-premium{

    padding:25px;

}

.table{

    width:100%;
    border-collapse:collapse;

}

.table th{

    padding:15px;

    text-align:left;

    border-bottom:1px solid rgba(255,255,255,.08);

}

.table td{

    padding:12px 15px;

    border-bottom:1px solid rgba(255,255,255,.05);

}

.btn-sm{

    padding:6px 10px;
    font-size:12px;

}

.aluno{

    font-weight:600;
    color:#fff;

}

.responsavel{

    color:#94a3b8;
    font-size:12px;

}

.filtro{

    display:flex;
    gap:10px;
    margin-bottom:20px;

}

.filtro input{

    flex:1;

}

@media print{

    .topbar,
    .sidebar,
    .page-header .btn,
    .acoes,
    .filtro{

        display:none !important;

    }

}

</style>

<div class="page-header">

    <div>

        <h2>🎟 Convites Gerados</h2>

        <div class="subtitle">

            Total de <?= count($convites) ?> convites

        </div>

    </div>

    <div>

        <a
        href="imprimir_venda.php?venda_id=<?= $venda_id ?>"
        target="_blank"
        class="btn btn-success">

            🖨 Imprimir Convites

        </a>

    </div>

</div>

<form method="GET" class="filtro">

    <input
    type="hidden"
    name="venda_id"
    value="<?= $venda_id ?>">

    <input
    type="text"
    name="busca"
    class="form-control"
    placeholder="Buscar aluno ou responsável..."
    value="<?= htmlspecialchars($busca) ?>">

    <button
    class="btn btn-primary">

        Buscar

    </button>

</form>

<div class="card-premium">

<table class="table table-hover">

<thead>

<tr>

    <th>Aluno</th>
    <th width="120">Convite</th>
    <th width="120">Geral</th>
    <th width="180">Código</th>
    <th width="180">Ações</th>

</tr>

</thead>

<tbody>

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

<tr>

    <td colspan="5"
        style="padding:30px;text-align:center;">

        Nenhum convite encontrado

    </td>

</tr>

<?php else: ?>

<?php foreach($convites as $c): ?>

<tr>

<td>

    <div class="aluno">

        <?= htmlspecialchars($c['nome']) ?>

    </div>

    <div class="responsavel">

        <?= htmlspecialchars($c['mae_nome']) ?>

    </div>

</td>

<td>

    <span class="numero-familia">

        <?= str_pad(
            $c['numero_familia'],
            2,
            '0',
            STR_PAD_LEFT
        ) ?>

        /

        <?= str_pad(
            $c['quantidade'],
            2,
            '0',
            STR_PAD_LEFT
        ) ?>

    </span>

</td>

<td>

    <span class="numero-geral">

        <?= str_pad(
            $c['numero_geral'],
            4,
            '0',
            STR_PAD_LEFT
        ) ?>

    </span>

</td>

<td>

    <span class="badge-codigo">

        <?= htmlspecialchars($c['codigo']) ?>

    </span>

</td>

<td>

    <div class="acoes">

        <a
        href="convite.php?id=<?= $c['id'] ?>"
        class="btn btn-primary btn-sm">

            👁 Visualizar

        </a>

        <a
        href="convite.php?id=<?= $c['id'] ?>"
        target="_blank"
        class="btn btn-success btn-sm">

            🖨 Imprimir

        </a>

    </div>

</td>

</tr>

<?php endforeach; ?>

<?php endif; ?>

</tbody>

</table>

</div>

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