<?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'];

$date_from = $_GET['from'] ?? date('Y-m-01');
$date_to   = $_GET['to'] ?? date('Y-m-t');

$stmt = $pdo->prepare("SELECT * FROM journal_entries WHERE company_id=? AND entry_date BETWEEN ? AND ? ORDER BY id DESC");
$stmt->execute([$cid,$date_from,$date_to]);
$rows = $stmt->fetchAll();

render_layout("القيود اليومية", "الرئيسية <span class='mx-1'>/</span> الحسابات <span class='mx-1'>/</span> القيود اليومية", function() use ($rows,$date_from,$date_to) {
?>
<div class="card cardx mt-3">
  <div class="p-3 d-flex align-items-center justify-content-between flex-wrap gap-2">
    <div>
      <div class="fw-bold">القيود اليومية</div>
      <div class="text-secondary small">تم إنشاؤها تلقائيًا من الوارد/المنصرف</div>
    </div>
  </div>

  <div class="px-3 pb-3">
    <form class="row g-2 align-items-end mb-2" method="get">
      <div class="col-12 col-md-4">
        <label class="form-label small text-secondary">من</label>
        <?php echo render_date_input('from', $date_from); ?>
      </div>
      <div class="col-12 col-md-4">
        <label class="form-label small text-secondary">إلى</label>
        <?php echo render_date_input('to', $date_to); ?>
      </div>
      <div class="col-12 col-md-4 d-flex gap-2">
        <button class="btn btn-outline-secondary w-100"><i class="bi bi-funnel"></i> فلترة</button>
        <a class="btn btn-outline-dark w-100" href="<?= url('/journal/index.php') ?>"><i class="bi bi-arrow-counterclockwise"></i> إعادة</a>
      </div>
    </form>

    <div class="table-responsive">
      <table class="table align-middle mb-0">
        <thead>
          <tr class="text-secondary small">
            <th>#</th>
            <th>التاريخ</th>
            <th>المرجع</th>
            <th>المصدر</th>
            <th>الحالة</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"><?= (int)$r['id'] ?></td>
              <td class="text-secondary"><?= h($r['entry_date']) ?></td>
              <td class="fw-semibold"><?= h($r['ref'] ?? '-') ?></td>
              <td class="text-secondary"><?= h($r['source']) ?></td>
              <td><span class="badge bg-success-subtle text-success border border-success-subtle"><?= h($r['status']) ?></span></td>
              <td class="text-end">
                <a class="btn btn-outline-dark btn-sm" href="<?= url('/journal/view.php?id='.(int)$r['id']) ?>"><i class="bi bi-eye"></i></a>
                <a class="btn btn-outline-warning btn-sm" href="<?= url('/journal/reverse_entry.php?id='.(int)$r['id']) ?>" onclick="return confirm('تأكيد إلغاء القيد؟ سيتم إنشاء قيد عكسي');" title="إلغاء القيد">
                  <i class="bi bi-arrow-counterclockwise"></i>
                </a>
              </td>
            </tr>
          <?php endforeach; endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
<?php }); ?>
