<?php /* Depto de Engenharia e Automação - Master Automação - Autor: Derlivon Silva */ ?>
<?php
$fmt = fn($v,$d=2) => 'R$ '.number_format((float)$v,$d,',','.');
$totalFat   = array_sum(array_column($linhas,'FATURAMENTO'));
$totalComis = array_sum(array_column($linhas,'COMISSAO'));
$percMedio  = $totalFat > 0 ? $totalComis / $totalFat * 100 : 0;
?>
<div class="pbi-page-header no-print">
    <h1 class="pbi-page-title"><i class="bi bi-percent me-2 text-accent"></i>Comissões de Garçons</h1>
</div>
<form method="get" class="pbi-filtro-barra mb-3 no-print">
    <div class="pbi-filtro-row">
        <div class="pbi-filtro-field"><label class="pbi-filtro-label">De</label>
            <input type="date" name="data_ini" value="<?= $filtros['dataIni'] ?>" class="pbi-filtro-input form-control"></div>
        <div class="pbi-filtro-field"><label class="pbi-filtro-label">Até</label>
            <input type="date" name="data_fim" value="<?= $filtros['dataFim'] ?>" class="pbi-filtro-input form-control"></div>
        <div class="pbi-filtro-field"><label class="pbi-filtro-label">% Comissão</label>
            <input type="number" name="perc" value="<?= (float)($filtros['perc']??10) ?>" min="0" max="100" step="0.5" class="pbi-filtro-input form-control" style="width:90px"></div>
        <button type="submit" class="pbi-btn-filtrar btn"><i class="bi bi-funnel me-1"></i>Filtrar</button>
    </div>
</form>

<?php include __DIR__ . '/_header.php' ?>

<?php
$relatorioMeta = [
    'titulo' => 'Comissões de Garçons',
    'empresa' => $empresa ?? '',
    'totais' => [
        ['label' => 'Garçons', 'valor' => count($linhas)],
        ['label' => 'Faturamento Total', 'valor' => $fmt($totalFat)],
        ['label' => 'Total Comissões', 'valor' => $fmt($totalComis)],
        ['label' => 'Taxa Aplicada', 'valor' => number_format((float)($filtros['perc']??10),1,',','.').'%'],
    ],
];
if(!empty($filtros['dataIni']) && !empty($filtros['dataFim'])){
    $relatorioMeta['periodo'] = date('d/m/Y', strtotime($filtros['dataIni'])) . ' a ' . date('d/m/Y', strtotime($filtros['dataFim']));
}
?>
<script type="application/json" id="relatorio-meta"><?= json_encode($relatorioMeta, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) ?></script>

<div class="pbi-kpi-grid mb-3 no-print" data-export-secao="kpis">
    <div class="pbi-kpi-card"><div class="pbi-kpi-label">Garçons</div><div class="pbi-kpi-value"><?= count($linhas) ?></div></div>
    <div class="pbi-kpi-card"><div class="pbi-kpi-label">Faturamento Total</div><div class="pbi-kpi-value text-accent"><?= $fmt($totalFat) ?></div></div>
    <div class="pbi-kpi-card"><div class="pbi-kpi-label">Total Comissões</div><div class="pbi-kpi-value text-warning"><?= $fmt($totalComis) ?></div></div>
    <div class="pbi-kpi-card"><div class="pbi-kpi-label">Taxa Aplicada</div><div class="pbi-kpi-value"><?= number_format((float)($filtros['perc']??10),1,',','.').'%' ?></div></div>
</div>

<div class="mb-2"><?php include __DIR__ . '/_acoes.php' ?></div>

<div class="pbi-card mb-3" data-export-secao="dados">
    <div class="pbi-card-header d-flex justify-content-between">
        <span><i class="bi bi-percent me-2"></i>Comissões por Garçom</span>
        <small class="text-muted no-print"><?= count($linhas) ?> garçons · <?= number_format($filtros['perc']??10,1,',','.').'%' ?> sobre faturamento</small>
    </div>
    <div class="pbi-hint mb-2" style="margin-top:0">Comissão a pagar por garçom. <strong>% Part.</strong> = fatia do garçom no faturamento total; <strong>Tx. Serviço</strong> = gorjeta (10%) lançada nas mesas; <strong>Comissão</strong> = % definido no filtro aplicado sobre o faturamento.</div>
    <div class="table-responsive">
        <table class="pbi-table pbi-table-print">
            <thead><tr>
                <th>#</th><th>Garçom</th>
                <th class="text-end">Mesas</th>
                <th class="text-end">Faturamento</th>
                <th class="text-end">% Part.</th>
                <th class="text-end">Tx. Serviço</th>
                <th class="text-end">Comissão (<?= number_format($filtros['perc']??10,1,',','.').'%' ?>)</th>
                <th class="text-end">Total a Pagar</th>
            </tr></thead>
            <tbody>
            <?php foreach($linhas as $i=>$r):
                $fat    = (float)($r['FATURAMENTO']??0);
                $tx     = (float)($r['TX_SERVICO']??0);
                $comis  = (float)($r['COMISSAO']??0);
                $total  = $comis;
            ?>
            <tr>
                <td class="text-muted"><?= $i+1 ?></td>
                <td class="fw-bold"><?= \App\Core\View::e($r['GARCOM']??'—') ?></td>
                <td class="text-end"><?= (int)($r['QTD_MESAS']??0) ?></td>
                <td class="text-end"><?= $fmt($fat) ?></td>
                <td class="text-end text-muted"><?= $totalFat>0?number_format($fat/$totalFat*100,1,',','.').'%':'—' ?></td>
                <td class="text-end"><?= $tx>0?$fmt($tx):'—' ?></td>
                <td class="text-end fw-bold text-accent"><?= $fmt($comis) ?></td>
                <td class="text-end fw-bold text-warning"><?= $fmt($total) ?></td>
            </tr>
            <?php endforeach; ?>
            <?php if(!$linhas): ?><tr><td colspan="8" class="text-center py-3 text-muted">Sem dados</td></tr><?php endif; ?>
            </tbody>
            <tfoot><tr class="pbi-table-total">
                <td colspan="3" class="text-end fw-bold">Total:</td>
                <td class="text-end fw-bold text-accent"><?= $fmt($totalFat) ?></td>
                <td colspan="2"></td>
                <td class="text-end fw-bold text-accent"><?= $fmt($totalComis) ?></td>
                <td class="text-end fw-bold text-warning"><?= $fmt($totalComis) ?></td>
            </tr></tfoot>
        </table>
    </div>
</div>

<?php if($mensal): ?>
<div class="pbi-card">
    <div class="pbi-card-header"><i class="bi bi-calendar3 me-2"></i>Evolução Mensal de Comissões</div>
    <div class="pbi-hint mb-2" style="margin-top:0">Total de comissões pagas mês a mês. <strong>% sobre Fat.</strong> = peso da comissão dentro do faturamento de cada mês.</div>
    <div class="table-responsive">
        <table class="pbi-table pbi-table-print">
            <thead><tr>
                <th>Mês</th>
                <th class="text-end">Faturamento</th>
                <th class="text-end">Comissão</th>
                <th class="text-end">% sobre Fat.</th>
            </tr></thead>
            <tbody>
            <?php
            $meses = ['','Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez'];
            foreach($mensal as $r):
                $fat = (float)($r['FATURAMENTO']??0);
                $com = (float)($r['COMISSAO']??0);
            ?>
            <tr>
                <td><?= $meses[(int)($r['MES']??0)] ?>/<?= $r['ANO']??'' ?></td>
                <td class="text-end"><?= $fat>0?$fmt($fat):'—' ?></td>
                <td class="text-end fw-bold text-accent"><?= $com>0?$fmt($com):'—' ?></td>
                <td class="text-end"><?= $fat>0?number_format($com/$fat*100,1,',','.').'%':'—' ?></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>
