<?php
require_once("auth.php");
include("header.php");

$page  = isset($_GET['page']) ? intval($_GET['page']) : 1;
$limit = 20;
$offset = ($page - 1) * $limit;

$total_row = mysqli_fetch_assoc(mysqli_query($conn,
    "SELECT COUNT(*) as cnt FROM agent_motta_log WHERE agent_userid='$agent_uid'"
));
$total_pages = ceil($total_row['cnt'] / $limit);

$logs = mysqli_query($conn,
    "SELECT l.*, s.mobile as user_mobile
     FROM agent_motta_log l
     LEFT JOIN shonu_subjects s ON s.id = l.to_user
     WHERE l.agent_userid='$agent_uid'
     ORDER BY l.id DESC
     LIMIT $limit OFFSET $offset"
);

$summary = mysqli_fetch_assoc(mysqli_query($conn,
    "SELECT COALESCE(SUM(amount),0) as total, COUNT(*) as cnt
     FROM agent_motta_log WHERE agent_userid='$agent_uid'"
));
$today_sum = mysqli_fetch_assoc(mysqli_query($conn,
    "SELECT COALESCE(SUM(amount),0) as total
     FROM agent_motta_log WHERE agent_userid='$agent_uid' AND DATE(created_at)=CURDATE()"
));
?>

<div class="al-wrap">
  <div class="al-page-title">Transfer History</div>
  <div class="al-page-sub">তোমার সব Coin transfer-এর সম্পূর্ণ খতিয়ান</div>

  <div class="al-stats">
    <div class="al-stat">
      <div class="k">মোট Transfer</div>
      <div class="v"><?php echo $summary['cnt']; ?></div>
    </div>
    <div class="al-stat">
      <div class="k">আজ দিয়েছো</div>
      <div class="v">৳<?php echo number_format($today_sum['total'],2); ?></div>
    </div>
    <div class="al-stat">
      <div class="k">সর্বমোট</div>
      <div class="v">৳<?php echo number_format($summary['total'],2); ?></div>
    </div>
  </div>

  <div style="display:flex;justify-content:space-between;align-items:baseline;margin:22px 0 10px;">
    <div style="font-size:11.5px;font-weight:700;color:var(--text-dim);text-transform:uppercase;letter-spacing:.6px;">সব Transfer</div>
    <div style="font-size:11.5px;color:var(--text-faint);">পাতা <?php echo $page; ?> / <?php echo max(1,$total_pages); ?></div>
  </div>

  <div class="al-table-wrap">
    <div class="al-trow head"><div>User</div><div>তারিখ</div><div>Amount</div></div>
    <?php
    $has = false;
    while($row = mysqli_fetch_assoc($logs)):
        $has = true;
    ?>
    <div class="al-trow">
      <div><b><?php echo $row['user_mobile'] ?? $row['to_user']; ?></b> <span style="color:var(--text-faint);font-size:10.5px;">#<?php echo $row['id']; ?></span></div>
      <div style="color:var(--text-faint);font-size:11.5px;"><?php echo $row['created_at']; ?></div>
      <div class="amt">৳<?php echo number_format($row['amount'],2); ?></div>
    </div>
    <?php endwhile; ?>
    <?php if(!$has): ?>
    <div class="al-empty"><div class="icon">📭</div><div class="msg">এখনো কোনো Transfer হয়নি</div></div>
    <?php endif; ?>
  </div>

  <?php if($total_pages > 1): ?>
  <div style="display:flex;gap:6px;flex-wrap:wrap;margin-top:14px;">
    <?php for($i=1;$i<=$total_pages;$i++): ?>
    <a href="?page=<?php echo $i; ?>" class="al-btn al-btn-sm <?php echo $i==$page?'al-btn-cyan':'al-btn-outline'; ?>" style="text-decoration:none;min-width:36px;">
      <?php echo $i; ?>
    </a>
    <?php endfor; ?>
  </div>
  <?php endif; ?>

</div>
</body>
</html>
