<div class="content-wrapper android-agents-report-view">
  <main class="aarv-main">
    <header class="aarv-heading">
      <div>
        <span>AGENT DETAILS</span>
        <h1><?= h($agent['name'] ?? 'المعتمد') ?></h1>
        <p><?= h($agent['username'] ?? '') ?><?= !empty($agent['phone']) ? ' · ' . h($agent['phone']) : '' ?></p>
      </div>
      <a href="agents_report.php?from=<?= h($from) ?>&amp;to=<?= h($to) ?>"><i class="fa fa-arrow-right"></i><b>التقرير</b></a>
    </header>

    <form class="aarv-filter" method="get" data-ua-android-native-layout>
      <input type="hidden" name="agent_id" value="<?= (int)$agent_id ?>">
      <label><span>من</span><input type="date" name="from" value="<?= h($from) ?>"></label>
      <label><span>إلى</span><input type="date" name="to" value="<?= h($to) ?>"></label>
      <button><i class="fa fa-filter"></i> تطبيق</button>
    </form>

    <section class="aarv-stats">
      <article><i class="fa fa-shopping-cart"></i><span>عدد المبيعات</span><strong><?= count($sales) ?></strong></article>
      <article><i class="fa fa-money"></i><span>إجمالي المبيعات</span><strong dir="ltr"><?= number_format($sales_sum, 2) ?></strong></article>
      <article><i class="fa fa-credit-card"></i><span>إجمالي المقبوضات</span><strong dir="ltr"><?= number_format($pay_sum, 2) ?></strong></article>
      <article><i class="fa fa-mobile"></i><span>تبديل جهاز</span><strong><?= (int)$reset_cnt ?></strong></article>
    </section>

    <section class="aarv-section">
      <header><div><span>SALES</span><h2>فواتير البيع</h2></div><b><?= count($sales) ?></b></header>
      <div class="aarv-list">
        <?php if (!$sales): ?><p class="aarv-empty">لا توجد مبيعات ضمن الفترة المحددة.</p><?php else: foreach ($sales as $sale): ?>
          <a class="aarv-card" href="sales_view.php?id=<?= (int)$sale['id'] ?>">
            <div><span>فاتورة #<?= (int)$sale['id'] ?></span><h3><?= h($sale['customer_name'] ?: 'عميل غير محدد') ?></h3><small dir="ltr"><?= h($sale['created_at'] ?? '-') ?></small></div>
            <div><strong dir="ltr"><?= number_format((float)($sale['net_total'] ?? 0), 2) ?></strong><small><?= h($sale['status'] ?? '-') ?></small><i class="fa fa-angle-left"></i></div>
          </a>
        <?php endforeach; endif; ?>
      </div>
    </section>

    <section class="aarv-section">
      <header><div><span>PAYMENTS</span><h2>المدفوعات المرتبطة</h2></div><b><?= count($payments) ?></b></header>
      <div class="aarv-list">
        <?php if (!$payments): ?><p class="aarv-empty">لا توجد مدفوعات ضمن الفترة المحددة.</p><?php else: foreach ($payments as $payment): ?>
          <a class="aarv-card" href="finance_payments.php?payment_id=<?= (int)$payment['id'] ?>">
            <div><span>دفعة #<?= (int)$payment['id'] ?></span><h3><?= h($payment['method'] ?? '-') ?></h3><small><?= h($payment['refx'] ?? '-') ?></small></div>
            <div><strong dir="ltr"><?= number_format((float)($payment['amount'] ?? 0), 2) ?></strong><small dir="ltr"><?= h($payment['paid_at'] ?? '-') ?></small><i class="fa fa-angle-left"></i></div>
          </a>
        <?php endforeach; endif; ?>
      </div>
    </section>

    <section class="aarv-section">
      <header><div><span>LEDGER</span><h2>سجل عمليات المعتمد</h2><small>المقبوض والتسويات الدائنة: <b dir="ltr"><?= number_format($topup_sum, 2) ?></b></small></div><b><?= count($tx) ?></b></header>
      <div class="aarv-list">
        <?php if (!$tx): ?><p class="aarv-empty">لا توجد حركات ضمن الفترة المحددة.</p><?php else: foreach ($tx as $entry): ?>
          <article class="aarv-card aarv-ledger">
            <div><span>قيد #<?= (int)$entry['id'] ?></span><h3><?= h($entry['type'] ?? '-') ?></h3><small><?= h($entry['note'] ?? '-') ?></small></div>
            <div><strong dir="ltr"><?= number_format((float)($entry['amount'] ?? 0), 2) ?></strong><small><?= h($entry['direction'] ?? '-') ?> · <?= h($entry['created_at'] ?? '-') ?></small></div>
          </article>
        <?php endforeach; endif; ?>
      </div>
    </section>
  </main>
</div>
