<?php
$typeLabels = [
  'deposit' => 'إيداع',
  'withdraw' => 'سحب',
  'cash_to_bank' => 'تحويل من خزنة إلى بنك',
  'bank_to_cash' => 'تحويل من بنك إلى خزنة',
  'customer_receipt' => 'سند قبض',
  'cash_payment' => 'سند صرف',
];
$balance = (float)($opening ?? 0);
$accountName = (string)($account['name'] ?? '');
$bankName = (string)($account['bank_name'] ?? '');
$accountNo = (string)($account['account_no'] ?? '');
$currency = (string)($account['currency_code'] ?? 'EGP');
?>
<section class="print-section keep-together">
  <div class="hero-grid bank-hero-grid">
    <div class="panel">
      <div class="panel-head"><span>بيانات الحساب البنكي</span><small>Bank Account</small></div>
      <div class="panel-body">
        <div class="detail-grid bank-detail-grid">
          <div class="detail-item"><div class="detail-label">الحساب البنكي</div><div class="detail-value"><?= e($accountName) ?></div></div>
          <div class="detail-item"><div class="detail-label">البنك</div><div class="detail-value"><?= e($bankName !== '' ? $bankName : '-') ?></div></div>
          <div class="detail-item"><div class="detail-label">رقم الحساب</div><div class="detail-value"><?= e($accountNo !== '' ? $accountNo : '-') ?></div></div>
          <div class="detail-item"><div class="detail-label">العملة</div><div class="detail-value"><?= e($currency !== '' ? $currency : 'EGP') ?></div></div>
          <div class="detail-item"><div class="detail-label">الفترة من</div><div class="detail-value"><?= e($from ?: '—') ?></div></div>
          <div class="detail-item"><div class="detail-label">الفترة إلى</div><div class="detail-value"><?= e($to ?: '—') ?></div></div>
        </div>
      </div>
    </div>
    <div class="summary-card bank-summary-card">
      <div class="summary-head">ملخص الحركة</div>
      <table>
        <tr><td class="label">رصيد أول المدة</td><td class="val"><?= e(format_money($opening ?? 0)) ?></td></tr>
        <tr><td class="label">إجمالي دائن</td><td class="val"><?= e(format_money($totals['in'] ?? 0)) ?></td></tr>
        <tr><td class="label">إجمالي مدين</td><td class="val"><?= e(format_money($totals['out'] ?? 0)) ?></td></tr>
        <tr class="grand"><td class="label">الرصيد الختامي</td><td class="val"><?= e(format_money($closing ?? 0)) ?></td></tr>
      </table>
    </div>
  </div>
</section>

<section class="print-section">
  <div class="section-heading">تفاصيل حركات البنك</div>
  <div class="doc-table-wrap">
    <table class="table table-compact bank-table">
      <thead>
        <tr>
          <th style="width:92px">التاريخ</th>
          <th style="width:118px">النوع</th>
          <th>المرجع</th>
          <th>الملاحظات</th>
          <th style="width:88px">دائن</th>
          <th style="width:88px">مدين</th>
          <th style="width:96px">الرصيد</th>
        </tr>
      </thead>
      <tbody>
        <tr class="bank-opening-row"><td colspan="6" class="strong text-center">رصيد أول المدة</td><td class="text-center strong"><?= e(format_money($balance)) ?></td></tr>
        <?php foreach (($rows ?? []) as $r): ?>
          <?php
            $in = (($r['trx_type'] ?? '') === 'in') ? (float)($r['amount'] ?? 0) : 0.0;
            $out = (($r['trx_type'] ?? '') === 'out') ? (float)($r['amount'] ?? 0) : 0.0;
            $balance += $in - $out;
            $trxDate = (string)($r['trx_date'] ?? '');
            $trxDateLabel = $trxDate !== '' ? date('Y-m-d H:i', strtotime($trxDate)) : '-';
          ?>
          <tr>
            <td class="text-center"><?= e($trxDateLabel) ?></td>
            <td class="strong text-center"><?= e($typeLabels[$r['source_type']] ?? (($r['trx_type'] ?? '') === 'in' ? 'دائن' : 'مدين')) ?></td>
            <td class="text-center"><?= e($r['ref_no'] ?: '-') ?></td>
            <td class="text-center"><?= e($r['notes'] ?: '-') ?></td>
            <td class="text-center ltr-number"><?= $in ? e(format_money($in)) : '—' ?></td>
            <td class="text-center ltr-number"><?= $out ? e(format_money($out)) : '—' ?></td>
            <td class="text-center strong ltr-number"><?= e(format_money($balance)) ?></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</section>
