<?php

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

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

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

if ($busca != '') {

    $where .= " AND (
        nome LIKE ?
        OR local_evento LIKE ?
    )";

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

$sql = "

SELECT *

FROM eventos

WHERE {$where}

ORDER BY data_evento DESC

";

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

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

?>

<div class="page-header">

    <div>
        <h2>Eventos</h2>

        <div class="subtitle">
            Cadastro e gerenciamento de eventos
        </div>
    </div>

    <div>
        <a
            href="evento_form.php"
            class="btn btn-novo">

            + Novo Evento

        </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="Nome ou local do evento">

    </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>Data</th>
    <th>Horário</th>
    <th>Local</th>
    <th>Valor</th>
    <th>Status</th>
    <th class="text-end">Ações</th>

</tr>

</thead>

<tbody>

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

<tr>

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

Nenhum evento encontrado

</td>

</tr>

<?php else: ?>

<?php foreach($eventos as $evento): ?>

<tr>

<td>

    <strong>

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

    </strong>

</td>

<td>

    <?= data_br($evento['data_evento']) ?>

</td>

<td>

    <?= substr($evento['hora_evento'],0,5) ?>

</td>

<td>

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

</td>

<td>

    <?= moeda($evento['valor_convite']) ?>

</td>

<td>

<span class="badge <?= $evento['status'] == 'ATIVO'
? 'badge-ativo'
: 'badge-inativo'
?>">

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

</span>

</td>

<td class="text-end">

<a
href="evento_form.php?id=<?= $evento['id'] ?>"
class="btn btn-sm btn-info">

✏

</a>

<a
href="../Vendas/vendas.php?evento_id=<?= $evento['id'] ?>"
class="btn btn-sm btn-success">

🎟

</a>

<a
href="../Relatórios/relatorio.php?evento_id=<?= $evento['id'] ?>"
class="btn btn-sm btn-warning">

📊

</a>

<a
href="evento_excluir.php?id=<?= $evento['id'] ?>"
class="btn btn-sm btn-danger"
onclick="return confirm('Deseja excluir este evento?')">

✖

</a>

</td>

</tr>

<?php endforeach; ?>

<?php endif; ?>

</tbody>

</table>

</div>

</div>

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