<?php
// vars: $cashboxes, $cashbox_id, $from, $to, $openBal, $endBal, $inTotal, $outTotal, $tx
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">كشف حركة خزنة</h4>
  <a class="btn btn-outline-secondary" href="<?= e(url($cashboxes_url ?? '/finance/cashboxes')) ?>">الخزن</a>
</div>

<form class="card filter-card p-3 mb-3" method="get">
  <div class="row g-2 align-items-end">
    <div class="col-md-4">
      <label class="form-label">الخزنة</label>
      <select class="form-select" name="cashbox_id">
        <?php foreach ($cashboxes as $cb): ?>
          <option value="<?= (int)$cb['id'] ?>" <?= ((int)$cashbox_id===(int)$cb['id']?'selected':'') ?>><?= e($cb['name']) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-3">
      <label class="form-label">من</label>
      <input type="date" class="form-control" name="from" value="<?= e($from) ?>">
    </div>
    <div class="col-md-3">
      <label class="form-label">إلى</label>
      <input type="date" class="form-control" name="to" value="<?= e($to) ?>">
    </div>
    <div class="col-md-2 d-flex gap-2">
      <button class="btn btn-outline-primary w-100" type="submit">عرض</button>
      <a class="btn btn-outline-secondary" href="<?= e(url($cashbox_ledger_url ?? '/finance/cashboxes/ledger')) ?>">مسح</a>
    </div>
  </div>
</form>

<div class="row g-3 mb-3">
  <div class="col-md-3">
    <div class="card summary-card p-3">
      <div class="text-muted">رصيد أول المدة</div>
      <div class="fs-4 fw-bold"><?= e(nf($openBal)) ?></div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card summary-card p-3">
      <div class="text-muted">إجمالي داخل</div>
      <div class="fs-4 fw-bold"><?= e(nf($inTotal)) ?></div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card summary-card p-3">
      <div class="text-muted">إجمالي خارج</div>
      <div class="fs-4 fw-bold"><?= e(nf($outTotal)) ?></div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card summary-card p-3">
      <div class="text-muted">رصيد آخر المدة</div>
      <div class="fs-4 fw-bold"><?= e(nf($endBal)) ?></div>
    </div>
  </div>
</div>

<div class="card content-card p-3">
  <div class="table-responsive">
    <table class="table table-sm table-bordered align-middle mb-0">
      <thead class="table-light">
        <tr>
          <th>التاريخ</th>
          <th>نوع</th>
          <th class="text-end">داخل</th>
          <th class="text-end">خارج</th>
          <th>مرجع</th>
          <th>ملاحظات</th>
          <th class="text-end">رصيد</th>
        </tr>
      </thead>
      <tbody>
        <tr class="table-secondary">
          <td colspan="6" class="fw-bold">رصيد أول المدة</td>
          <td class="text-end fw-bold"><?= e(nf($openBal)) ?></td>
        </tr>

        <?php if (empty($tx)): ?>
          <tr><td colspan="7" class="text-center text-muted py-4">لا توجد حركات خلال الفترة</td></tr>
        <?php endif; ?>

        <?php foreach ($tx as $r): ?>
          <tr>
            <td><?= e(date('Y-m-d H:i', strtotime((string)$r['trx_date']))) ?></td>
            <td><span class="badge <?= $r['trx_type']==='in' ? 'text-bg-success' : 'text-bg-danger' ?>"><?= e(reference_type_label((string)($r['ref_type'] ?? ($r['trx_type']==='in' ? 'deposit' : 'withdraw')))) ?></span></td>
            <td class="text-end"><?= $r['trx_type']==='in' ? e(nf($r['amount'])) : '' ?></td>
            <td class="text-end"><?= $r['trx_type']==='out' ? e(nf($r['amount'])) : '' ?></td>
            <td><?= e(human_reference_label((string)($r['ref_type'] ?? ''), (int)($r['ref_id'] ?? 0), (string)($r['ref_no'] ?? ''))) ?></td>
            <td><?= e(trim((string)($r['notes'] ?? '')) ?: human_reference_label((string)($r['ref_type'] ?? ''), (int)($r['ref_id'] ?? 0), (string)($r['ref_no'] ?? ''))) ?></td>
            <td class="text-end fw-bold"><?= e(nf($r['running'])) ?></td>
          </tr>
        <?php endforeach; ?>

        <tr class="table-secondary">
          <td colspan="6" class="fw-bold">رصيد آخر المدة</td>
          <td class="text-end fw-bold"><?= e(nf($endBal)) ?></td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
