<?php
$today = date('Y-m-d');
$week_start = date('Y-m-d', strtotime('monday this week'));
$week_end   = date('Y-m-d', strtotime('sunday this week'));

$last_week_start = date('Y-m-d', strtotime('monday last week'));
$last_week_end   = date('Y-m-d', strtotime('sunday last week'));

$q_week = mysqli_query($koneksi,"
  SELECT SUM(total) AS total 
  FROM tb_transaksi 
  WHERE id_user = '$extplayer'
  AND transaksi = 'Bonus Refferal'
  AND status = 'Sukses'
  AND DATE(tanggal) BETWEEN '$week_start' AND '$week_end'
");
$d_week = mysqli_fetch_array($q_week);
$komisi_minggu_ini = $d_week['total'] ?? 0;

$q_last = mysqli_query($koneksi,"
  SELECT SUM(total) AS total 
  FROM tb_transaksi 
  WHERE id_user = '$extplayer'
  AND transaksi = 'Bonus Refferal'
  AND status = 'Sukses'
  AND DATE(tanggal) BETWEEN '$last_week_start' AND '$last_week_end'
");
$d_last = mysqli_fetch_array($q_last);
$komisi_minggu_lalu = $d_last['total'] ?? 0;

$q_downline = mysqli_query($koneksi,"
  SELECT COUNT(id) AS total
  FROM tb_refferal
  WHERE user_refferal = '$extplayer'
");
$d_downline = mysqli_fetch_assoc($q_downline);
$total_pendaftar = $d_downline['total'] ?? 0;

$q_aktif = mysqli_query($koneksi,"
  SELECT COUNT(DISTINCT id_user) AS total 
  FROM tb_transaksi
  WHERE transaksi = 'Top Up'
  AND status = 'Sukses'
  AND id_user IN (
    SELECT id_user 
    FROM tb_refferal 
    WHERE user_refferal = '$extplayer'
  )
");
$d_aktif = mysqli_fetch_array($q_aktif);
$downline_aktif = $d_aktif['total'];

?>

<?php
$fullRefLink = $urlweb . "/?reff=" . $extplayer;

// tampil 3 depan + ...
$shortRef = substr($extplayer, 0, 3) . "...";
$shortRefLink = $urlweb . "/?reff=" . $shortRef;
?>

  <!-- HEADER -->
  <div class="none-header">
    <div class="left" onclick="window.location.href='?page=home'">
      <i class="fas fa-chevron-left"></i> Ajak Teman
    </div>
  
    <div class="right">
     <a href="?page=bahan-iklan" class="btn-header-reff">
      <i class="fas fa-download"></i> Bahan iklan
      </a>
      <a href="javascript:void(0)" onclick="openLiveChat()" class="button-nav">
        <i class="fas fa-headset m-auto"></i>
      </a>
    </div>
  </div>

  <main id="ref-main">
  <!-- TOP TAB -->
  <div class="top-tabs">
    <button class="top-tab active" onclick="openTab('ringkasan', this)">Refferal</button>
    <button class="top-tab" onclick="openTab('kyc', this)">KYC</button>
    <button class="top-tab" onclick="openTab('bonus', this)">Bonus</button>
    <button class="top-tab" onclick="openTab('tim', this)">Team</button>
  </div>

  <!-- ================= RINGKASAN ================= -->
  <div class="tab-content active" id="ringkasan">

    <!-- INVITE CARD -->
    <div class="card invite-card">
      <h3>Undang melalui</h3>

      <div class="invite-item">
        <span>Kode</span>
        <div class="invite-box">
          <b id="refCode"><?= $extplayer ?></b>
          <button onclick="copyText('refCode')">Salin</button>
        </div>
      </div>

      <div class="invite-item">
        <span>Tautan</span>
        <div class="invite-box">
        <small 
          id="refLink" 
          data-full="<?= $fullRefLink ?>"
        >
          <?= $shortRefLink ?>
        </small>
        <button onclick="copyRefLink()">Salin</button>
        </div>
      </div>
    </div>
    
    <!-- BANNER -->
    <div class="banner banner-kyc">
      <div class="banner-text">
        <h4>Daftar resmi KYC</h4>
        <p>Dapatkan komisi s/d 10% setiap deposit</p>
        <button onclick="openKYCModal()">Informasi KYC</button>
      </div>
    </div>
    
    <!-- BANNER -->
    <div class="banner banner-blue">
      <div class="banner-text">
        <h4>Komisi total Deposit downline</h4>
        <p>Dapatkan komisi s/d 0.8% setiap deposit</p>
        <button onclick="openReferralModal()">Muat lebih banyak</button>
      </div>
    </div>
  
    <!-- STATS -->
    <div class="stats">
      <div class="stat-box">
        Komisi minggu ini
        <b>Rp <?= number_format($komisi_minggu_ini) ?></b>
      </div>

      <div class="stat-box">
        Komisi minggu lalu
        <b>Rp <?= number_format($komisi_minggu_lalu) ?></b>
      </div>

      <div class="stat-box">
        Total pendaftar
        <b><?= $total_pendaftar ?></b>
      </div>

      <div class="stat-box">
        Downline aktif
        <b><?= $downline_aktif ?></b>
      </div>
    </div>
  </div>

  <!-- ================= BONUS ================= -->
  <div class="tab-content" id="bonus">
    <div class="card">
      <h3>Riwayat Bonus</h3>

      <table class="ref-table">
        <thead>
          <tr>
            <th>Username</th>
            <th>Keterangan</th>
            <th>Bonus</th>
          </tr>
        </thead>
        <tbody>
          <?php
          $query4 = mysqli_query($koneksi, "SELECT * FROM tb_refferal WHERE user_refferal='$extplayer'");
          while ($d = mysqli_fetch_array($query4)) {
            $id = $d['id_user'];
            $u  = mysqli_fetch_array(mysqli_query($koneksi, "SELECT username FROM tb_user WHERE refferal='$id'"));
          ?>
            <tr>
              <td><?= $u['username'] ?></td>
              <td><?= $d['keterangan'] ?></td>
              <td class="bonus">Rp <?= number_format($d['bonus']) ?></td>
            </tr>
          <?php } ?>
        </tbody>
      </table>
    </div>
  </div>

  <!-- ================= TIM ================= -->
  <div class="tab-content" id="tim">
    <div class="card">
      <h3>Downline Saya</h3>

      <!-- SEARCH -->
      <div class="team-search">
        <input
          type="text"
          id="searchDownline"
          placeholder="Cari username / ID player..."
          onkeyup="searchDownline()"
        >
        <span class="team-search-icon">🔍</span>
      </div>

      <!-- TABLE -->
      <div class="table-wrapper">
        <table class="ref-table" id="downlineTable">
          <thead>
            <tr>
              <th>Username</th>
              <th>ID Player</th>
              <th>Total Deposit</th>
              <th>Total Bonus</th>
            </tr>
          </thead>
          <tbody>
            <?php
            $query4 = mysqli_query($koneksi, "
              SELECT * FROM tb_refferal 
              WHERE user_refferal = '$extplayer'
            ");

            while ($d = mysqli_fetch_array($query4)) {
              $ext = $d['id_user'];

              // USERNAME
              $u = mysqli_fetch_array(mysqli_query($koneksi, "
                SELECT username 
                FROM tb_user 
                WHERE refferal = '$ext'
                LIMIT 1
              "));
              $username = $u['username'] ?? '-';

              // TOTAL DEPOSIT
              $qDeposit = mysqli_query($koneksi, "
                SELECT SUM(total) AS total_deposit
                FROM tb_transaksi
                WHERE id_user = '$ext'
                  AND transaksi = 'Top Up'
                  AND status = 'Sukses'
              ");
              $dps = mysqli_fetch_array($qDeposit);
              $total_deposit = $dps['total_deposit'] ?? 0;

              // TOTAL BONUS
              $qBonus = mysqli_query($koneksi, "
                SELECT SUM(bonus) AS total_bonus
                FROM tb_refferal
                WHERE id_user = '$ext'
                  AND user_refferal = '$extplayer'
              ");
              $b = mysqli_fetch_array($qBonus);
              $total_bonus = $b['total_bonus'] ?? 0;
            ?>
              <tr>
                <td><?= htmlspecialchars($username) ?></td>
                <td style="font-size:10px;color:#aaa"><?= htmlspecialchars($ext) ?></td>
                <td>Rp <?= number_format($total_deposit) ?></td>
                <td class="bonus">Rp <?= number_format($total_bonus) ?></td>
              </tr>
            <?php } ?>

            <?php if (mysqli_num_rows($query4) == 0) { ?>
              <tr>
                <td colspan="4" style="text-align:center;color:#777">
                  Belum ada downline
                </td>
              </tr>
            <?php } ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
  
  <!-- ================= KYC ================= -->
  <div class="tab-content" id="kyc">
    <button class="btn-kyc-info" onclick="openKYCModal()">
    🔐 Keuntungan & Informasi KYC
  </button>
  
  <div class="card" id="kycCard">
    <h3>Form KYC</h3>
      <form id="kycForm" onsubmit="sendKYC(event)">
        
        <div class="form-group">
          <label>Nama Lengkap</label>
          <input type="text" id="kyc_nama" required>
        </div>
  
        <div class="form-group">
          <label>NIK</label>
          <input type="number" id="kyc_ktp" required>
        </div>
  
        <div class="form-group">
          <label>No kk</label>
          <input type="number" id="kyc_kk" required>
        </div>
        <div class="form-group">
          <label>No Rekening</label>
          <input type="number" id="kyc_rekening" required>
        </div>
        
        <div class="form-group">
          <label>Nama Rekening</label>
          <input type="text" id="kyc_nama_rekening" required>
        </div>
        <div class="form-group">
          <label>Nomor WhatsApp Aktif</label>
          <input type="number" id="kyc_wa" required>
        </div>
  
        <div class="form-group">
          <label>Pengalaman Kerja</label>
          <textarea id="kyc_pengalaman" rows="3" required></textarea>
        </div>
  
        <button type="submit" class="btn-kyc">Kirim KYC</button>
      </form>
  
    </div>
      <div class="kyc-lock-overlay" id="kycLock">
    <div class="kyc-lock-box">
      <h3>SLOT TIDAK TERSEDIA</h3>
      <p>
        maaf saat ini kyc sudah penuh<br>
        silahkan kembali lagi nanti<br>
        atau hubungi admin
      </p>
    </div>
  </div>
  </div>
</main>

<div class="modal-sheet5-overlay" id="refModal">
  <div class="modal-sheet5-box">
    <div class="modal-sheet5-header">
      <div class="modal-sheet5-drag"></div>
      <span>Cara Kerja Bonus Referral</span>
      <span class="modal-sheet5-close" onclick="closeReferralModal()">✕</span>
    </div>

    <div class="modal-sheet5-body">

      <div class="step">
        <span class="step-num">1</span>
        <div>
          <h4>Bagikan Kode / Link Referral</h4>
          <p>
            Undang teman menggunakan <b>kode atau link referral</b> milik kamu.
            Teman yang mendaftar akan otomatis tercatat sebagai <b>downline</b>.
          </p>
        </div>
      </div>

      <div class="step">
        <span class="step-num">2</span>
        <div>
          <h4>Downline Melakukan Deposit</h4>
          <p>
            Saat downline melakukan deposit dan deposit tersebut <b>disetujui (Sukses)</b>,
            sistem akan memproses bonus referral secara otomatis.
          </p>
        </div>
      </div>

      <div class="step highlight">
        <span class="step-num">3</span>
        <div>
          <h4>Bonus Referral Dibagikan</h4>
          <p>
            Kamu akan mendapatkan <b>bonus 0.8%</b> dari total deposit downline.
            Bonus langsung masuk ke <b>saldo aktif</b> kamu.
          </p>
          <p class="example">
            Contoh: Downline deposit <b>Rp 1.000.000</b><br>
            Bonus kamu: <b>Rp 8.000</b>
          </p>
        </div>
      </div>

      <div class="step">
        <span class="step-num">4</span>
        <div>
          <h4>Bonus Tercatat & Terakumulasi</h4>
          <p>
            Setiap bonus referral akan:
          </p>
          <ul>
            <li>✔ Ditambahkan ke saldo aktif referrer</li>
            <li>✔ Dicatat sebagai transaksi <b>Bonus Referral</b></li>
            <li>✔ Diakumulasikan ke total bonus referral</li>
          </ul>
        </div>
      </div>

      <div class="note">
        Bonus referral akan terus bertambah setiap kali downline kamu melakukan deposit,
        tanpa batas jumlah bonus.
      </div>

    </div>

    <div class="modal-sheet5-footer">
      <button onclick="closeReferralModal()">Mengerti</button>
    </div>
  </div>
</div>

<!-- ================= MODAL INFO KYC ================= -->
<div class="modal-sheet5-overlay" id="kycModal">
  <div class="modal-sheet5-box">
    
    <div class="modal-sheet5-header">
      <div class="modal-sheet5-drag"></div>
      <span>Keuntungan KYC</span>
      <span class="modal-sheet5-close" onclick="closeKYCModal()">✕</span>
    </div>

    <div class="modal-sheet5-body">

      <div class="step highlight">
        <span class="step-num">🔐</span>
        <div>
          <h4>KYC = Akses Premium</h4>
          <p>
            KYC bukan sekadar verifikasi, tapi <b>tiket kamu untuk naik level</b>
            dan membuka semua fitur eksklusif.
          </p>
        </div>
      </div>

      <div class="step">
        <span class="step-num">1</span>
        <div>
          <h4>Kasbon Eksklusif</h4>
          <p>
            Member KYC berhak mengajukan <b>kasbon</b> sesuai ketentuan yang berlaku.
          </p>
        </div>
      </div>

      <div class="step">
        <span class="step-num">2</span>
        <div>
          <h4>Komisi Deposit 32%</h4>
          <p>
            Dapatkan <b>32% dari setiap deposit member</b> kamu  
            <b>tanpa batas</b> dan berlaku seumur hidup.
          </p>
        </div>
      </div>
      
      <div class="step highlight">
        <span class="step-num">💡</span>
        <div>
          <h4>Contoh Perhitungan 32%</h4>
          <p>
            Jika member kamu melakukan <b>deposit Rp1.000.000</b>,  
            maka kamu akan mendapatkan <b>Rp320.000</b> langsung.
          </p>
          <p>
            Deposit Rp5.000.000 → <b>Rp1.600.000</b><br>
            Deposit Rp10.000.000 → <b>Rp3.200.000</b>
          </p>
        </div>
      </div>

      <div class="step highlight">
        <span class="step-num">💰</span>
        <div>
          <h4>Penghasilan Pasif</h4>
          <p>
            Semakin banyak member aktif,  
            semakin besar <b>penghasilan otomatis setiap hari</b>.
          </p>
        </div>
      </div>

      <div class="note">
        Kesempatan KYC tidak selalu dibuka.  
        <b>Isi form sekarang dan amankan posisi kamu.</b>
      </div>

    </div>

    <div class="modal-sheet5-footer">
      <button onclick="closeKYCModal()">Saya Mengerti</button>
    </div>

  </div>
</div>
<div class="toast" id="toast">Berhasil disalin</div>

<script>
function openKYCModal(){
  document.getElementById("kycModal").classList.add("active");
}

function closeKYCModal(){
  document.getElementById("kycModal").classList.remove("active");
}
</script>

<script>
function openReferralModal(){
  document.getElementById("refModal").classList.add("active");
}

function closeReferralModal(){
  document.getElementById("refModal").classList.remove("active");
}
</script>

<script>
function openTab(id, el) {
  document.querySelectorAll('.tab-content')
    .forEach(t => t.classList.remove('active'));
  
  document.querySelectorAll('.top-tab')
    .forEach(b => b.classList.remove('active'));
  
  const target = document.getElementById(id);
  target.classList.add('active');
  el.classList.add('active');
  
  // 🔒 Kalau buka KYC → lock card form saja
  if (id === "kyc") {
    document.getElementById("kycCard").classList.add("locked");
    document.getElementById("kycLock").classList.add("active");
  }
}
/* COPY */
function copyText(id){
  navigator.clipboard.writeText(
    document.getElementById(id).innerText
  ).then(()=>showToast("Berhasil disalin"));
}

  function showToast(msg){
  const t=document.getElementById("toast");
  t.innerText=msg;
  t.classList.add("show");
  setTimeout(()=>t.classList.remove("show"),2000);
}
</script>

<script>
  function searchDownline() {
  const input = document.getElementById("searchDownline");
  const filter = input.value.toLowerCase();
  const table = document.getElementById("downlineTable");
  const tr = table.getElementsByTagName("tr");

  for (let i = 1; i < tr.length; i++) {
    const tdUsername = tr[i].getElementsByTagName("td")[0];
    const tdID = tr[i].getElementsByTagName("td")[1];

    if (tdUsername || tdID) {
      const txtUser = tdUsername.textContent || tdUsername.innerText;
      const txtID = tdID.textContent || tdID.innerText;

      if (
        txtUser.toLowerCase().includes(filter) ||
        txtID.toLowerCase().includes(filter)
      ) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}
</script>

<script>
document.addEventListener("DOMContentLoaded", function () {
  const params = new URLSearchParams(window.location.search);
  const tab = params.get("tab");

  if (tab) {
    const targetTab = document.getElementById(tab);
    const targetBtn = document.querySelector(
      `.top-tab[onclick*="'${tab}'"]`
    );

    if (targetTab && targetBtn) {
      openTab(tab, targetBtn);
    }
  }
});
</script>

<script>
  function sendKYC(e){
  e.preventDefault();
  
  const nama = document.getElementById("kyc_nama").value;
  const ktp = document.getElementById("kyc_ktp").value;
  const kk = document.getElementById("kyc_kk").value;
  const wa = document.getElementById("kyc_wa").value;
  const pengalaman = document.getElementById("kyc_pengalaman").value;
  const rekening = document.getElementById("kyc_rekening").value;
  const namaRek = document.getElementById("kyc_nama_rekening").value;

  const pesan =`
  🔐 FORM KYC MEMBER 🔐
  
  Nama KTP      : ${nama}
  Nomor KTP     : ${ktp}
  Nomor KK      : ${kk}
  WhatsApp      : ${wa}
  
  Pengalaman Kerja:
  ${pengalaman}
  
  Data Rekening:
  - Nomor Rekening : ${rekening}
  - Nama Rekening  : ${namaRek}
    `;
  
    const admin = "083874277588";
    const url = "https://wa.me/62" + admin.substring(1) + "?text=" + encodeURIComponent(pesan);
  
    window.open(url, "_blank");
  }
</script>

<script>
function copyRefLink(){
  const el = document.getElementById("refLink");
  const fullLink = el.getAttribute("data-full");

  navigator.clipboard.writeText(fullLink)
    .then(()=>showToast("Link referral disalin"));
}
</script>

<style>
  /* Blur + Lock */
/* 🔒 Blur + Lock hanya card form KYC */
#kyc .card.locked {
  filter: blur(2px);
  pointer-events: none;
  user-select: none;
}
#kyc {
  position: relative;
}
.kyc-lock-overlay {
  position: absolute;          /* ⬅ dari fixed jadi absolute */
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 10;                 /* kecilin z-index */
}

.kyc-lock-overlay.active {
  display: flex;
}

.kyc-lock-box {
  background: #111;
  padding: 25px;
  border-radius: 14px;
  text-align: center;
  color: #fff;
  max-width: 300px;
}

.kyc-lock-box h3 {
  margin-bottom: 10px;
  font-size:20px;
  color: #d6a24f;
}

.kyc-lock-box p {
  margin-bottom: 10px;
  font-size: 13px;
  color: white;
}

.kyc-lock-box button {
  margin-top: 15px;
  padding: 8px 18px;
  border: none;
  border-radius: 8px;
  background: #d6a24f;
  color: #fff;
  font-weight: bold;
  cursor: pointer;
}
</style>


<link rel="stylesheet" id="templateStyle" type="text/css" href="luxury-them/custom/css/refferal.css?v=2.6.0.0">

