<?php
require_once __DIR__ . '/../app/helpers.php';
require_once __DIR__ . '/../app/db.php';
require_once __DIR__ . '/../app/layout.php';
require_login();

$pdo = db();
$cid = (int)$_SESSION['company_id'];
$id = (int)($_GET['id'] ?? 0);

$stmt = $pdo->prepare("SELECT * FROM journal_entries WHERE id=? AND company_id=? LIMIT 1");
$stmt->execute([$id,$cid]);
$je = $stmt->fetch();
if(!$je){ http_response_code(404); die("Not found"); }

$stmt = $pdo->prepare("
  SELECT jl.*, a.code, a.name
  FROM journal_lines jl
  JOIN accounts a ON a.id=jl.account_id
  WHERE jl.entry_id=? AND jl.company_id=?
  ORDER BY jl.line_no
");
$stmt->execute([$id,$cid]);
$lines = $stmt->fetchAll();

$totDr = 0; $totCr = 0;
foreach($lines as $l){ $totDr += (float)$l['debit']; $totCr += (float)$l['credit']; }

render_layout("تفاصيل قيد #".$id, "الرئيسية <span class='mx-1'>/</span> الحسابات <span class='mx-1'>/</span> القيود اليومية <span class='mx-1'>/</span> تفاصيل", function() use ($je,$lines,$totDr,$totCr) {
?>
<div class="row g-3 mt-3">
  <div class="col-12">
    <div class="card cardx p-3">
      <div class="d-flex align-items-center justify-content-between flex-wrap gap-2">
        <div>
          <div class="fw-bold">قيد يومية</div>
          <div class="text-secondary small">
            تاريخ: <?= h($je['entry_date']) ?> • مرجع: <?= h($je['ref'] ?? '-') ?> • مصدر: <?= h($je['source']) ?>
          </div>
        </div>
        <a class="btn btn-outline-secondary btn-sm" href="<?= url('/journal/index.php') ?>"><i class="bi bi-arrow-right"></i> رجوع</a>
      </div>

      <div class="table-responsive mt-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 foreach($lines as $l): ?>
              <tr>
                <td class="text-secondary"><?= (int)$l['line_no'] ?></td>
                <td class="small"><span class="fw-semibold"><?= h($l['code']) ?></span> — <?= h($l['name']) ?></td>
                <td class="text-secondary small"><?= h($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; ?>
          </tbody>
          <tfoot>
            <tr>
              <th colspan="3" class="text-end">الإجمالي</th>
              <th class="text-end"><?= number_format($totDr,2) ?></th>
              <th class="text-end"><?= number_format($totCr,2) ?></th>
            </tr>
          </tfoot>
        </table>
      </div>

      <?php if(abs($totDr-$totCr) > 0.009): ?>
        <div class="alert alert-warning mt-3">⚠️ القيد غير متوازن (مدين ≠ دائن) — راجع البيانات.</div>
      <?php endif; ?>
    </div>
  </div>
</div>
<?php }); ?>
