<?php
require_once __DIR__ . '/../app/bootstrap.php';
use App\Core\PosService; use App\Core\Database;
PosService::requireAccess();
$vendorId = PosService::currentVendorId();
$register = PosService::defaultRegister($vendorId);
$shift = $register ? PosService::currentShift((int)$register['id'], (int)(current_user()['id'] ?? 0)) : null;
$today = Database::fetchOne('SELECT COUNT(*) sales, COALESCE(SUM(total_amount),0) total, COALESCE(SUM(discount_amount),0) discount, COALESCE(SUM(tax_amount),0) tax FROM pos_sales WHERE vendor_id=? AND DATE(created_at)=CURDATE()', [$vendorId]) ?: ['sales'=>0,'total'=>0,'discount'=>0,'tax'=>0];
$payments = Database::fetchAll('SELECT pp.payment_method, COUNT(*) cnt, COALESCE(SUM(pp.amount),0) total FROM pos_payments pp JOIN pos_sales ps ON ps.id=pp.sale_id WHERE ps.vendor_id=? AND DATE(ps.created_at)=CURDATE() GROUP BY pp.payment_method', [$vendorId]);
$sales = Database::fetchAll('SELECT ps.*,u.name AS cashier_name FROM pos_sales ps LEFT JOIN users u ON u.id=ps.cashier_id WHERE ps.vendor_id=? ORDER BY ps.id DESC LIMIT 30', [$vendorId]);
$pageTitle = 'X/Z Reports | Pro POS';
require_once __DIR__ . '/partials/header.php';
?>
<div class="row g-3 mb-3"><div class="col-md-3"><div class="stat-tile"><div class="small text-muted">Today's Sales</div><strong><?= number_format((int)$today['sales']) ?></strong></div></div><div class="col-md-3"><div class="stat-tile"><div class="small text-muted">Revenue</div><strong><?= money($today['total']) ?></strong></div></div><div class="col-md-3"><div class="stat-tile"><div class="small text-muted">Discount</div><strong><?= money($today['discount']) ?></strong></div></div><div class="col-md-3"><div class="stat-tile"><div class="small text-muted">VAT/TAX</div><strong><?= money($today['tax']) ?></strong></div></div></div>
<div class="row g-3"><div class="col-lg-4"><div class="card pos-card"><div class="card-header bg-white"><strong>Payment Summary</strong></div><div class="table-responsive"><table class="table mb-0"><tbody><?php if(!$payments): ?><?= table_empty(2,'No payments today.') ?><?php else: foreach($payments as $p): ?><tr><td><?= e(ucfirst($p['payment_method'])) ?> <span class="text-muted">(<?= e($p['cnt']) ?>)</span></td><td class="text-end"><?= money($p['total']) ?></td></tr><?php endforeach; endif; ?></tbody></table></div><div class="card-footer bg-white d-flex gap-2"><button class="btn btn-outline-primary btn-sm" onclick="window.print()">Print X Report</button><a class="btn btn-outline-secondary btn-sm" href="<?= url('api/report-export.php?report=pos') ?>">Export CSV</a></div></div></div><div class="col-lg-8"><div class="card pos-card"><div class="card-header bg-white"><strong>Recent POS Sales</strong></div><div class="table-responsive"><table class="table mb-0"><thead><tr><th>Sale No</th><th>Cashier</th><th>Total</th><th>Status</th><th>Date</th><th></th></tr></thead><tbody><?php if(!$sales): ?><?= table_empty(6,'No POS sales yet.') ?><?php else: foreach($sales as $s): ?><tr><td><?= e($s['sale_no']) ?></td><td><?= e($s['cashier_name'] ?? '') ?></td><td><?= money($s['total_amount']) ?></td><td><?= badge($s['sale_status']) ?></td><td><?= e($s['created_at']) ?></td><td><a class="btn btn-sm btn-outline-primary" target="_blank" href="<?= url('pos/receipt.php?id='.(int)$s['id']) ?>">Receipt</a></td></tr><?php endforeach; endif; ?></tbody></table></div></div></div></div>
<?php require_once __DIR__ . '/partials/footer.php'; ?>
