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

/** Render a section of {label / amount} rows with a subtotal. */
$section = function (string $title, array $rows, float $total) use ($asOf) { ?>
  <div class="mb-3">
    <h6 class="text-uppercase text-muted small mb-2" style="letter-spacing:.06em;"><?= $title; ?></h6>
    <table class="table table-sm mb-0">
      <tbody>
        <?php if (empty($rows)): ?>
          <tr><td colspan="2" class="text-center text-muted small py-2">— none —</td></tr>
        <?php else: ?>
          <?php foreach ($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=1970-01-01&to=<?= $asOf; ?>"
                   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; ?>
        <tr class="fw-semibold border-top">
          <td>Total <?= strtolower($title); ?></td>
          <td class="money font-monospace"><?= number_format($total, 2); ?></td>
        </tr>
      </tbody>
    </table>
  </div>
<?php };
?>

<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-building-columns me-2 text-muted"></i>Balance Sheet</h5>
    <div class="text-muted small">As of <?= humanDate($asOf); ?></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>

<form method="GET" class="card border-0 shadow-sm mb-3">
  <div class="card-body py-2 d-flex flex-wrap gap-2 align-items-end">
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">As of</label>
      <input type="date" name="as_of" value="<?= htmlspecialchars($asOf); ?>" class="form-control form-control-sm">
    </div>
    <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-sync me-1"></i>Refresh</button>
    <div class="ms-auto d-flex gap-1">
      <?php
        $today = new DateTimeImmutable('today');
        foreach ([
          'Today'         => $today->format('Y-m-d'),
          'End of last M' => $today->modify('last day of last month')->format('Y-m-d'),
          'End of year'   => $today->format('Y-12-31'),
        ] as $lab => $d):
      ?>
        <a class="btn btn-sm btn-outline-secondary" href="<?= PROOT; ?>FinancialReports/balanceSheet?as_of=<?= $d; ?>"><?= $lab; ?></a>
      <?php endforeach; ?>
    </div>
  </div>
</form>

<div class="row g-3">
  <div class="col-lg-6">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body">
        <?php $section('Non-current assets', $r['non_current_assets'], $r['total_nca']); ?>
        <?php $section('Current assets',     $r['current_assets'],     $r['total_ca']); ?>
        <div class="border-top pt-2 mt-2 d-flex justify-content-between align-items-center">
          <div class="text-uppercase fw-bold">Total Assets</div>
          <div class="money font-monospace fs-5 fw-bold">TZS <?= number_format($r['total_assets'], 2); ?></div>
        </div>
      </div>
    </div>
  </div>
  <div class="col-lg-6">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body">
        <?php $section('Current liabilities',     $r['current_liab'],     $r['total_cl']); ?>
        <?php $section('Non-current liabilities', $r['non_current_liab'], $r['total_ncl']); ?>

        <div class="mb-3">
          <h6 class="text-uppercase text-muted small mb-2" style="letter-spacing:.06em;">Equity</h6>
          <table class="table table-sm mb-0">
            <tbody>
              <?php if (empty($r['equity'])): ?>
                <tr><td colspan="2" class="text-center text-muted small py-2">— no equity accounts —</td></tr>
              <?php else: ?>
                <?php foreach ($r['equity'] 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=1970-01-01&to=<?= $asOf; ?>" 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; ?>
              <tr>
                <td>
                  <span class="font-monospace small text-muted me-2">P&amp;L</span>
                  Net income (current period, unclosed)
                </td>
                <td class="money font-monospace"><?= number_format($r['net_income'], 2); ?></td>
              </tr>
              <tr class="fw-semibold border-top">
                <td>Total equity</td>
                <td class="money font-monospace"><?= number_format($r['total_equity'], 2); ?></td>
              </tr>
            </tbody>
          </table>
        </div>

        <div class="border-top pt-2 mt-2 d-flex justify-content-between align-items-center">
          <div class="text-uppercase fw-bold">Total Liabilities + Equity</div>
          <div class="money font-monospace fs-5 fw-bold">TZS <?= number_format($r['total_liab_eq'], 2); ?></div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="card border-0 shadow-sm mt-3">
  <div class="card-body py-2 small">
    <?php if ($r['balanced']): ?>
      <span class="badge bg-success"><i class="fas fa-check me-1"></i>Balanced</span>
      Assets equal Liabilities + Equity + current-period Net Income.
    <?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_assets'] - $r['total_liab_eq']), 2); ?>.
      Investigate before closing the period &mdash; check the Trial Balance for the source.
    <?php endif; ?>
  </div>
</div>
<?php $this->end(); ?>
