<?php
$balance = (float)$opening;
?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">كشف حساب بنك</h4>
  <?php if ($bank_account_id > 0): ?>
    <div class="btn-group">
      <a class="btn btn-outline-primary" target="_blank" href="<?= e(url('/reports/finance/bank-statement/print?bank_account_id='.(int)$bank_account_id.'&from='.urlencode($from).'&to='.urlencode($to).'&q='.urlencode($q).'&format=a4')) ?>"><i class="fa-solid fa-print"></i> طباعة</a>
      <a class="btn btn-outline-danger" href="<?= e(url('/reports/finance/bank-statement/pdf?bank_account_id='.(int)$bank_account_id.'&from='.urlencode($from).'&to='.urlencode($to).'&q='.urlencode($q))) ?>">PDF</a>
      <a class="btn btn-outline-success" href="<?= e(url('/reports/finance/bank-statement/export?bank_account_id='.(int)$bank_account_id.'&from='.urlencode($from).'&to='.urlencode($to).'&q='.urlencode($q))) ?>">Excel</a>
    </div>
  <?php endif; ?>
</div>
<form class="card filter-card p-3 mb-3" method="get">
  <div class="row g-2 align-items-end">
    <div class="col-md-3">
      <label class="form-label">الحساب البنكي</label>
      <select class="form-select" name="bank_account_id" required>
        <option value="0">اختر الحساب</option>
        <?php foreach ($bankAccounts as $a): ?><option value="<?= (int)$a['id'] ?>" <?= ((int)$bank_account_id===(int)$a['id']?'selected':'') ?>><?= e($a['name']) ?></option><?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-2"><label class="form-label">من</label><input type="date" class="form-control" name="from" value="<?= e($from) ?>"></div>
    <div class="col-md-2"><label class="form-label">إلى</label><input type="date" class="form-control" name="to" value="<?= e($to) ?>"></div>
    <div class="col-md-3"><label class="form-label">بحث</label><input type="text" class="form-control" name="q" value="<?= e($q) ?>" placeholder="مرجع / ملاحظات"></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($bank_statement_url ?? '/reports/finance/bank-statement')) ?>">مسح</a></div>
  </div>
</form>
<?php if ($account): ?>
<div class="row g-3 mb-3">
  <div class="col-md-3"><div class="card summary-card p-3"><div class="text-muted small">الحساب</div><div class="fw-bold"><?= e($account['name']) ?></div></div></div>
  <div class="col-md-3"><div class="card summary-card p-3"><div class="text-muted small">رصيد أول المدة</div><div class="fs-5 fw-bold"><?= e(nf($opening)) ?></div></div></div>
  <div class="col-md-2"><div class="card summary-card p-3"><div class="text-muted small">إجمالي داخل</div><div class="fs-5 fw-bold text-success"><?= e(nf($totals['in'])) ?></div></div></div>
  <div class="col-md-2"><div class="card summary-card p-3"><div class="text-muted small">إجمالي خارج</div><div class="fs-5 fw-bold text-danger"><?= e(nf($totals['out'])) ?></div></div></div>
  <div class="col-md-2"><div class="card summary-card p-3"><div class="text-muted small">الرصيد الختامي</div><div class="fs-5 fw-bold"><?= e(nf($closing)) ?></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>الخزنة</th><th>المرجع</th><th>الملاحظات</th><th class="text-end">داخل</th><th class="text-end">خارج</th><th class="text-end">الرصيد</th></tr></thead>
      <tbody>
        <tr><td colspan="7">رصيد أول المدة</td><td class="text-end fw-bold"><?= e(nf($balance)) ?></td></tr>
        <?php if (empty($rows)): ?><tr><td colspan="8" class="text-center text-muted py-4">لا توجد حركات خلال الفترة</td></tr><?php endif; ?>
        <?php foreach ($rows as $r): ?>
          <?php $in = ($r['trx_type']==='in') ? (float)$r['amount'] : 0.0; $out = ($r['trx_type']==='out') ? (float)$r['amount'] : 0.0; $balance += $in - $out; ?>
          <tr>
            <td><?= e(date('Y-m-d H:i', strtotime((string)$r['trx_date']))) ?></td>
            <td><?= e(reference_type_label((string)($r['source_type'] ?? ''))) ?></td>
            <td><?= e($r['cashbox_name'] ?: '-') ?></td>
            <td><?= e(human_reference_label((string)($r['source_type'] ?? ''), (int)($r['source_id'] ?? 0), (string)($r['ref_no'] ?? ''))) ?></td>
            <td><?= e(trim((string)($r['notes'] ?? '')) ?: human_reference_label((string)($r['source_type'] ?? ''), (int)($r['source_id'] ?? 0), (string)($r['ref_no'] ?? ''))) ?></td>
            <td class="text-end"><?= $in ? e(nf($in)) : '-' ?></td>
            <td class="text-end"><?= $out ? e(nf($out)) : '-' ?></td>
            <td class="text-end fw-bold"><?= e(nf($balance)) ?></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
<?php else: ?>
<div class="card p-4 text-center text-muted">اختر الحساب البنكي والفترة ثم اضغط عرض.</div>
<?php endif; ?>
