<?php $this->start('body'); ?>
<?php $r = $this->data['r']; ?>

<style>
  @media print { .no-print { display: none !important; } body { background: #fff; } }
  .payslip { max-width: 700px; margin: 20px auto; background: #fff; padding: 28px 36px; font-family: 'Open Sans', Arial, sans-serif; }
  .payslip h1 { color: #2626F5; letter-spacing: 1px; margin: 0; }
  .payslip .header-row { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 2px solid #2626F5; padding-bottom: 12px; margin-bottom: 20px; }
  .payslip .meta { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; margin-bottom: 20px; font-size: 13px; }
  .payslip .meta .lab { color: #666; }
  .payslip table { width: 100%; border-collapse: collapse; margin-bottom: 16px; }
  .payslip th, .payslip td { padding: 6px 10px; border-bottom: 1px solid #eee; font-size: 13px; }
  .payslip thead th { background: #2626F5; color: #fff; text-transform: uppercase; font-size: 11px; }
  .payslip .money { text-align: right; font-variant-numeric: tabular-nums; }
  .payslip .totals { margin-left: auto; width: 60%; font-size: 14px; }
  .payslip .totals .net { font-weight: bold; font-size: 16px; border-top: 2px solid #2626F5; }
  .payslip .footer { border-top: 1px solid #ddd; margin-top: 20px; padding-top: 12px; color: #666; font-size: 11px; text-align: center; }
</style>

<div class="no-print p-3 text-end">
  <button class="btn btn-sm btn-primary" onclick="window.print();"><i class="fas fa-print me-1"></i>Print</button>
  <a href="<?= PROOT; ?>Payroll/view/<?= (int)$r['period_id']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<div class="payslip">
  <div class="header-row">
    <div>
      <h1><?= htmlspecialchars(defined('COMPANY_NAME') ? COMPANY_NAME : 'Pacific Logistics'); ?></h1>
      <div style="color:#666;font-size:12px;">Payslip · <?= humanDate($r['period_start']); ?> – <?= humanDate($r['period_end']); ?></div>
    </div>
    <div style="text-align:right;">
      <div style="font-family:monospace;font-size:14px;font-weight:bold;"><?= htmlspecialchars($r['period_no']); ?></div>
      <div style="color:#666;font-size:12px;">Status: <?= htmlspecialchars(ucfirst($r['status'])); ?></div>
    </div>
  </div>

  <div class="meta">
    <div><span class="lab">Employee:</span> <strong><?= htmlspecialchars($r['full_name']); ?></strong></div>
    <div><span class="lab">Employee no:</span> <?= htmlspecialchars($r['employee_no'] ?: '—'); ?></div>
    <div><span class="lab">Phone:</span> <?= htmlspecialchars($r['phone_1'] ?: '—'); ?></div>
    <div><span class="lab">Trip count:</span> <?= (int)$r['trip_count']; ?> · Revenue TZS <?= number_format((float)$r['trip_revenue'], 0); ?></div>
    <?php if ($r['bank_account_no']): ?><div><span class="lab">Bank:</span> <?= htmlspecialchars($r['bank_account_no']); ?></div><?php endif; ?>
    <?php if ($r['mobile_money_msisdn']): ?><div><span class="lab">Mobile:</span> <?= htmlspecialchars($r['mobile_money_msisdn']); ?></div><?php endif; ?>
  </div>

  <table>
    <thead><tr><th>Earnings</th><th class="money">TZS</th></tr></thead>
    <tbody>
      <tr><td>Base salary</td><td class="money"><?= number_format((float)$r['base_salary'], 2); ?></td></tr>
      <?php if ((float)$r['commission'] > 0): ?><tr><td>Commission (<?= number_format((float)$r['trip_revenue'], 0); ?> × commission rate)</td><td class="money"><?= number_format((float)$r['commission'], 2); ?></td></tr><?php endif; ?>
      <?php if ((float)$r['allowances'] > 0): ?><tr><td>Allowances</td><td class="money"><?= number_format((float)$r['allowances'], 2); ?></td></tr><?php endif; ?>
      <?php if ((float)$r['other_earnings'] > 0): ?><tr><td>Other earnings</td><td class="money"><?= number_format((float)$r['other_earnings'], 2); ?></td></tr><?php endif; ?>
      <tr style="border-top:2px solid #2626F5;font-weight:bold;"><td>Gross pay</td><td class="money"><?= number_format((float)$r['gross_pay'], 2); ?></td></tr>
    </tbody>
  </table>

  <table>
    <thead><tr><th>Deductions</th><th class="money">TZS</th></tr></thead>
    <tbody>
      <tr><td>PAYE</td><td class="money"><?= number_format((float)$r['paye'], 2); ?></td></tr>
      <tr><td>NSSF (employee)</td><td class="money"><?= number_format((float)$r['nssf_ee'], 2); ?></td></tr>
      <?php if ((float)$r['health_ins'] > 0): ?><tr><td>Health insurance</td><td class="money"><?= number_format((float)$r['health_ins'], 2); ?></td></tr><?php endif; ?>
      <?php if ((float)$r['other_deductions'] > 0): ?><tr><td>Other deductions</td><td class="money"><?= number_format((float)$r['other_deductions'], 2); ?></td></tr><?php endif; ?>
      <tr style="border-top:2px solid #2626F5;font-weight:bold;"><td>Total deductions</td><td class="money"><?= number_format((float)$r['paye'] + (float)$r['nssf_ee'] + (float)$r['health_ins'] + (float)$r['other_deductions'], 2); ?></td></tr>
    </tbody>
  </table>

  <table class="totals">
    <tr><td>Gross pay</td><td class="money"><?= number_format((float)$r['gross_pay'], 2); ?></td></tr>
    <tr><td>less Deductions</td><td class="money">(<?= number_format((float)$r['paye'] + (float)$r['nssf_ee'] + (float)$r['health_ins'] + (float)$r['other_deductions'], 2); ?>)</td></tr>
    <tr class="net"><td>NET PAY</td><td class="money">TZS <?= number_format((float)$r['net_pay'], 2); ?></td></tr>
  </table>

  <div style="margin-top: 16px;font-size:11px;color:#666;">
    <strong>Employer contributions</strong> (informational — not deducted from your pay): NSSF (10%) TZS <?= number_format((float)$r['nssf_er'], 2); ?> · SDL (3.5%) TZS <?= number_format((float)$r['sdl'], 2); ?> · WCF (0.5%) TZS <?= number_format((float)$r['wcf'], 2); ?>
  </div>

  <div class="footer">
    Generated <?= date('d M Y H:i'); ?> · This is a system-generated payslip · Contact HR for any query.
  </div>
</div>

<?php $this->end(); ?>
