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

<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-scale-balanced me-2 text-muted"></i>Trial Balance</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'; ?>

<div class="card border-0 shadow-sm">
  <div class="table-responsive">
    <table class="table table-sm mb-0 align-middle">
      <thead class="table-light">
        <tr>
          <th style="width:110px;">Code</th>
          <th>Account</th>
          <th style="width:120px;">Type</th>
          <th class="money" style="width:150px;">Debit (TZS)</th>
          <th class="money" style="width:150px;">Credit (TZS)</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($r['rows'])): ?>
          <tr><td colspan="5" class="text-center text-muted py-5">No posted journal activity in this period.</td></tr>
        <?php else: ?>
          <?php foreach ($r['rows'] as $row): ?>
            <?php $a = $row['acc']; ?>
            <tr>
              <td class="font-monospace small"><?= htmlspecialchars($a['account_code']); ?></td>
              <td>
                <a href="<?= PROOT; ?>FinancialReports/ledger/<?= (int)$a['c0d']; ?>?from=<?= $from; ?>&to=<?= $to; ?>"
                   class="text-decoration-none"><?= htmlspecialchars($a['account_name']); ?></a>
              </td>
              <td class="small text-muted"><?= htmlspecialchars($a['account_type']); ?></td>
              <td class="money font-monospace small"><?= $row['debit']  > 0 ? number_format($row['debit'],  2) : '—'; ?></td>
              <td class="money font-monospace small"><?= $row['credit'] > 0 ? number_format($row['credit'], 2) : '—'; ?></td>
            </tr>
          <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
      <tfoot class="table-light">
        <tr class="fw-bold">
          <th colspan="3" class="text-uppercase small">Totals</th>
          <th class="money font-monospace"><?= number_format($r['total_debit'],  2); ?></th>
          <th class="money font-monospace"><?= number_format($r['total_credit'], 2); ?></th>
        </tr>
        <tr>
          <td colspan="5" class="small">
            <?php if ($r['balanced']): ?>
              <span class="badge bg-success"><i class="fas fa-check me-1"></i>Balanced</span>
              Debits equal credits &mdash; the ledger is internally consistent.
            <?php else: ?>
              <span class="badge bg-danger"><i class="fas fa-triangle-exclamation me-1"></i>Out of balance</span>
              Difference: TZS <?= number_format(abs($r['total_debit'] - $r['total_credit']), 2); ?>.
              This means at least one journal entry was posted with unequal debits/credits &mdash;
              investigate before closing the period.
            <?php endif; ?>
          </td>
        </tr>
      </tfoot>
    </table>
  </div>
</div>
<?php $this->end(); ?>
