<?php include APP_PATH.'/views/partials/report_header.php'; ?>

<div class="rpt-btn-bar no-print">
  <?php $sc=[['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')]];
  foreach($sc as[$l,$f,$t]):$a=$from===$f&&$to===$t;?>
  <a href="?from=<?=$f?>&to=<?=$t?>" class="btn btn-sm <?=$a?'btn-primary':'btn-outline-secondary'?> py-0 px-2"><?=$l?></a>
  <?php endforeach;?>
  <form method="GET" action="<?=APP_URL?>/reports/profit-loss" class="d-flex gap-2 align-items-end mt-2 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>
    <button class="btn btn-secondary btn-sm align-self-end">Filter</button>
  </form>
</div>

<!-- P&L Statement -->
<div class="card mb-3 no-break">
  <div class="card-header fw-semibold small">
    Profit &amp; Loss Statement &nbsp;|&nbsp; <?=fDate($from,'d M Y')?> – <?=fDate($to,'d M Y')?>
  </div>
  <div class="table-responsive">
    <table class="table table-sm mb-0">
      <tbody>
        <tr style="background:#e8edf5"><td colspan="3" class="fw-bold py-2 ps-3" style="text-transform:uppercase;letter-spacing:.5px;font-size:.75rem;color:#0f2557">Revenue</td></tr>
        <tr><td class="ps-4 py-1">Gross Sales Revenue</td><td></td><td class="text-end pe-3 fw-semibold text-success" style="width:150px"><?=currency($revenue)?></td></tr>
        <tr><td class="ps-4 py-1 text-danger">Less: Discounts Given</td><td></td><td class="text-end pe-3 text-danger">(<?=currency($discounts)?>)</td></tr>
        <tr class="fw-bold" style="background:#f5f7fb"><td class="ps-3 py-1">Net Revenue</td><td></td><td class="text-end pe-3"><?=currency($revenue-$discounts)?></td></tr>

        <tr style="background:#e8edf5"><td colspan="3" class="fw-bold py-2 ps-3" style="text-transform:uppercase;letter-spacing:.5px;font-size:.75rem;color:#0f2557">Cost of Goods Sold</td></tr>
        <tr><td class="ps-4 py-1 text-danger">Cost of Products Sold (COGS)</td><td></td><td class="text-end pe-3 text-danger">(<?=currency($cogs)?>)</td></tr>
        <tr class="fw-bold" style="border-top:2px solid #0f2557;background:#f5f7fb">
          <td class="ps-3 py-2">Gross Profit</td>
          <td class="text-muted small"><?=$grossMarginPct?>% margin</td>
          <td class="text-end pe-3 <?=$grossProfit>=0?'text-success':'text-danger'?>" style="font-size:11pt"><?=currency(abs($grossProfit))?><?=$grossProfit<0?' (Loss)':''?></td>
        </tr>

        <tr style="background:#e8edf5"><td colspan="3" class="fw-bold py-2 ps-3" style="text-transform:uppercase;letter-spacing:.5px;font-size:.75rem;color:#0f2557">Operating Expenses</td></tr>
        <?php foreach($expByCategory as$ec):?>
        <tr><td class="ps-4 py-1"><?=e($ec['category_name'])?> <small class="text-muted">(<?=$ec['count']?> items)</small></td><td></td><td class="text-end pe-3 text-danger">(<?=currency($ec['total'])?>)</td></tr>
        <?php endforeach;?>
        <tr style="background:#f5f7fb" class="fw-bold"><td class="ps-3 py-1">Total Expenses</td><td></td><td class="text-end pe-3 text-danger">(<?=currency($expenses)?>)</td></tr>

        <tr style="background:#e8edf5"><td colspan="3" class="fw-bold py-2 ps-3" style="text-transform:uppercase;letter-spacing:.5px;font-size:.75rem;color:#0f2557"><?=setting('tax_name','Tax')?></td></tr>
        <tr><td class="ps-4 py-1"><?=setting('tax_name','VAT')?> Collected</td><td></td><td class="text-end pe-3 text-warning"><?=currency($tax)?></td></tr>

        <tr style="background:#0f2557;color:#fff" class="fw-bold">
          <td class="ps-3 py-3" style="font-size:11pt">NET PROFIT / (LOSS)</td>
          <td style="font-size:9pt;opacity:.8"><?=$netMarginPct?>% net margin</td>
          <td class="text-end pe-3" style="font-size:12pt;color:<?=$netProfit>=0?'#4ade80':'#f87171'?>">
            <?=currency(abs($netProfit))?><?=$netProfit<0?' (LOSS)':''?>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

<!-- By Category -->
<?php if(!empty($byCategory)):?>
<div class="card no-break">
  <div class="card-header fw-semibold small">Gross Profit by Product Category</div>
  <div class="table-responsive">
    <table class="table table-sm mb-0">
      <thead class="table-dark"><tr><th>#</th><th>Category</th><th class="text-end">Qty Sold</th><th class="text-end">Revenue</th><th class="text-end">Cost</th><th class="text-end">Gross Profit</th><th class="text-end">Margin %</th></tr></thead>
      <tbody>
        <?php foreach($byCategory as$i=>$c): $m=$c['revenue']>0?round($c['profit']/$c['revenue']*100,1):0;?>
        <tr>
          <td class="text-muted"><?=$i+1?></td>
          <td class="fw-semibold"><?=e($c['category_name'])?></td>
          <td class="text-end"><?=number_format($c['qty_sold'],2)?></td>
          <td class="text-end"><?=currency($c['revenue'])?></td>
          <td class="text-end text-danger"><?=currency($c['cost'])?></td>
          <td class="text-end fw-bold <?=$c['profit']>=0?'text-success':'text-danger'?>"><?=currency($c['profit'])?></td>
          <td class="text-end"><span class="badge <?=$m>=20?'bg-success':($m>=10?'bg-warning':'bg-danger')?>"><?=$m?>%</span></td>
        </tr>
        <?php endforeach;?>
      </tbody>
      <tfoot class="table-secondary fw-bold">
        <tr><td colspan="3">TOTAL</td>
          <td class="text-end"><?=currency(array_sum(array_column($byCategory,'revenue')))?></td>
          <td class="text-end text-danger"><?=currency(array_sum(array_column($byCategory,'cost')))?></td>
          <td class="text-end text-success"><?=currency(array_sum(array_column($byCategory,'profit')))?></td>
          <td class="text-end"><?=$grossMarginPct?>%</td>
        </tr>
      </tfoot>
    </table>
  </div>
</div>
<?php endif;?>