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

<div class="al-wrap">
  <div class="al-page-title">Dashboard</div>
  <div class="al-page-sub">তোমার আজকের অবস্থা, এক নজরে</div>

  <div class="al-passbook">
    <div class="label">Balance</div>
    <div class="amount mono">৳<?php echo number_format($agent_balance, 2); ?></div>
  </div>

  <!-- আমার Invitation Code ও Registration Link -->
  <?php
  /* ✅ FIX — আগে এখানে পুরনো, ভাঙা Vue-Hash Format ব্যবহার হতো
     (#/register?invitationCode=...), যেটা কখনো Register Page-এ
     নিয়ে যেতো না, Home-এ চলে যেতো। এখন সঠিক, আসল Register Page-এর
     (newregister.php) সাথে মিলিয়ে দেওয়া হয়েছে। */
  $reg_link = 'https://cv111.site/newregister.php?code=' . $agent_owncode;
  ?>
  <div class="al-card" style="border-left:3px solid var(--gold);">
    <div style="font-size:11.5px;color:var(--text-dim);margin-bottom:10px;">User-দের এই link পাঠাও — click করলেই Invitation Code auto-fill হয়ে যাবে</div>
    <div style="display:flex;align-items:center;justify-content:space-between;gap:10px;flex-wrap:wrap;background:var(--bg);border:1px solid var(--border);border-radius:8px;padding:10px 14px;">
      <div class="mono" style="font-size:12px;word-break:break-all;color:var(--text);"><?php echo htmlspecialchars($reg_link); ?></div>
      <button class="al-btn al-btn-cyan al-btn-sm" style="flex-shrink:0;"
        onclick="navigator.clipboard.writeText('<?php echo htmlspecialchars($reg_link); ?>');this.textContent='Copied ✓';setTimeout(()=>{this.textContent='Copy Link';},1500);">
        Copy Link
      </button>
    </div>
    <div style="display:flex;align-items:center;justify-content:space-between;gap:10px;flex-wrap:wrap;margin-top:10px;">
      <div style="font-size:12px;color:var(--text-dim);">Code: <b class="mono" style="font-size:14px;color:var(--gold);letter-spacing:1px;"><?php echo htmlspecialchars($agent_owncode); ?></b></div>
      <button class="al-btn al-btn-outline al-btn-sm"
        onclick="navigator.clipboard.writeText('<?php echo htmlspecialchars($agent_owncode); ?>');this.textContent='Copied ✓';setTimeout(()=>{this.textContent='Copy Code';},1500);">
        Copy Code
      </button>
    </div>
  </div>

  <?php
  $today_given = 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()"
  ));
  $total_given = mysqli_fetch_assoc(mysqli_query($conn,
      "SELECT COALESCE(SUM(amount),0) as total FROM agent_motta_log
       WHERE agent_userid='$agent_uid'"
  ));
  $total_users = mysqli_fetch_assoc(mysqli_query($conn,
      "SELECT COUNT(DISTINCT to_user) as total FROM agent_motta_log
       WHERE agent_userid='$agent_uid'"
  ));
  ?>
  <div class="al-stats">
    <div class="al-stat">
      <div class="k">Given Today</div>
      <div class="v">৳<?php echo number_format($today_given['total'], 2); ?></div>
    </div>
    <div class="al-stat">
      <div class="k">Users Served</div>
      <div class="v"><?php echo $total_users['total']; ?></div>
    </div>
    <div class="al-stat">
      <div class="k">Total Given</div>
      <div class="v">৳<?php echo number_format($total_given['total'], 2); ?></div>
    </div>
  </div>

  <div style="font-size:11.5px;font-weight:700;color:var(--text-dim);margin:22px 0 10px;text-transform:uppercase;letter-spacing:.6px;">Recent Activity</div>

  <div class="al-table-wrap">
    <div class="al-trow head"><div>User</div><div>তারিখ</div><div>Amount</div></div>
    <?php
    $recent = 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 10"
    );
    $has = false;
    while($row = mysqli_fetch_assoc($recent)):
        $has = true;
    ?>
    <div class="al-trow">
      <div><b><?php echo $row['user_mobile'] ?? $row['to_user']; ?></b></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>

  <div style="text-align:right;margin-top:12px;">
    <a href="history.php" style="color:var(--cyan);font-size:12.5px;font-weight:600;text-decoration:none;">সম্পূর্ণ History দেখো →</a>
  </div>

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