<?php
require_once __DIR__ . '/../config/config.php';
require_permission('accounting.reports_view');

$asOf = $_GET['as_of'] ?? date('Y-m-d');
$accounts = get_chart_of_accounts($pdo);

$rows = [];
$totalDebit = 0; $totalCredit = 0;
foreach ($accounts as $a) {
    $balance = get_account_balance($pdo, $a['id'], $a['normal_balance'], $asOf);
    if (abs($balance) < 0.005) continue; // skip zero-balance accounts
    $debit = $a['normal_balance'] === 'debit' ? max($balance, 0) : max(-$balance, 0);
    $credit = $a['normal_balance'] === 'credit' ? max($balance, 0) : max(-$balance, 0);
    $totalDebit += $debit;
    $totalCredit += $credit;
    $rows[] = ['account' => $a, 'debit' => $debit, 'credit' => $credit];
}

$pageTitle = 'Trial Balance';
include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<h1 class="text-2xl font-bold mb-6">Trial Balance</h1>

<form method="GET" class="flex flex-wrap items-end gap-3 mb-6 bg-white rounded-xl shadow-sm border border-slate-200 p-4">
  <div>
    <label class="text-xs text-slate-500 block mb-1">As of Date</label>
    <input type="date" name="as_of" value="<?= e($asOf) ?>" class="border border-slate-300 rounded-lg px-3 py-2 text-sm">
  </div>
  <button class="bg-blue-700 hover:bg-blue-800 text-white text-sm font-medium px-4 py-2 rounded-lg">View</button>
</form>

<div class="overflow-x-auto">
<div class="bg-white rounded-xl shadow-sm border border-slate-200">
  <table class="w-full text-sm">
    <thead class="bg-slate-50 text-slate-500 text-xs uppercase"><tr>
      <th class="text-left px-5 py-2">Code</th><th class="text-left px-5 py-2">Account</th>
      <th class="text-left px-5 py-2">Debit</th><th class="text-left px-5 py-2">Credit</th></tr></thead>
    <tbody>
    <?php foreach ($rows as $r): ?>
      <tr class="border-t border-slate-100">
        <td class="px-5 py-2.5 font-mono text-xs"><?= e($r['account']['account_code']) ?></td>
        <td class="px-5 py-2.5"><?= e($r['account']['description']) ?></td>
        <td class="px-5 py-2.5"><?= $r['debit'] > 0 ? '₦'.number_format($r['debit'],2) : '' ?></td>
        <td class="px-5 py-2.5"><?= $r['credit'] > 0 ? '₦'.number_format($r['credit'],2) : '' ?></td>
      </tr>
    <?php endforeach; ?>
    <?php if (empty($rows)): ?><tr><td colspan="4" class="px-5 py-6 text-center text-slate-400">No account activity as of this date.</td></tr><?php endif; ?>
    </tbody>
    <tfoot><tr class="border-t border-slate-200 font-semibold bg-slate-50">
      <td colspan="2" class="px-5 py-2.5 text-right">Total</td>
      <td class="px-5 py-2.5">₦<?= number_format($totalDebit,2) ?></td>
      <td class="px-5 py-2.5">₦<?= number_format($totalCredit,2) ?></td>
    </tr></tfoot>
  </table>
</div>
</div>
<?php if (round($totalDebit,2) !== round($totalCredit,2)): ?>
  <p class="text-red-600 text-sm mt-3">⚠ Debits and credits do not match — check for unbalanced manual entries.</p>
<?php else: ?>
  <p class="text-emerald-600 text-sm mt-3">✓ Debits and credits balance.</p>
<?php endif; ?>
<?php include __DIR__ . '/../includes/footer.php'; ?>
