<?php

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

$id = intval($_GET['id'] ?? 0);

if ($id <= 0) {
    die('Convite não informado');
}

$sql = "

SELECT

    ec.*,

    a.nome AS aluno,

    a.mae_nome,
    a.mae_telefone,

    e.nome AS evento,
    e.data_evento,
    e.hora_evento,
    e.local_evento,

    ev.quantidade

FROM evento_convites ec

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

INNER JOIN eventos e
    ON e.id = ec.evento_id

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

WHERE ec.id = :id

LIMIT 1

";

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

$stmt->execute([
    'id' => $id
]);

$convite = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$convite) {
    die('Convite não encontrado');
}

?>

<style>

.convite-container{

    position:relative;

    width:900px;

    margin:20px auto;

}

.convite-fundo{

    width:100%;

    display:block;

}

/* FAMÍLIA */

.familia{

    position:absolute;

    top:230px;
    left:635px;

    width:220px;
    height:60px;

    display:flex;
    align-items:center;
    justify-content:center;

    text-align:center;

    font-size:18px;
    font-weight:bold;

    line-height:22px;

    color:#222;

}

/* CONVITE FAMÍLIA */

.convite-familia{

    position:absolute;

    top:355px;
    left:645px;

    width:220px;

    text-align:center;

    font-size:22px;
    font-weight:bold;

    color:#222;

}

/* CONVITE GERAL */

.convite-geral{

    position:absolute;

    top:460px;
    left:645px;

    width:220px;

    text-align:center;

    font-size:24px;
    font-weight:bold;

    color:#222;

}

/* QR CODE */

.qrcode{

    position:absolute;

    top:520px;
    left:645px;

    width:200px;
    height:200px;

}
.qrcode img{

    width:100%;
    height:100%;

}

/* BOTÕES */

.botoes{

    display:flex;

    gap:10px;

    justify-content:center;

    margin-bottom:20px;

}

@media print{

    .topbar,
    .sidebar,
    .menu-toggle,
    .botoes,
    .page-header{

        display:none !important;

    }

    .convite-container{

        margin:0 auto;

    }

}

</style>

<div class="page-header">

<div>

    <h2>Convite</h2>

    <div class="subtitle">

        <?= sanitize($convite['evento']) ?>

    </div>

</div>

</div>

<div class="botoes">

<button
onclick="window.print();"
class="btn btn-primary">

    Imprimir

</button>

<a
href="convites.php?venda_id=<?= $convite['venda_id'] ?>"
class="btn btn-secondary">

    Voltar

</a>

</div>

<div class="convite-container">

<img
src="../../assets/img/eventos/template_convite.png"
class="convite-fundo">

<div class="familia">

    <?= sanitize($convite['aluno']) ?>

</div>

<div class="convite-familia">

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

    /

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

</div>

<div class="convite-geral">

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

</div>

<div class="qrcode">

    <img
    src="../gerar_qrcode.php?token=<?= urlencode($convite['token']) ?>"
    alt="QR Code">

</div>

</div>

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