<div class="content-wrapper android-agents-balances"><main class="agb-main"><header class="agb-heading"><div><span>Agent Balances</span><h1>أرصدة المعتمدين</h1><p>كشف حساب مترابط مع الفواتير والمدفوعات والمبيعات.</p></div><div><i class="fa fa-balance-scale"></i></div></header><nav class="agb-nav"><a href="agents_portal_preview.php"><i class="fa fa-eye"></i><span>المعاينة</span></a><a href="agents.php"><i class="fa fa-users"></i><span>المعتمدون</span></a><a href="agent_transactions.php"><i class="fa fa-book"></i><span>الحركات</span></a><a class="is-active" href="agents_balances.php"><i class="fa fa-balance-scale"></i><span>الأرصدة</span></a></nav><?php if($msg): ?><div class="agb-message"><i class="fa fa-check-circle"></i><?= h($msg) ?></div><?php endif; ?><?php if($err): ?><div class="agb-message is-danger"><i class="fa fa-exclamation-triangle"></i><?= h($err) ?></div><?php endif; ?><section class="agb-stats"><article><span>إجمالي المستحق</span><strong dir="ltr"><?= number_format($totals['due'],2) ?></strong></article><article><span>القيود المدينة</span><strong dir="ltr"><?= number_format($totals['debit'],2) ?></strong></article><article><span>القيود الدائنة</span><strong dir="ltr"><?= number_format($totals['credit'],2) ?></strong></article><article><span>المعتمدون</span><strong><?= (int)$totals['agents'] ?></strong></article></section><details class="agb-adjust"><summary><span><i class="fa fa-money"></i><b>قبض أو تسوية</b><small>تسجيل حركة مباشرة على حساب المعتمد</small></span><i class="fa fa-angle-down"></i></summary><form method="post" data-ua-android-native-layout><?php ua_csrf_field(); ?><label><span>المعتمد</span><select required name="agent_id"><option value="">اختر المعتمد</option><?php foreach($agents as $agent): ?><option value="<?= (int)$agent['id'] ?>"><?= h($agent['full_name']?:$agent['name']) ?> (<?= number_format($agent['ledger_balance'],2) ?>)</option><?php endforeach; ?></select></label><div class="agb-pair"><label><span>المبلغ</span><input required type="number" min="0.01" step="0.01" name="amount"></label><label><span>المرجع</span><input name="reference"></label></div><label><span>البيان</span><textarea name="note" rows="2"></textarea></label><div class="agb-actions"><button name="action" value="receipt"><i class="fa fa-money"></i> قبض</button><button class="is-debit" name="action" value="debit_adjustment"><i class="fa fa-minus-circle"></i> مدينة</button><button class="is-credit" name="action" value="credit_adjustment"><i class="fa fa-plus-circle"></i> دائنة</button></div></form></details><section class="agb-filter"><form method="get"><select name="agent_id"><option value="0">كل المعتمدين</option><?php foreach($agents as $agent): ?><option value="<?= (int)$agent['id'] ?>" <?= $agentId===(int)$agent['id']?'selected':'' ?>><?= h($agent['full_name']?:$agent['name']) ?></option><?php endforeach; ?></select><button><i class="fa fa-eye"></i> عرض الكشف</button></form></section><section class="agb-section"><div class="agb-section-head"><div><span>Ledger</span><h2>كشف الحساب</h2></div><b><?= count($entries) ?></b></div><div class="agb-list"><?php if(!$entries): ?><p class="agb-empty">لا توجد قيود مالية.</p><?php else: foreach($entries as $entry): ?><article class="agb-card <?= $entry['direction']==='DEBIT'?'is-debit':'is-credit' ?>"><header><div><span dir="ltr"><?= h($entry['occurred_at']) ?></span><h3><?= h($entry['agent_name']) ?></h3></div><strong dir="ltr"><?= number_format((float)$entry['amount'],2) ?></strong></header><div class="agb-data"><span><small>النوع</small><b><?= h($entry['entry_type']) ?></b></span><span><small>الاتجاه</small><b><?= h($entry['direction']) ?></b></span><span><small>الرصيد بعد</small><b dir="ltr"><?= number_format((float)$entry['balance_after'],2) ?></b></span></div><?php if($entry['description']): ?><p><?= h($entry['description']) ?></p><?php endif; ?></article><?php endforeach; endif; ?></div></section></main></div>
