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

$accounts = get_chart_of_accounts($pdo);
$accountId = (int)($_GET['account_id'] ?? ($accounts[0]['id'] ?? 0));
$from = $_GET['from'] ?? date('Y-m-01');
$to = $_GET['to'] ?? date('Y-m-d');

$account = null;
foreach ($accounts as $a) if ($a['id'] == $accountId) $account = $a;

$lines = [];
$openingBalance = 0;
if ($account) {
    $openingBalance = get_account_balance($pdo, $accountId, $account['normal_balance'], date('Y-m-d', strtotime($from . ' -1 day')));

    $stmt = $pdo->prepare("SELECT jel.*, je.entry_number, je.entry_date, je.description entry_desc
                            FROM journal_entry_lines jel
                            JOIN journal_entries je ON je.id = jel.entry_id
                            WHERE jel.account_id = ? AND je.status='posted' AND je.entry_date BETWEEN ? AND ?
                            ORDER BY je.entry_date, je.id");
    $stmt->execute([$accountId, $from, $to]);
    $lines = $stmt->fetchAll();
}

$pageTitle = 'General Ledger';
include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<h1 class="text-2xl font-bold mb-6">General Ledger</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 class="flex-1 min-w-[200px]">
    <label class="text-xs text-slate-500 block mb-1">Account</label>
    <select name="account_id" class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm">
      <?php foreach ($accounts as $a): ?>
        <option value="<?= $a['id'] ?>" <?= $a['id']==$accountId?'selected':'' ?>><?= e($a['account_code']) ?> - <?= e($a['description']) ?></option>
      <?php endforeach; ?>
    </select>
  </div>
  <div>
    <label class="text-xs text-slate-500 block mb-1">From</label>
    <input type="date" name="from" value="<?= e($from) ?>" class="border border-slate-300 rounded-lg px-3 py-2 text-sm">
  </div>
  <div>
    <label class="text-xs text-slate-500 block mb-1">To</label>
    <input type="date" name="to" value="<?= e($to) ?>" 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>

<?php if ($account): $running = $openingBalance; ?>
<div class="overflow-x-auto">
<div class="bg-white rounded-xl shadow-sm border border-slate-200">
  <div class="px-5 py-4 border-b border-slate-200 font-semibold"><?= e($account['account_code']) ?> - <?= e($account['description']) ?>
    <span class="text-xs text-slate-400 font-normal capitalize">(<?= e($account['normal_balance']) ?> normal)</span>
  </div>
  <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">Date</th><th class="text-left px-5 py-2">Entry No.</th>
      <th class="text-left px-5 py-2">Description</th><th class="text-left px-5 py-2">Debit</th>
      <th class="text-left px-5 py-2">Credit</th><th class="text-left px-5 py-2">Balance</th></tr></thead>
    <tbody>
      <tr class="border-t border-slate-100 bg-slate-50 font-medium">
        <td class="px-5 py-2.5" colspan="5">Opening Balance</td>
        <td class="px-5 py-2.5">₦<?= number_format($openingBalance,2) ?></td>
      </tr>
      <?php foreach ($lines as $l):
          $running += $account['normal_balance']==='debit' ? ($l['debit'] - $l['credit']) : ($l['credit'] - $l['debit']);
      ?>
      <tr class="border-t border-slate-100">
        <td class="px-5 py-2.5"><?= date('M j, Y', strtotime($l['entry_date'])) ?></td>
        <td class="px-5 py-2.5"><a href="../journal_entries/view.php?id=<?= $l['entry_id'] ?>" class="text-blue-600 hover:underline"><?= e($l['entry_number']) ?></a></td>
        <td class="px-5 py-2.5 text-slate-500"><?= e($l['description'] ?: $l['entry_desc']) ?></td>
        <td class="px-5 py-2.5"><?= $l['debit'] > 0 ? '₦'.number_format($l['debit'],2) : '' ?></td>
        <td class="px-5 py-2.5"><?= $l['credit'] > 0 ? '₦'.number_format($l['credit'],2) : '' ?></td>
        <td class="px-5 py-2.5 font-medium">₦<?= number_format($running,2) ?></td>
      </tr>
      <?php endforeach; ?>
      <?php if (empty($lines)): ?><tr><td colspan="6" class="px-5 py-6 text-center text-slate-400">No activity in this period.</td></tr><?php endif; ?>
    </tbody>
    <tfoot><tr class="border-t border-slate-200 font-semibold bg-slate-50">
      <td colspan="5" class="px-5 py-2.5 text-right">Closing Balance</td>
      <td class="px-5 py-2.5">₦<?= number_format($running,2) ?></td>
    </tr></tfoot>
  </table>
</div>
</div>
<?php endif; ?>
<?php include __DIR__ . '/../includes/footer.php'; ?>
