<?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'));
$totalMesas= array_sum(array_column($linhas,'QTD_MESAS'));
?>
<div class="pbi-page-header no-print">
    <h1 class="pbi-page-title"><i class="bi bi-person-workspace me-2 text-accent"></i>Desempenho 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>
        <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' ?>

<script type="application/json" id="relatorio-meta"><?= json_encode([
    'titulo' => 'Desempenho de Garçons',
    'empresa' => $empresa ?? '',
    'periodo' => (isset($filtros['dataIni']) && isset($filtros['dataFim'])) ? ($filtros['dataIni'].' a '.$filtros['dataFim']) : null,
    'totais' => [
        ['label' => 'Total Mesas', 'valor' => number_format($kpis['MESAS_FECHADAS']??0,0,',','.')],
        ['label' => 'Faturamento Total', 'valor' => $fmt($totalFat)],
        ['label' => 'Ticket Médio Geral', 'valor' => $fmt($kpis['TICKET_MEDIO']??0)],
        ['label' => 'Garçons Ativos', 'valor' => count($linhas)],
    ],
], 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">Total Mesas</div><div class="pbi-kpi-value"><?= number_format($kpis['MESAS_FECHADAS']??0,0,',','.') ?></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">Ticket Médio Geral</div><div class="pbi-kpi-value"><?= $fmt($kpis['TICKET_MEDIO']??0) ?></div></div>
    <div class="pbi-kpi-card"><div class="pbi-kpi-label">Garçons Ativos</div><div class="pbi-kpi-value"><?= count($linhas) ?></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"><i class="bi bi-person-workspace me-2"></i>Ranking de Garçons</div>
    <div class="pbi-hint mb-2" style="margin-top:0">Desempenho de cada garçom. <strong>% Part.</strong> = fatia dele no faturamento total; <strong>Ticket Médio</strong> = valor médio por mesa; <strong>Pessoas</strong> = clientes atendidos; <strong>Tx. Serviço</strong> = gorjeta (10%) gerada.</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">Ticket Médio</th>
                <th class="text-end">Pessoas</th>
                <th class="text-end">Tx. Serviço</th>
            </tr></thead>
            <tbody>
            <?php foreach($linhas as $i=>$r): ?>
            <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 fw-bold text-accent"><?= $fmt($r['FATURAMENTO']??0) ?></td>
                <td class="text-end text-muted"><?= $totalFat>0?number_format((float)($r['FATURAMENTO']??0)/$totalFat*100,1,',','.').'%':'—' ?></td>
                <td class="text-end"><?= $fmt($r['TICKET_MEDIO']??0) ?></td>
                <td class="text-end"><?= (int)($r['QTD_PESSOAS']??0) ?></td>
                <td class="text-end"><?= $fmt($r['TX_SERVICO']??0) ?></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="2" class="fw-bold">Total</td>
                <td class="text-end fw-bold"><?= $totalMesas ?></td>
                <td class="text-end fw-bold text-accent"><?= $fmt($totalFat) ?></td>
                <td colspan="4"></td>
            </tr></tfoot>
        </table>
    </div>
</div>

<?php if($tempo): ?>
<div class="pbi-card">
    <div class="pbi-card-header"><i class="bi bi-clock me-2"></i>Tempo Médio de Atendimento</div>
    <div class="pbi-hint mb-2" style="margin-top:0">Tempo que cada garçom leva entre abrir e fechar a mesa. <strong>Tempo Médio</strong> = média em minutos; <strong>Tempo Máx.</strong> = a mesa mais demorada.</div>
    <div class="table-responsive">
        <table class="pbi-table pbi-table-print">
            <thead><tr><th>Garçom</th><th class="text-end">Mesas</th><th class="text-end">Tempo Médio (min)</th><th class="text-end">Tempo Máx.</th></tr></thead>
            <tbody>
            <?php foreach($tempo as $r): ?>
            <tr>
                <td><?= \App\Core\View::e($r['GARCOM']??'—') ?></td>
                <td class="text-end"><?= (int)($r['QTD_MESAS']??0) ?></td>
                <td class="text-end"><?= number_format((float)($r['TEMPO_MEDIO_MIN']??0),0,',','.') ?> min</td>
                <td class="text-end"><?= number_format((float)($r['TEMPO_MAX_MIN']??0),0,',','.') ?> min</td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>
