<?php

require_once __DIR__ . '/../../config/conexao.php';

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

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

$stmt = $pdo->prepare("

SELECT

    ec.*,

    a.nome AS aluno,

    e.nome AS 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.venda_id = :venda_id

ORDER BY ec.numero_familia

");

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

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

if (!$convites) {
    die('Nenhum convite encontrado');
}

?>

<!DOCTYPE html>
<html lang="pt-br">

<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Imprimir Convites</title>

<style>

@page{
    size:A4 portrait;
    margin:0;
}

html,
body{

    margin:0;
    padding:0;

    background:#fff;

    font-family:Arial, Helvetica, sans-serif;

}

.pagina{

    width:100%;

}

.pagina:not(:last-child){

    page-break-after:always;

}

.convite-container{

    position:relative;

    width:900px;
    height:850px;

    margin:0 auto;

    background-image:url('/sistema_licolico/assets/img/eventos/template_convite.png');
    background-repeat:no-repeat;
    background-position:top left;
    background-size:900px auto;

}

/* NOME DO ALUNO */

.familia{

    position:absolute;

    top:240px;
    left:630px;

    width:230px;

    text-align:center;

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

    line-height:22px;

    color:#222;

}

/* CONVITE FAMÍLIA */

.convite-familia{

    position:absolute;

    top:355px;
    left:630px;

    width:230px;

    text-align:center;

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

    color:#222;

}

/* CONVITE GERAL */

.convite-geral{

    position:absolute;

    top:470px;
    left:630px;

    width:230px;

    text-align:center;

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

    color:#222;

}

/* QR CODE */

.qrcode{

    position:absolute;

    top:530px;
    left:660px;

    width:180px;
    height:180px;

}

.qrcode img{

    width:100%;
    height:100%;

}

@media print{

    html,
    body{

        margin:0;
        padding:0;

    }

}

</style>

</head>

<body>

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

<div class="pagina">

    <div class="convite-container">

        <div class="familia">

            <?= nl2br(
                wordwrap(
                    htmlspecialchars($convite['aluno']),
                    18,
                    "\n",
                    true
                )
            ) ?>

        </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>

</div>

<?php endforeach; ?>

<script>

window.onload = function(){

    setTimeout(function(){

        window.print();

    }, 1000);

};

</script>

</body>
</html>