<?php
require_once __DIR__ . '/_report_common.php';
[$from,$to] = get_range();

$stmt = $pdo->prepare("
  SELECT
    a.code, a.name, a.type,
    SUM(jl.debit)  AS total_debit,
    SUM(jl.credit) AS total_credit,
    SUM(jl.debit - jl.credit) AS balance
  FROM journal_lines jl
  JOIN journal_entries je ON je.id = jl.entry_id
  JOIN accounts a ON a.id = jl.account_id
  WHERE jl.company_id = ? AND je.status='POSTED' AND je.entry_date BETWEEN ? AND ?
  GROUP BY a.id
  ORDER BY a.code
");
$stmt->execute([$cid,$from,$to]);
$rows = $stmt->fetchAll();

$sumDr=0; $sumCr=0;
foreach($rows as $r){ $sumDr += (float)$r['total_debit']; $sumCr += (float)$r['total_credit']; }

function page_actions(){ ?>
  <a class="btn btn-outline-secondary btn-sm" href="<?= url('/reports/trial_balance.php') ?>"><i class="bi bi-arrow-counterclockwise"></i> إعادة</a>
<?php }

render_layout("ميزان المراجعة", "الرئيسية <span class='mx-1'>/</span> التقارير <span class='mx-1'>/</span> ميزان المراجعة", function() use ($rows,$from,$to,$sumDr,$sumCr) {
?>
<div class="card cardx mt-3">
  <div class="p-3">
    <form class="row g-2 align-items-end" method="get">
      <div class="col-12 col-md-4">
        <label class="form-label small text-secondary">من</label>
        <?= render_date_input('from', $from) ?>
      </div>
      <div class="col-12 col-md-4">
        <label class="form-label small text-secondary">إلى</label>
        <?= render_date_input('to', $to) ?>
      </div>
      <div class="col-12 col-md-4 d-flex gap-2">
        <button class="btn btn-dark w-100"><i class="bi bi-funnel"></i> تطبيق</button>
      </div>
    </form>
  </div>

  <div class="table-responsive px-3 pb-3">
    <table class="table align-middle mb-0">
      <thead>
        <tr class="text-secondary small">
          <th>الكود</th>
          <th>الحساب</th>
          <th>النوع</th>
          <th class="text-end">مدين</th>
          <th class="text-end">دائن</th>
          <th class="text-end">الرصيد</th>
        </tr>
      </thead>
      <tbody>
        <?php if(!$rows): ?>
          <tr><td colspan="6" class="text-center text-secondary py-4">لا توجد بيانات</td></tr>
        <?php else: foreach($rows as $r): ?>
          <tr>
            <td class="text-secondary"><?= h($r['code']) ?></td>
            <td class="fw-semibold"><?= h($r['name']) ?></td>
            <td class="text-secondary small"><?= h($r['type']) ?></td>
            <td class="text-end"><?= number_format((float)$r['total_debit'],2) ?></td>
            <td class="text-end"><?= number_format((float)$r['total_credit'],2) ?></td>
            <td class="text-end fw-semibold"><?= number_format((float)$r['balance'],2) ?></td>
          </tr>
        <?php endforeach; endif; ?>
      </tbody>
      <tfoot>
        <tr>
          <th colspan="3" class="text-end">الإجمالي</th>
          <th class="text-end"><?= number_format($sumDr,2) ?></th>
          <th class="text-end"><?= number_format($sumCr,2) ?></th>
          <th class="text-end"><?= number_format($sumDr-$sumCr,2) ?></th>
        </tr>
      </tfoot>
    </table>
  </div>
</div>
<?php }); ?>
