<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<style>
body { font-family: DejaVu Sans, Arial, sans-serif; font-size: 11px; color: #222; }
h1 { text-align: center; font-size: 15px; margin-bottom: 4px; }
.periodo { text-align: center; font-size: 11px; color: #555; margin-bottom: 10px; }
.escritorio { text-align: center; margin-bottom: 8px; }
.escritorio strong { font-size: 14px; }
.cliente-box { background: #1a659e; color: #fff; padding: 8px 12px; margin-bottom: 10px; border-radius: 3px; }
.cliente-box strong { font-size: 14px; }
table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
th { background: #1a659e; color: #fff; padding: 5px 4px; text-align: left; font-size: 10px; }
td { padding: 4px; border-bottom: 1px solid #eee; font-size: 10px; }
.text-right { text-align: right; }
.text-success { color: #2e7d32; }
.text-danger  { color: #c62828; }
.text-warning { color: #e65100; }
.tfoot-row { background: #f4f4f4; font-weight: bold; border-top: 2px solid #333; }
table.resumo-box { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
table.resumo-box td { border: 1px solid #ddd; padding: 8px; text-align: center; font-size: 11px; }
.lbl { font-size: 10px; color: #666; display: block; }
.val { font-size: 14px; font-weight: bold; display: block; margin-top: 2px; }
.rodape { font-size: 9px; text-align: center; color: #aaa; margin-top: 14px; border-top: 1px solid #eee; padding-top: 6px; }
</style>
</head>
<body>

<!-- Cabeçalho do escritório -->
<div class="escritorio">
    <?php if (!empty($setting->company_name)): ?>
    <strong><?php echo htmlspecialchars($setting->company_name) ?></strong><br>
    <?php elseif (!empty($setting->name)): ?>
    <strong><?php echo htmlspecialchars($setting->name) ?></strong><br>
    <?php endif; ?>
    <?php if (!empty($setting->company_address)): ?>
    <?php echo htmlspecialchars($setting->company_address) ?>
    <?php elseif (!empty($setting->address)): ?>
    <?php echo htmlspecialchars($setting->address) ?>
    <?php endif; ?>
</div>

<!-- Título -->
<?php if ($cliente): ?>
<div class="cliente-box">
    <strong>BPO FINANCEIRO – <?php echo htmlspecialchars($cliente->name ?? '') ?></strong>
</div>
<?php else: ?>
<h1>BPO FINANCEIRO – POSIÇÃO GERAL</h1>
<?php endif; ?>

<div class="periodo">
    Período: <?php echo date('d/m/Y', strtotime($data_ini)) ?> a <?php echo date('d/m/Y', strtotime($data_fim)) ?>
</div>

<!-- Resumo -->
<table class="resumo-box">
    <tr>
        <td style="background: #e8f5e9">
            <span class="lbl">Total Entradas</span>
            <span class="val text-success">R$ <?php echo number_format($totais['total_receber'], 2, ',', '.') ?></span>
        </td>
        <td style="background: #ffebee">
            <span class="lbl">Total Saídas</span>
            <span class="val text-danger">R$ <?php echo number_format($totais['total_pagar'], 2, ',', '.') ?></span>
        </td>
        <td style="background: <?php echo ($saldo ?? 0) >= 0 ? '#e3f2fd' : '#fff3e0' ?>">
            <span class="lbl">Saldo do Período</span>
            <span class="val <?php echo ($saldo ?? 0) >= 0 ? 'text-success' : 'text-danger' ?>">
                R$ <?php echo number_format($saldo ?? 0, 2, ',', '.') ?>
            </span>
        </td>
    </tr>
</table>

<!-- Lançamentos -->
<table>
    <thead>
        <tr>
            <th>Data/Venc.</th>
            <th>Descrição</th>
            <th>Categoria</th>
            <th class="text-right">Entrada (R$)</th>
            <th class="text-right">Saída (R$)</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
    <?php
    $total_entrada = 0;
    $total_saida   = 0;
    foreach ($lancamentos as $l):
        $v = (float)$l->valor;
        if ($l->tipo === 'receber') $total_entrada += $v;
        else                        $total_saida   += $v;
    ?>
    <tr>
        <td><?php echo date('d/m/Y', strtotime($l->data_pagamento ?? $l->vencimento)) ?></td>
        <td>
            <?php echo htmlspecialchars($l->descricao ?? '-') ?>
            <?php if (!empty($l->client_nome)): ?>
            <br><small style="color:#777"><?php echo htmlspecialchars($l->client_nome) ?></small>
            <?php endif; ?>
        </td>
        <td><?php echo htmlspecialchars($l->categoria ?? '-') ?></td>
        <td class="text-right text-success">
            <?php echo $l->tipo === 'receber' ? 'R$ ' . number_format($v, 2, ',', '.') : '' ?>
        </td>
        <td class="text-right text-danger">
            <?php echo $l->tipo === 'pagar' ? 'R$ ' . number_format($v, 2, ',', '.') : '' ?>
        </td>
        <td><?php echo ucfirst($l->status ?? 'pendente') ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    <tfoot>
        <tr class="tfoot-row">
            <td colspan="3">TOTAIS DO PERÍODO</td>
            <td class="text-right text-success">R$ <?php echo number_format($total_entrada, 2, ',', '.') ?></td>
            <td class="text-right text-danger">R$ <?php echo number_format($total_saida, 2, ',', '.') ?></td>
            <td></td>
        </tr>
    </tfoot>
</table>

<div class="rodape">
    Emitido em <?php echo date('d/m/Y \à\s H:i') ?> — Sistema Breno Paiva Advocacia — BPO Financeiro
</div>

</body>
</html>
