<?php include APP_PATH.'/views/partials/report_header.php'; ?>
<div class="rpt-btn-bar no-print">
  <div class="d-flex gap-2 flex-wrap mb-2">
    <?php foreach([['Today',date('Y-m-d'),date('Y-m-d')],['This Month',date('Y-m-01'),date('Y-m-d')],['Last Month',date('Y-m-01',strtotime('first day of last month')),date('Y-m-t',strtotime('last month'))],['This Year',date('Y-01-01'),date('Y-m-d')]] as[$l,$f,$t]):$a=$from===$f&&$to===$t;?>
    <a href="?from=<?=$f?>&to=<?=$t?>&group=<?=e($groupBy)?>" class="btn btn-sm <?=$a?'btn-primary':'btn-outline-secondary'?> py-0 px-2"><?=$l?></a>
    <?php endforeach;?>
  </div>
  <form method="GET" action="<?=APP_URL?>/reports/tax" class="d-flex gap-2 align-items-end mt-1 filter-form">
    <div><label class="form-label small mb-1">From</label><input type="date" name="from" class="form-control form-control-sm" value="<?=e($from)?>"></div>
    <div><label class="form-label small mb-1">To</label><input type="date" name="to" class="form-control form-control-sm" value="<?=e($to)?>"></div>
    <div><label class="form-label small mb-1">Group By</label><select name="group" class="form-select form-select-sm"><option value="day" <?=$groupBy==='day'?'selected':''?>>Daily</option><option value="week" <?=$groupBy==='week'?'selected':''?>>Weekly</option><option value="month" <?=$groupBy==='month'?'selected':''?>>Monthly</option></select></div>
    <button class="btn btn-secondary btn-sm align-self-end">Filter</button>
  </form>
</div>

<div class="row g-2 mb-3 no-break">
  <div class="col-6 col-md-3"><div class="card text-center py-3"><div class="fw-bold fs-5"><?=number_format($totalInvoices)?></div><small class="text-muted">Invoices</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3 border-primary"><div class="fw-bold text-primary"><?=currency($totalRevenue)?></div><small class="text-muted">Gross Revenue</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3 border-danger"><div class="fw-bold text-danger"><?=currency($totalDiscount)?></div><small class="text-muted">Discounts</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3 border-warning"><div class="fw-bold fs-5 text-warning"><?=currency($totalTax)?></div><small class="text-muted"><?=e($taxName)?> Collected</small></div></div>
</div>

<div class="card mb-3 no-break">
  <div class="card-header fw-semibold small"><?=e($taxName)?> Breakdown — <?=ucfirst($groupBy)?> &nbsp;|&nbsp; <?=fDate($from,'d M Y')?> – <?=fDate($to,'d M Y')?></div>
  <div class="table-responsive"><table class="table table-sm mb-0">
    <thead class="table-dark"><tr><th>#</th><th>Period</th><th class="text-end">Invoices</th><th class="text-end">Revenue</th><th class="text-end">Discount</th><th class="text-end fw-bold"><?=e($taxName)?> Collected</th><th class="text-end">% Share</th></tr></thead>
    <tbody>
      <?php foreach($rows as$i=>$r): $pct=$totalTax>0?round($r['tax_collected']/$totalTax*100,1):0;?>
      <tr><td class="text-muted"><?=$i+1?></td><td class="fw-semibold"><?=e($r['period'])?></td><td class="text-end"><?=number_format($r['invoices'])?></td><td class="text-end"><?=currency($r['revenue'])?></td><td class="text-end text-danger"><?=currency($r['discount'])?></td><td class="text-end fw-bold text-warning"><?=currency($r['tax_collected'])?></td><td class="text-end small"><?=$pct?>%</td></tr>
      <?php endforeach; if(empty($rows)):?><tr><td colspan="7" class="text-center text-muted py-4">No data.</td></tr><?php endif;?>
    </tbody>
    <tfoot class="table-secondary fw-bold"><tr><td colspan="2">TOTAL</td><td class="text-end"><?=number_format($totalInvoices)?></td><td class="text-end"><?=currency($totalRevenue)?></td><td class="text-end text-danger"><?=currency($totalDiscount)?></td><td class="text-end text-warning"><?=currency($totalTax)?></td><td class="text-end">100%</td></tr></tfoot>
  </table></div>
</div>

<?php if(!empty($byRate)||!empty($byCategory)):?>
<div class="row g-3">
  <?php if(!empty($byRate)):?>
  <div class="col-md-5 no-break"><div class="card h-100"><div class="card-header fw-semibold small">By Tax Rate</div>
    <div class="table-responsive"><table class="table table-sm mb-0">
      <thead class="table-dark"><tr><th>Rate</th><th class="text-end">Lines</th><th class="text-end">Revenue</th><th class="text-end"><?=e($taxName)?></th></tr></thead>
      <tbody><?php foreach($byRate as$r):?><tr><td><span class="badge bg-warning text-dark"><?=number_format((float)$r['tax_percent'],1)?>%</span></td><td class="text-end"><?=number_format($r['line_count'])?></td><td class="text-end"><?=currency($r['revenue'])?></td><td class="text-end fw-bold text-warning"><?=currency($r['tax_collected'])?></td></tr><?php endforeach;?></tbody>
      <tfoot class="table-secondary fw-bold"><tr><td colspan="3">TOTAL</td><td class="text-end text-warning"><?=currency(array_sum(array_column($byRate,'tax_collected')))?></td></tr></tfoot>
    </table></div>
  </div></div>
  <?php endif;?>
  <?php if(!empty($byCategory)):?>
  <div class="col-md-7 no-break"><div class="card h-100"><div class="card-header fw-semibold small">By Product Category</div>
    <div class="table-responsive"><table class="table table-sm mb-0">
      <thead class="table-dark"><tr><th>Category</th><th class="text-end">Invoices</th><th class="text-end">Revenue</th><th class="text-end"><?=e($taxName)?></th></tr></thead>
      <tbody><?php foreach($byCategory as$c):?><tr><td class="fw-semibold small"><?=e($c['category_name'])?></td><td class="text-end small"><?=number_format($c['invoice_count'])?></td><td class="text-end"><?=currency($c['revenue'])?></td><td class="text-end fw-bold text-warning"><?=currency($c['tax_collected'])?></td></tr><?php endforeach;?></tbody>
      <tfoot class="table-secondary fw-bold"><tr><td colspan="3">TOTAL</td><td class="text-end text-warning"><?=currency(array_sum(array_column($byCategory,'tax_collected')))?></td></tr></tfoot>
    </table></div>
  </div></div>
  <?php endif;?>
</div>
<?php endif;?>