<?php $paymentTotal=0.0; foreach($rows as $paymentItem) $paymentTotal+=(float)($paymentItem['amount']??0); ?>
<div class="content-wrapper android-finance-payments"><main class="afp-main">
  <header class="afp-heading"><div><span>Payments Ledger</span><h1>الحركة المالية</h1><p>إضافة الدفعات ومراجعة آخر الحركات المرتبطة بالفواتير.</p></div><div class="afp-heading-icon"><i class="fa fa-exchange"></i></div></header>
  <nav class="afp-nav"><a href="finance_reports.php"><i class="fa fa-pie-chart"></i><span>الملخص</span></a><a href="invoices.php"><i class="fa fa-file-text-o"></i><span>الفواتير</span></a><a class="is-active" href="finance_payments.php"><i class="fa fa-exchange"></i><span>الحركة</span></a><a href="purchases_report.php"><i class="fa fa-shopping-bag"></i><span>المشتريات</span></a></nav>
  <?php if($filter_payment_id||$filter_ref!==''||$filter_invoice_id): ?><div class="afp-filter-note"><span>عرض مفلتر<?= $filter_payment_id?' · دفعة #'.(int)$filter_payment_id:'' ?><?= $filter_invoice_id?' · فاتورة #'.(int)$filter_invoice_id:'' ?><?= $filter_ref!==''?' · '.h($filter_ref):'' ?></span><a href="finance_payments.php">إلغاء</a></div><?php endif; ?>
  <?php if($msg): ?><div class="afp-message"><i class="fa fa-check-circle"></i><?= h($msg) ?></div><?php endif; ?><?php if($flash_warning): ?><div class="afp-message is-warning"><i class="fa fa-exclamation-circle"></i><?= h($flash_warning) ?></div><?php endif; ?><?php if($err): ?><div class="afp-message is-danger"><i class="fa fa-exclamation-triangle"></i><?= h($err) ?></div><?php endif; ?>
  <section class="afp-section"><div class="afp-section-head"><div><span>Add Payment</span><h2>إضافة دفعة</h2></div><i class="fa fa-money"></i></div><form method="post" data-ua-android-native-layout><input type="hidden" name="action" value="add_payment"><div class="afp-pair"><label><span>رقم الفاتورة</span><input name="invoice_id" type="number" min="0" value="0" inputmode="numeric"></label><label><span>المبلغ</span><input name="amount" type="number" step="0.01" min="0" required inputmode="decimal"></label></div><label><span>طريقة الدفع</span><select name="method"><option value="cash">نقدي</option><option value="transfer">تحويل</option><option value="card">بطاقة</option><option value="other">أخرى</option></select></label><div class="afp-pair"><label><span>المرجع</span><input name="reference" placeholder="#"></label><label><span>ملاحظة</span><input name="note" placeholder="اختياري"></label></div><button class="afp-save"><i class="fa fa-save"></i> حفظ الدفعة</button></form></section>
  <section class="afp-overview"><div><strong><?= count($rows) ?></strong><span>حركة ظاهرة</span></div><div><strong dir="ltr"><?= number_format($paymentTotal,2) ?></strong><span>إجمالي المبالغ</span></div></section>
  <section class="afp-section"><div class="afp-section-head"><div><span>Recent Payments</span><h2>آخر الدفعات</h2></div><i class="fa fa-history"></i></div><div class="afp-list"><?php if(!$rows): ?><p class="afp-empty">لا توجد بيانات</p><?php else: foreach($rows as $payment): ?><article class="afp-card"><header><div><span>دفعة #<?= (int)$payment['id'] ?> · فاتورة #<?= (int)$payment['invoice_id'] ?></span><strong dir="ltr"><?= number_format((float)$payment['amount'],2) ?></strong></div><b><?= h($payment['method']) ?></b></header><div class="afp-data"><span><small>المرجع</small><b><?= h($payment['reference']?:'—') ?></b></span><span><small>بواسطة</small><b><?= h($payment['created_by']?:'—') ?></b></span><span><small>التاريخ</small><b dir="ltr"><?= h($payment['created_at']) ?></b></span></div><details class="afp-delete"><summary><i class="fa fa-ban"></i> إلغاء الدفعة</summary><form method="post"><input type="hidden" name="action" value="delete_payment"><input type="hidden" name="id" value="<?= (int)$payment['id'] ?>"><p>سيتم حذف هذه الحركة المالية.</p><button>تأكيد الإلغاء</button></form></details></article><?php endforeach; endif; ?></div></section>
</main></div>
