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

$conta_id = intval($_GET['conta_id'] ?? 0);
$dt_ini = sanitize($_GET['dt_ini'] ?? date('Y-m-01'));
$dt_fim = sanitize($_GET['dt_fim'] ?? date('Y-m-t'));

// Buscar conta
$stmt_c = $pdo->prepare("SELECT * FROM contas_bancarias WHERE id = :id");
$stmt_c->execute(['id' => $conta_id]);
$conta = $stmt_c->fetch();

if (!$conta) {
    echo '<div class="alerta alerta-erro">Conta bancaria nao encontrada.</div>';
    require_once __DIR__ . '/../includes/footer.php';
    exit;
}

// Extrato
$stmt = $pdo->prepare("SELECT * FROM extrato_bancario WHERE conta_bancaria_id = :cid AND data_movimento BETWEEN :di AND :df ORDER BY data_movimento ASC, id ASC");
$stmt->execute(['cid' => $conta_id, 'di' => $dt_ini, 'df' => $dt_fim]);
$extrato = $stmt->fetchAll();

$total_entradas = array_sum(array_map(fn($e) => $e['tipo']==='entrada' ? $e['valor'] : 0, $extrato));
$total_saidas = array_sum(array_map(fn($e) => $e['tipo']==='saida' ? $e['valor'] : 0, $extrato));
?>

<div class="page-header">
    <div>
        <h2>Extrato: <?= sanitize($conta['nome']) ?></h2>
        <div class="subtitle"><?= sanitize($conta['banco']) ?> - Ag: <?= sanitize($conta['agencia']) ?> / CC: <?= sanitize($conta['numero_conta']) ?></div>
    </div>
    <a href="contas_bancarias.php" class="btn btn-secondary">Voltar</a>
</div>

<div class="grid-4 mb-3">
    <div class="card-kpi blue">
        <div class="kpi-label">Saldo Atual</div>
        <div class="kpi-value"><?= moeda($conta['saldo_atual']) ?></div>
    </div>
    <div class="card-kpi green">
        <div class="kpi-label">Entradas (Periodo)</div>
        <div class="kpi-value"><?= moeda($total_entradas) ?></div>
    </div>
    <div class="card-kpi red">
        <div class="kpi-label">Saidas (Periodo)</div>
        <div class="kpi-value"><?= moeda($total_saidas) ?></div>
    </div>
    <div class="card-kpi purple">
        <div class="kpi-label">Resultado</div>
        <div class="kpi-value"><?= moeda($total_entradas - $total_saidas) ?></div>
    </div>
</div>

<form method="GET" class="filtros-bar">
    <input type="hidden" name="conta_id" value="<?= $conta_id ?>">
    <div class="form-group">
        <label>De</label>
        <input type="date" name="dt_ini" value="<?= $dt_ini ?>">
    </div>
    <div class="form-group">
        <label>Ate</label>
        <input type="date" name="dt_fim" value="<?= $dt_fim ?>">
    </div>
    <div class="form-group" style="max-width:100px;">
        <label>&nbsp;</label>
        <button class="btn btn-primary w-100">Filtrar</button>
    </div>
</form>

<div class="card-premium">
<div class="table-responsive">
<table class="table-premium">
<thead>
<tr>
    <th>Data</th>
    <th>Descricao</th>
    <th>Tipo</th>
    <th class="text-end">Valor</th>
    <th class="text-end">Saldo</th>
</tr>
</thead>
<tbody>
<?php if (empty($extrato)): ?>
<tr><td colspan="5" class="text-center text-muted" style="padding:30px;">Nenhuma movimentacao no periodo</td></tr>
<?php else: ?>
<?php foreach ($extrato as $e): ?>
<tr>
    <td><?= data_br($e['data_movimento']) ?></td>
    <td><?= sanitize($e['descricao']) ?></td>
    <td><span class="badge badge-<?= $e['tipo']==='entrada' ? 'entrada' : 'saida' ?>"><?= ucfirst($e['tipo']) ?></span></td>
    <td class="text-end <?= $e['tipo']==='entrada' ? 'text-success' : 'text-danger' ?>">
        <?= $e['tipo']==='entrada' ? '+' : '-' ?> <?= moeda($e['valor']) ?>
    </td>
    <td class="text-end"><?= moeda($e['saldo_posterior']) ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>

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