<div class="content-wrapper android-agents-activity"><main class="aga-main">
<header class="aga-heading"><div><span>Agent Activity</span><h1>سجل المعتمدين</h1><p>العمليات الواردة والصادرة والمحلية ونتيجة التنفيذ.</p></div><div><i class="fa fa-history"></i></div></header>
<nav class="aga-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 class="is-active" href="agents_activity.php"><i class="fa fa-history"></i><span>السجل</span></a><a href="agents_balances.php"><i class="fa fa-balance-scale"></i><span>الأرصدة</span></a></nav>
<?php if($msg): ?><div class="aga-message"><i class="fa fa-check-circle"></i><?= h($msg) ?></div><?php endif; ?><?php if($err): ?><div class="aga-message is-danger"><i class="fa fa-exclamation-triangle"></i><?= h($err) ?></div><?php endif; ?>
<details class="aga-filter"><summary><span><i class="fa fa-filter"></i><b>تصفية السجل</b><small><?= count($rows) ?> حركة مطابقة</small></span><i class="fa fa-angle-down"></i></summary><form method="get" data-ua-android-native-layout><label><span>المعتمد</span><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></label><div class="aga-pair"><label><span>الاتجاه</span><select name="direction"><option value="">الكل</option><option value="TO_MAIN" <?= $direction==='TO_MAIN'?'selected':'' ?>>إلى الرئيسية</option><option value="FROM_MAIN" <?= $direction==='FROM_MAIN'?'selected':'' ?>>من الرئيسية</option><option value="LOCAL" <?= $direction==='LOCAL'?'selected':'' ?>>محلي</option></select></label><label><span>النتيجة</span><select name="status"><option value="">الكل</option><option <?= $status==='SUCCESS'?'selected':'' ?>>SUCCESS</option><option <?= $status==='FAILED'?'selected':'' ?>>FAILED</option></select></label></div><label><span>نوع الحدث</span><select name="event_type"><option value="">الكل</option><?php foreach($types as $type): ?><option <?= $eventType===$type?'selected':'' ?>><?= h($type) ?></option><?php endforeach; ?></select></label><button><i class="fa fa-filter"></i> تطبيق</button></form></details>
<section class="aga-section"><div class="aga-section-head"><div><span>Operations</span><h2>العمليات</h2></div><b><?= count($rows) ?></b></div><div class="aga-list"><?php if(!$rows): ?><p class="aga-empty">لا توجد حركات مطابقة.</p><?php else: foreach($rows as $activity): ?><article class="aga-card <?= $activity['status']==='SUCCESS'?'is-success':'is-failed' ?>"><header><div><span dir="ltr"><?= h($activity['occurred_at']) ?></span><h3><?= h($activity['event_type']) ?></h3></div><b><?= h($activity['status']) ?></b></header><div class="aga-data"><span><small>المعتمد</small><b><?= h($activity['agent_name']?:'—') ?></b></span><span><small>الاتجاه</small><b><?= h($activity['direction']) ?></b></span><span><small>المرجع</small><b><?= h(trim($activity['entity_type'].' #'.$activity['entity_id'],' #')) ?></b></span></div><p><?= h($activity['summary']) ?></p><?php if($activity['error_message']): ?><em><?= h($activity['error_message']) ?></em><?php endif; ?></article><?php endforeach; endif; ?></div></section>
<details class="aga-sync"><summary><span><small>Sync Queue</small><b>المزامنة والمتابعة</b></span><strong><?= count($syncEvents) ?></strong></summary><div><?php if(!$syncEvents): ?><p class="aga-empty">لا توجد أحداث معلقة أو فاشلة.</p><?php else: foreach($syncEvents as $event): ?><article><header><div><span><?= h($event['agent_name']?:'—') ?></span><b><?= h($event['event_type']) ?></b></div><strong class="<?= $event['status']==='PENDING'?'is-pending':'is-failed' ?>"><?= h($event['status']) ?></strong></header><div class="aga-data"><span><small>الاتجاه</small><b><?= h($event['direction']) ?></b></span><span><small>المحاولات</small><b><?= (int)$event['attempts'] ?></b></span><span><small>الموعد التالي</small><b dir="ltr"><?= h($event['available_at']) ?></b></span></div><?php if($event['last_error']): ?><em><?= h($event['last_error']) ?></em><?php endif; ?><?php if(in_array($event['status'],['FAILED','DEAD'],true)): ?><form method="post"><?php ua_csrf_field(); ?><input type="hidden" name="event_id" value="<?= (int)$event['id'] ?>"><button name="retry_sync"><i class="fa fa-repeat"></i> إعادة المحاولة</button></form><?php endif; ?></article><?php endforeach; endif; ?></div></details>
</main></div>
