<?php
$page_title = 'سجل عمليات المعتمدين';
$UA_FORCE_DESIGN_V2 = true;
$uaAndroidAgentTransactions = ((string)($_GET['ua_android'] ?? '') === '1')
  || stripos((string)($_SERVER['HTTP_USER_AGENT'] ?? ''), 'NetworkCustomersAndroid/') !== false;
if ($uaAndroidAgentTransactions) $UA_PAGE_CSS = ['assets/css/android_agent_transactions.css'];
include __DIR__ . '/header.php';
include __DIR__ . '/sidebar.php';
require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/db_bootstrap.php';
require_once __DIR__ . '/agent_service.php';

require_login();
$conn = ensure_db(true);
if(function_exists('require_any_perm')) require_any_perm(['can_view_agent_log','can_manage_agents']);

ua_agent_schema_migrate($conn);
$rs = $conn->query("SELECT t.*,a.full_name AS agent_name,s.full_name AS subscriber_name,s.created_by_type,s.created_by_id
  FROM agent_ledger_entries t
  LEFT JOIN agents a ON t.agent_id=a.id
  LEFT JOIN subscribers s ON s.id=t.subscriber_id
  ORDER BY t.id DESC LIMIT 200");
if ($uaAndroidAgentTransactions) {
  require __DIR__ . '/android_agent_transactions.php';
  include __DIR__ . '/footer.php';
  return;
}
?>
<div class="content-wrapper ua-extracted-page ua-agents-page">
  <section class="content-header"><div class="container-fluid"><h1 class="mb-0">سجل عمليات المعتمدين</h1><?php include __DIR__ . '/agents_nav.php'; ?></div></section>
  <section class="content"><div class="container-fluid">
    <div class="card"><div class="card-body">
      <div class="table-responsive">
        <table class="table table-bordered">
          <thead><tr><th>#</th><th>المعتمد</th><th>المشترك</th><th>مصدر الإضافة</th><th>النوع</th><th>الاتجاه</th><th>المبلغ</th><th>الرصيد بعد</th><th>ملاحظة</th><th>الوقت</th></tr></thead>
          <tbody>
            <?php while($r=$rs->fetch_assoc()){ ?>
              <?php
                $createdByThisAgent = ($r['created_by_type'] ?? '') === 'agent' && (int)($r['created_by_id'] ?? 0) === (int)$r['agent_id'];
                $sourceLabel = $createdByThisAgent ? 'أضافه هذا المعتمد' : ((($r['created_by_type'] ?? '') === 'agent') ? 'أضافه معتمد آخر' : 'من النسخة الأساسية');
              ?>
              <tr>
                <td><?= (int)$r['id'] ?></td>
                <td><?= h($r['agent_name'] ?? '-') ?></td>
                <td><?= $r['subscriber_id'] ? h(($r['subscriber_name'] ?: ('#' . (int)$r['subscriber_id']))) : '-' ?></td>
                <td><span class="badge <?= $createdByThisAgent ? 'badge-success' : 'badge-secondary' ?>"><?= h($sourceLabel) ?></span></td>
                <td><?= h($r['entry_type']) ?></td>
                <td><?= h($r['direction']) ?></td>
                <td><?= number_format((float)$r['amount'],2) ?></td>
                <td><?= number_format((float)$r['balance_after'],2) ?></td>
                <td><?= h($r['description'] ?? '') ?></td>
                <td><?= h($r['occurred_at'] ?? '') ?></td>
              </tr>
            <?php } ?>
          </tbody>
        </table>
      </div>
    </div></div>
  </div></section>
</div>
<?php include __DIR__ . '/footer.php'; ?>
