<?php
$u=current_user();$isSuper=is_super_admin();
if(!$isSuper&&!is_role(['company','employee'])){http_response_code(403);exit('Sem permissão.');}
$companyId=$isSuper?(int)request('company_id',0):resolve_company_id_for_user($u);
if($isSuper&&!$companyId){$rows=db()->query("SELECT id FROM companies WHERE status='active' ORDER BY id")->fetchAll(PDO::FETCH_COLUMN);foreach($rows as $cid){if(company_is_gas((int)$cid)){$companyId=(int)$cid;break;}}}
if($companyId<1||!company_is_gas($companyId)){http_response_code(403);exit('Distribuidora de gás não encontrada.');}
$period=(string)request('period','all');if(!in_array($period,['today','month','all'],true))$period='all';
$where="o.company_id=? AND COALESCE(p.is_gas_cylinder,0)=1";$params=[$companyId];
if($period==='today'){$where.=' AND date(COALESCE(o.updated_at,o.created_at))=?';$params[]=today();}
elseif($period==='month'){$where.=' AND substr(COALESCE(o.updated_at,o.created_at),1,7)=?';$params[]=substr(today(),0,7);}
$q=db()->prepare("SELECT o.id,o.order_number,o.status,o.payment_status,o.total,o.created_at,o.updated_at,u.name client_name,u.username client_username,u.public_id client_public_id,u.phone client_phone,SUM(oi.qty) gas_qty,SUM(oi.total) gas_total,GROUP_CONCAT(oi.name||' x '||oi.qty,' • ') items FROM orders o JOIN order_items oi ON oi.order_id=o.id JOIN products p ON p.id=oi.product_id LEFT JOIN users u ON u.id=o.client_id WHERE $where GROUP BY o.id ORDER BY o.id DESC");$q->execute($params);$rows=$q->fetchAll();
$total=0;$qty=0;foreach($rows as $r){$total+=(float)$r['gas_total'];$qty+=(float)$r['gas_qty'];}
$context=$isSuper?'&company_id='.$companyId:'';$periodLabel=$period==='today'?'Hoje':($period==='month'?'Este mês':'Todo o histórico');
?>
<section class="page-intro"><div><span class="mini-label">🔥 DISTRIBUIDORA DE GÁS</span><h2>Histórico de vendas</h2><p><?= e($periodLabel) ?> • <?= count($rows) ?> pedido(s) • <?= number_format($qty,0,',','.') ?> botijão(ões).</p></div><div class="actions"><a class="btn btn-secondary" href="dashboard.php?page=gas_admin<?= e($context) ?>">← Painel de gás</a><a class="btn btn-secondary" href="dashboard.php?page=orders<?= e($context) ?>">Pedidos</a></div></section>
<section class="panel"><div class="panel-header"><div><h3><?= e($periodLabel) ?></h3><p>Total em produtos de gás: <?= money($total) ?></p></div><div class="actions"><a class="btn btn-secondary btn-sm" href="dashboard.php?page=gas_history&period=today<?= e($context) ?>">Hoje</a><a class="btn btn-secondary btn-sm" href="dashboard.php?page=gas_history&period=month<?= e($context) ?>">Mês</a><a class="btn btn-secondary btn-sm" href="dashboard.php?page=gas_history&period=all<?= e($context) ?>">Tudo</a></div></div>
<div class="table-wrap"><table><thead><tr><th>Pedido</th><th>Cliente</th><th>Produtos</th><th>Qtd.</th><th>Valor gás</th><th>Pagamento</th><th>Status</th><th>Data</th></tr></thead><tbody>
<?php if(!$rows):?><tr><td colspan="8"><div class="empty-state"><strong>Nenhuma venda de gás neste período</strong></div></td></tr><?php endif;?>
<?php foreach($rows as $row):?><tr><td><strong><?= e($row['order_number']) ?></strong></td><td><?= e(user_public_name(['username'=>$row['client_username'],'public_id'=>$row['client_public_id'],'name'=>$row['client_name']])) ?><small style="display:block"><?= e(format_phone($row['client_phone'])) ?></small></td><td><?= e($row['items']?:'—') ?></td><td><?= e(number_format((float)$row['gas_qty'],0,',','.')) ?></td><td><?= money((float)$row['gas_total']) ?></td><td><?= status_badge($row['payment_status']) ?></td><td><?= status_badge($row['status']) ?></td><td><?= e(date('d/m/Y H:i',strtotime($row['updated_at']?:$row['created_at']))) ?></td></tr><?php endforeach;?>
</tbody></table></div></section>
