<?php
require_once '../connection/db.php';
requireLogin();
$user=getCurrentUser();$memberId=(int)$user['member_id'];$db=getDB();
$page=max(1,(int)($_GET['page']??1));$limit=20;$offset=($page-1)*$limit;
$tQ=$db->prepare('SELECT COUNT(*) AS c FROM meddolic_user_details WHERE sponser_id=?');
$tQ->execute([$memberId]);$totalRows=(int)($tQ->fetch()['c']??0);$totalPages=(int)ceil($totalRows/$limit);
$rQ=$db->prepare("SELECT ud.user_id, ud.walletAddress, ud.date_time, ud.activation_date, ud.topup_flag, ud.currentPackage,
    pl.poolName
    FROM meddolic_user_details ud
    LEFT JOIN meddolic_config_pool_list pl ON pl.id=ud.currentPackage
    WHERE ud.sponser_id=? ORDER BY ud.date_time DESC LIMIT ? OFFSET ?");
$rQ->execute([$memberId,$limit,$offset]);$referrals=$rQ->fetchAll();
$totalInc=getReferralIncomeTotal($memberId);
$pageTitle='My Referrals';$activePage='referrals';require_once 'Include/header.php';
?>
<div class="layout"><?php require_once 'Include/menu.php';?>
<div class="main-content"><?php require_once 'Include/topbar.php';?>
<div class="page-content">
<div class="page-header">
  <div class="breadcrumb"><i class="fas fa-home"></i> <i class="fas fa-chevron-right"></i> My Referrals</div>
  <h3>My Referrals</h3><div class="subtitle">All direct referrals from your link</div>
</div>
<div class="stats-grid" style="margin-bottom:20px">
  <div class="stat-card"><div class="stat-icon"><i class="fas fa-user-plus"></i></div>
    <div class="stat-info"><p>Total Referrals</p><h5><?=number_format($totalRows)?></h5></div></div>
  <div class="stat-card"><div class="stat-icon"><i class="fas fa-coins"></i></div>
    <div class="stat-info"><p>Referral Earnings</p><h5 class="gold"><?=fmtUSD($totalInc)?></h5></div></div>
</div>
<!-- Referral link box -->
<div class="referral-card" style="margin-bottom:16px">
  <div class="card-title"><i class="fas fa-share-alt"></i> Your Referral Link</div>
  <div class="ref-input-group">
    <input class="ref-input" id="refLink" readonly
     value="<?=e('http'.(isset($_SERVER['HTTPS'])?'s':'').'://'.$_SERVER['HTTP_HOST'].dirname(dirname($_SERVER['SCRIPT_NAME'])).'/register/Register.php?ref='.($user['walletAddress']??''))?>">
    <button class="btn-copy" onclick="copyText('refLink')"><i class="fas fa-copy"></i> Copy</button>
  </div>
</div>
<div class="card">
  <div class="card-title"><i class="fas fa-user-plus"></i> Referral List</div>
  <div class="table-responsive"><table class="data-table">
    <thead><tr><th>#</th><th>User ID</th><th>Wallet</th><th>Current Package</th><th>Join Date</th><th>Active Date</th><th>Status</th></tr></thead>
    <tbody>
    <?php if(empty($referrals)):?><tr class="empty-row"><td colspan="7"><i class="fas fa-inbox"></i> No referrals yet. Share your link!</td></tr>
    <?php else:foreach($referrals as $i=>$r):?>
    <tr>
      <td><?=$offset+$i+1?></td>
      <td class="amount"><?=e($r['user_id'])?></td>
      <td style="font-family:monospace;font-size:11px;color:#555"><?=e(substr($r['walletAddress']??'',0,12))?>…</td>
      <td><?=$r['poolName']?'<span class="badge badge-blue">'.e($r['poolName']).'</span>':'<span style="color:#444">—</span>'?></td>
      <td style="font-size:12px;color:var(--text-muted)"><?=date('d M Y',strtotime($r['date_time']))?></td>
      <td style="font-size:12px;color:var(--text-muted)"><?=$r['activation_date']?date('d M Y',strtotime($r['activation_date'])):'—'?></td>
      <td><?=(int)$r['topup_flag']?'<span class="badge badge-green">Active</span>':'<span class="badge badge-red">Pending</span>'?></td>
    </tr>
    <?php endforeach;endif;?>
    </tbody>
  </table></div>
  <?php if($totalPages>1):?><div style="display:flex;justify-content:center;gap:8px;margin-top:16px;flex-wrap:wrap">
    <?php for($p=1;$p<=$totalPages;$p++):?><a href="?page=<?=$p?>" style="padding:6px 14px;border-radius:6px;font-size:13px;background:<?=$p==$page?'rgba(241,145,7,.2)':'var(--dark3)'?>;border:1px solid <?=$p==$page?'rgba(241,145,7,.4)':'var(--border)'?>;color:<?=$p==$page?'var(--gold)':'var(--text-muted)'?>;text-decoration:none"><?=$p?></a><?php endfor;?>
  </div><?php endif;?>
</div>
</div><?php require_once 'Include/footer.php';?></div></div>
