<?php $this->start('body'); ?>
<?php
$acc = $this->data['acc']; $rows = $this->data['rows'];
$from = $this->data['from']; $to = $this->data['to'];
$this->data['form_action'] = PROOT . 'FinancialReports/ledger/' . (int)$acc['c0d'];

$runDebit = $runCredit = 0.0;
?>

<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-book me-2 text-muted"></i>Ledger</h5>
    <div class="text-muted small">
      <span class="font-monospace me-2"><?= htmlspecialchars($acc['account_code']); ?></span>
      <?= htmlspecialchars($acc['account_name']); ?>
      <span class="badge bg-light text-muted ms-2"><?= htmlspecialchars($acc['account_type']); ?></span>
      &middot; <?= humanDate($from); ?> &rarr; <?= humanDate($to); ?>
    </div>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= PROOT; ?>FinancialReports/trialBalance?from=<?= $from; ?>&to=<?= $to; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Trial Balance</a>
  </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;">Date</th>
          <th style="width:140px;">JE</th>
          <th style="width:100px;">Source</th>
          <th>Narrative</th>
          <th class="money" style="width:120px;">Debit</th>
          <th class="money" style="width:120px;">Credit</th>
          <th class="money" style="width:140px;">Running</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($rows)): ?>
          <tr><td colspan="7" class="text-center text-muted py-4">No postings against this account in the range.</td></tr>
        <?php else: ?>
          <?php foreach ($rows as $l): ?>
            <?php
              $runDebit  += (float)$l['debit'];
              $runCredit += (float)$l['credit'];
              $running    = $runDebit - $runCredit;
              $narr = $l['line_narrative'] ?: $l['je_narrative'];
            ?>
            <tr>
              <td class="small"><?= humanDate($l['entry_date']); ?></td>
              <td class="font-monospace small"><?= htmlspecialchars($l['je_no']); ?></td>
              <td class="small text-muted"><?= htmlspecialchars($l['source_type']); ?></td>
              <td class="small"><?= htmlspecialchars($narr); ?></td>
              <td class="money font-monospace small"><?= (float)$l['debit']  > 0 ? number_format($l['debit'],  2) : '—'; ?></td>
              <td class="money font-monospace small"><?= (float)$l['credit'] > 0 ? number_format($l['credit'], 2) : '—'; ?></td>
              <td class="money font-monospace small <?= $running >= 0 ? '' : 'text-danger'; ?>"><?= number_format($running, 2); ?></td>
            </tr>
          <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
      <tfoot class="table-light">
        <tr class="fw-bold">
          <th colspan="4" class="text-uppercase small">Totals</th>
          <th class="money font-monospace"><?= number_format($runDebit, 2); ?></th>
          <th class="money font-monospace"><?= number_format($runCredit, 2); ?></th>
          <th class="money font-monospace"><?= number_format($runDebit - $runCredit, 2); ?></th>
        </tr>
      </tfoot>
    </table>
  </div>
</div>
<?php $this->end(); ?>
