<?php /* payroll/report.php */ include APP_PATH.'/views/partials/report_header.php'; ?>
<div class="rpt-btn-bar no-print">
  <form method="GET" class="d-flex gap-2 align-items-end">
    <div><label class="form-label small fw-semibold">Year</label>
      <select name="year" class="form-select form-select-sm">
        <?php for($y=date('Y')+1;$y>=date('Y')-4;$y--):?><option value="<?=$y?>" <?=$y===$year?'selected':''?>><?=$y?></option><?php endfor;?>
      </select>
    </div>
    <button class="btn btn-secondary btn-sm align-self-end">Filter</button>
  </form>
</div>
<div class="row g-2 mb-3">
  <div class="col-md-3"><div class="card text-center py-3 border-primary"><div class="fw-bold"><?=currency(array_sum(array_column($monthly,'total_gross')))?></div><small class="text-muted">Total Gross <?=$year?></small></div></div>
  <div class="col-md-3"><div class="card text-center py-3 border-success"><div class="fw-bold text-success"><?=currency(array_sum(array_column($monthly,'total_net')))?></div><small class="text-muted">Total Net <?=$year?></small></div></div>
  <div class="col-md-3"><div class="card text-center py-3"><div class="fw-bold"><?=currency(array_sum(array_column($monthly,'total_epf_emp')))?></div><small class="text-muted">EPF Employee</small></div></div>
  <div class="col-md-3"><div class="card text-center py-3"><div class="fw-bold"><?=currency(array_sum(array_column($monthly,'total_epf_er'))+array_sum(array_column($monthly,'total_etf')))?></div><small class="text-muted">EPF+ETF Employer</small></div></div>
</div>
<div class="card mb-3">
  <div class="card-header fw-semibold small">Monthly Payroll Summary — <?=$year?></div>
  <table class="table table-sm mb-0">
    <thead class="table-dark"><tr><th>Month</th><th class="text-center">Staff</th><th class="text-end">Gross</th><th class="text-end">Deductions</th><th class="text-end">Net</th><th class="text-end">EPF Emp</th><th class="text-end">EPF Er</th><th class="text-end">ETF</th><th class="text-center">Status</th></tr></thead>
    <tbody>
      <?php foreach($monthly as $m):$sc=['draft'=>'bg-secondary','processing'=>'bg-warning text-dark','approved'=>'bg-info text-dark','paid'=>'bg-success'][$m['status']]??'bg-secondary';?>
      <tr>
        <td class="fw-semibold"><?=e($m['period_name'])?></td>
        <td class="text-center"><?=$m['emp_count']?></td>
        <td class="text-end"><?=currency($m['total_gross'])?></td>
        <td class="text-end text-danger"><?=currency($m['total_gross']-$m['total_net'])?></td>
        <td class="text-end fw-bold text-success"><?=currency($m['total_net'])?></td>
        <td class="text-end small"><?=currency($m['total_epf_emp'])?></td>
        <td class="text-end small"><?=currency($m['total_epf_er'])?></td>
        <td class="text-end small"><?=currency($m['total_etf'])?></td>
        <td class="text-center"><span class="badge <?=$sc?>" style="font-size:.6rem"><?=ucfirst($m['status'])?></span></td>
      </tr>
      <?php endforeach;?>
      <?php if(empty($monthly)):?><tr><td colspan="9" class="text-center text-muted py-3">No payroll data for <?=$year?></td></tr><?php endif;?>
    </tbody>
    <tfoot class="table-secondary fw-bold">
      <tr><td>TOTAL</td><td></td>
        <td class="text-end"><?=currency(array_sum(array_column($monthly,'total_gross')))?></td>
        <td class="text-end"><?=currency(array_sum(array_column($monthly,'total_gross'))-array_sum(array_column($monthly,'total_net')))?></td>
        <td class="text-end text-success"><?=currency(array_sum(array_column($monthly,'total_net')))?></td>
        <td class="text-end"><?=currency(array_sum(array_column($monthly,'total_epf_emp')))?></td>
        <td class="text-end"><?=currency(array_sum(array_column($monthly,'total_epf_er')))?></td>
        <td class="text-end"><?=currency(array_sum(array_column($monthly,'total_etf')))?></td>
        <td></td>
      </tr>
    </tfoot>
  </table>
</div>
<div class="card">
  <div class="card-header fw-semibold small">Employee Payroll Summary — <?=$year?></div>
  <table class="table table-sm mb-0">
    <thead class="table-dark"><tr><th>#</th><th>Employee</th><th>Designation</th><th>Department</th><th class="text-center">Months</th><th class="text-end">Total Net</th></tr></thead>
    <tbody>
      <?php foreach($employees as $i=>$e):?>
      <tr><td class="text-muted"><?=$i+1?></td><td class="fw-semibold small"><?=e($e['full_name'])?> <code style="font-size:.62rem"><?=e($e['emp_code'])?></code></td>
        <td class="small text-muted"><?=e($e['designation']??'—')?></td><td class="small text-muted"><?=e($e['department']??'—')?></td>
        <td class="text-center small"><?=$e['months_paid']?></td><td class="text-end fw-bold text-success"><?=currency($e['total_net'])?></td></tr>
      <?php endforeach;?>
    </tbody>
    <tfoot class="table-secondary fw-bold"><tr><td colspan="5">TOTAL</td><td class="text-end text-success"><?=currency(array_sum(array_column($employees,'total_net')))?></td></tr></tfoot>
  </table>
</div>