<?php $this->start('body'); ?>
<?php
$r = $this->data['r'];
$from = $this->data['from']; $to = $this->data['to'];
$this->data['form_action'] = PROOT . 'FinancialReports/pnl';
?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold"><i class="fas fa-hand-holding-dollar me-2 text-muted"></i>Profit &amp; Loss</h5>
    <div class="text-muted small"><?= humanDate($from); ?> &rarr; <?= humanDate($to); ?></div>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= PROOT; ?>FinancialReports" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Reports</a>
    <button class="btn btn-sm btn-outline-primary" onclick="window.print()"><i class="fas fa-print me-1"></i>Print</button>
  </div>
</div>

<?php include __DIR__ . '/_range_form.php'; ?>

<!-- Headline KPI strip -->
<div class="row g-2 mb-3">
  <div class="col-md-4"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Revenue</div>
    <div class="fs-4 fw-bold text-success">TZS <?= number_format($r['revenue_total'], 0); ?></div>
  </div></div></div>
  <div class="col-md-4"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Expenses</div>
    <div class="fs-4 fw-bold text-danger">TZS <?= number_format($r['expense_total'], 0); ?></div>
  </div></div></div>
  <div class="col-md-4"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Net income</div>
    <div class="fs-4 fw-bold <?= $r['net_income'] >= 0 ? 'text-success' : 'text-danger'; ?>">TZS <?= number_format($r['net_income'], 0); ?></div>
    <?php if ($r['revenue_total'] > 0): ?>
      <div class="small text-muted">margin <?= number_format(($r['net_income'] / $r['revenue_total']) * 100, 1); ?>%</div>
    <?php endif; ?>
  </div></div></div>
</div>

<div class="row g-3">
  <!-- Revenue side -->
  <div class="col-lg-6">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body p-0">
        <div class="p-3 pb-2">
          <h6 class="text-uppercase text-muted small mb-0">Revenue</h6>
        </div>
        <table class="table table-sm mb-0">
          <tbody>
            <?php if (empty($r['revenue_rows'])): ?>
              <tr><td colspan="2" class="text-center text-muted py-4">No revenue in this period.</td></tr>
            <?php else: ?>
              <?php foreach ($r['revenue_rows'] as $row): $a = $row['acc']; ?>
                <tr>
                  <td>
                    <span class="font-monospace small text-muted me-2"><?= htmlspecialchars($a['account_code']); ?></span>
                    <a href="<?= PROOT; ?>FinancialReports/ledger/<?= (int)$a['c0d']; ?>?from=<?= $from; ?>&to=<?= $to; ?>"
                       class="text-decoration-none"><?= htmlspecialchars($a['account_name']); ?></a>
                  </td>
                  <td class="money font-monospace"><?= number_format($row['amount'], 2); ?></td>
                </tr>
              <?php endforeach; ?>
            <?php endif; ?>
          </tbody>
          <tfoot class="table-light">
            <tr class="fw-bold">
              <th>Total revenue</th>
              <th class="money font-monospace">TZS <?= number_format($r['revenue_total'], 2); ?></th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>

  <!-- Expense side -->
  <div class="col-lg-6">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body p-0">
        <div class="p-3 pb-2">
          <h6 class="text-uppercase text-muted small mb-0">Expenses</h6>
        </div>
        <table class="table table-sm mb-0">
          <tbody>
            <?php if (empty($r['expense_rows'])): ?>
              <tr><td colspan="2" class="text-center text-muted py-4">No expenses in this period.</td></tr>
            <?php else: ?>
              <?php foreach ($r['expense_rows'] as $row): $a = $row['acc']; ?>
                <tr>
                  <td>
                    <span class="font-monospace small text-muted me-2"><?= htmlspecialchars($a['account_code']); ?></span>
                    <a href="<?= PROOT; ?>FinancialReports/ledger/<?= (int)$a['c0d']; ?>?from=<?= $from; ?>&to=<?= $to; ?>"
                       class="text-decoration-none"><?= htmlspecialchars($a['account_name']); ?></a>
                  </td>
                  <td class="money font-monospace"><?= number_format($row['amount'], 2); ?></td>
                </tr>
              <?php endforeach; ?>
            <?php endif; ?>
          </tbody>
          <tfoot class="table-light">
            <tr class="fw-bold">
              <th>Total expenses</th>
              <th class="money font-monospace">TZS <?= number_format($r['expense_total'], 2); ?></th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>
</div>

<!-- Net income banner -->
<div class="card border-0 shadow-sm mt-3">
  <div class="card-body py-3 d-flex align-items-center justify-content-between">
    <div class="text-uppercase small text-muted" style="letter-spacing:.06em;">Net income for the period</div>
    <div class="fs-3 fw-bold <?= $r['net_income'] >= 0 ? 'text-success' : 'text-danger'; ?>">
      TZS <?= number_format($r['net_income'], 2); ?>
    </div>
  </div>
</div>
<?php $this->end(); ?>
