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

// ─────────────────────────────────────────────
// শুধুমাত্র এই Agent-এর owncode দিয়ে Registration করা User-দের হিসাব
// (অন্য কোনো Agent-এর data এখানে আসবেই না — WHERE clause-এই নিশ্চিত)
// ─────────────────────────────────────────────
$totalUsers = 0;
$activeUsers = 0;
$totalDeposit = 0;
$totalWithdraw = 0;
$todayDeposit = 0;
$todayWithdraw = 0;

$stmt = $conn->prepare("
    SELECT
        COUNT(*) AS total_users,
        SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS active_users
    FROM shonu_subjects
    WHERE code = ?
");
$stmt->bind_param("s", $agent_owncode);
$stmt->execute();
$row = $stmt->get_result()->fetch_assoc();
$stmt->close();
$totalUsers = $row['total_users'] ?? 0;
$activeUsers = $row['active_users'] ?? 0;

// এই Agent-এর downline User-দের সব সফল Deposit/Withdraw (subquery দিয়ে user-id মিলিয়ে)
$stmt2 = $conn->prepare("
    SELECT COALESCE(SUM(t.motta),0) AS total
    FROM thevani t
    INNER JOIN shonu_subjects s ON s.id = t.balakedara
    WHERE s.code = ? AND t.sthiti = 1
");
$stmt2->bind_param("s", $agent_owncode);
$stmt2->execute();
$totalDeposit = $stmt2->get_result()->fetch_assoc()['total'] ?? 0;
$stmt2->close();

$stmt3 = $conn->prepare("
    SELECT COALESCE(SUM(w.motta),0) AS total
    FROM hintegedukolli w
    INNER JOIN shonu_subjects s ON s.id = w.balakedara
    WHERE s.code = ? AND w.sthiti = 1
");
$stmt3->bind_param("s", $agent_owncode);
$stmt3->execute();
$totalWithdraw = $stmt3->get_result()->fetch_assoc()['total'] ?? 0;
$stmt3->close();

$stmt4 = $conn->prepare("
    SELECT COALESCE(SUM(t.motta),0) AS total
    FROM thevani t
    INNER JOIN shonu_subjects s ON s.id = t.balakedara
    WHERE s.code = ? AND t.sthiti = 1 AND DATE(t.dinankavannuracisi) = CURDATE()
");
$stmt4->bind_param("s", $agent_owncode);
$stmt4->execute();
$todayDeposit = $stmt4->get_result()->fetch_assoc()['total'] ?? 0;
$stmt4->close();

$stmt5 = $conn->prepare("
    SELECT COALESCE(SUM(w.motta),0) AS total
    FROM hintegedukolli w
    INNER JOIN shonu_subjects s ON s.id = w.balakedara
    WHERE s.code = ? AND w.sthiti = 1 AND DATE(w.dinankavannuracisi) = CURDATE()
");
$stmt5->bind_param("s", $agent_owncode);
$stmt5->execute();
$todayWithdraw = $stmt5->get_result()->fetch_assoc()['total'] ?? 0;
$stmt5->close();
?>

<div class="al-wrap">
  <div class="al-page-title">আমার Business Overview</div>
  <div class="al-page-sub">শুধুমাত্র তোমার নিজের Invitation Code (<?php echo htmlspecialchars($agent_owncode); ?>) দিয়ে Join করা User-দের হিসাব</div>

  <div style="display:flex; gap:12px; margin-bottom:16px;">
    <div class="al-card" style="flex:1; text-align:center; padding:14px;">
      <div style="font-size:11px; color:var(--text-dim);">মোট User</div>
      <div style="font-size:22px; font-weight:700; color:var(--gold);"><?php echo $totalUsers; ?></div>
    </div>
    <div class="al-card" style="flex:1; text-align:center; padding:14px;">
      <div style="font-size:11px; color:var(--text-dim);">Active User</div>
      <div style="font-size:22px; font-weight:700; color:#1FB979;"><?php echo $activeUsers; ?></div>
    </div>
  </div>

  <div style="color:var(--gold); font-weight:700; margin:20px 0 10px; font-size:15px;">💰 আজকের হিসাব</div>
  <div style="display:flex; gap:12px; margin-bottom:16px;">
    <div class="al-card" style="flex:1; text-align:center; padding:14px;">
      <div style="font-size:11px; color:var(--text-dim);">আজকের Deposit</div>
      <div style="font-size:17px; font-weight:700; color:#1FB979;">৳<?php echo number_format($todayDeposit, 2); ?></div>
    </div>
    <div class="al-card" style="flex:1; text-align:center; padding:14px;">
      <div style="font-size:11px; color:var(--text-dim);">আজকের Withdraw</div>
      <div style="font-size:17px; font-weight:700; color:#E8544A;">৳<?php echo number_format($todayWithdraw, 2); ?></div>
    </div>
  </div>

  <div style="color:var(--gold); font-weight:700; margin:20px 0 10px; font-size:15px;">📊 সর্বমোট (এখন পর্যন্ত)</div>
  <div style="display:flex; gap:12px;">
    <div class="al-card" style="flex:1; text-align:center; padding:14px;">
      <div style="font-size:11px; color:var(--text-dim);">Total Deposit</div>
      <div style="font-size:17px; font-weight:700; color:#1FB979;">৳<?php echo number_format($totalDeposit, 2); ?></div>
    </div>
    <div class="al-card" style="flex:1; text-align:center; padding:14px;">
      <div style="font-size:11px; color:var(--text-dim);">Total Withdraw</div>
      <div style="font-size:17px; font-weight:700; color:#E8544A;">৳<?php echo number_format($totalWithdraw, 2); ?></div>
    </div>
  </div>
</div>
</body>
</html>
