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

$accId = (int)($_GET['account_id'] ?? 0);

$accountsStmt = $pdo->prepare("SELECT id, code, name FROM accounts WHERE company_id=? AND is_active=1 ORDER BY code");
$accountsStmt->execute([$cid]);
$accounts = $accountsStmt->fetchAll();

$lines = [];
if($accId){
  $stmt = $pdo->prepare("
    SELECT je.entry_date, je.ref, je.memo, jl.description, jl.debit, jl.credit
    FROM journal_lines jl
    JOIN journal_entries je ON je.id=jl.entry_id
    WHERE jl.company_id=? AND je.status='POSTED'
      AND jl.account_id=? AND je.entry_date BETWEEN ? AND ?
    ORDER BY je.entry_date, jl.id
  ");
  $stmt->execute([$cid,$accId,$from,$to]);
  $lines = $stmt->fetchAll();
}

render_layout("دفتر الأستاذ", "الرئيسية <span class='mx-1'>/</span> التقارير <span class='mx-1'>/</span> دفتر الأستاذ", function() use ($accounts,$accId,$lines,$from,$to) {
?>
<div class="card cardx mt-3 p-3">
  <form class="row g-2 align-items-end" method="get">
    <div class="col-12 col-xl-5">
      <label class="form-label small text-secondary">الحساب</label>
      <select class="selectpicker" data-style="form-select" data-live-search="true" data-size="8"  required name="account_id">
        <option value="">— اختر حساب —</option>
        <?php foreach($accounts as $a): ?>
          <option value="<?= (int)$a['id'] ?>" <?= $accId===(int)$a['id'] ? 'selected':'' ?>>
            <?= h($a['code'].' - '.$a['name']) ?>
          </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-6 col-xl-3">
      <label class="form-label small text-secondary">من</label>
      <?= render_date_input('from', $from) ?>
    </div>
    <div class="col-6 col-xl-3">
      <label class="form-label small text-secondary">إلى</label>
      <?= render_date_input('to', $to) ?>
    </div>
    <div class="col-12 col-xl-1">
      <button class="btn btn-dark w-100"><i class="bi bi-funnel"></i></button>
    </div>
  </form>
</div>

<div class="card cardx mt-3">
  <div class="p-3">
    <div class="fw-bold">الحركات</div>
    <div class="text-secondary small">تظهر بعد اختيار حساب</div>
  </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>
        </tr>
      </thead>
      <tbody>
        <?php if(!$accId): ?>
          <tr><td colspan="5" class="text-center text-secondary py-4">اختر حسابًا لعرض دفتر الأستاذ</td></tr>
        <?php elseif(!$lines): ?>
          <tr><td colspan="5" class="text-center text-secondary py-4">لا توجد حركات</td></tr>
        <?php else: foreach($lines as $l): ?>
          <tr>
            <td class="text-secondary"><?= h($l['entry_date']) ?></td>
            <td class="text-secondary"><?= h($l['ref'] ?? '-') ?></td>
            <td class="small"><?= h(($l['memo'] ?? '') . ' ' . ($l['description'] ?? '')) ?></td>
            <td class="text-end"><?= number_format((float)$l['debit'],2) ?></td>
            <td class="text-end"><?= number_format((float)$l['credit'],2) ?></td>
          </tr>
        <?php endforeach; endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?php }); ?>
