<?php
// AMBIL DATA REKENING USER
 $qBank = mysqli_query(
  $koneksi,
  "SELECT * FROM tb_bank WHERE id_user = '$extplayer' LIMIT 1"
);
 $bank = mysqli_fetch_assoc($qBank);

// ================= TOTAL DEPOSIT VIP =================
 $qDeposit = mysqli_query($koneksi, "
  SELECT COALESCE(SUM(CAST(total AS UNSIGNED)),0) AS total_deposit
  FROM tb_transaksi
  WHERE transaksi = 'Top Up'
  AND status = 'Sukses'
  AND id_user = '$extplayer'
");

 $d = mysqli_fetch_assoc($qDeposit);
 $total_deposit = (int)$d['total_deposit'];

// AMBIL SALDO
 $query1 = mysqli_query(
  $koneksi,
  "SELECT active FROM tb_saldo WHERE id_user = '$extplayer'"
);
 $liat = mysqli_fetch_array($query1);

// AMBIL DATA USER
 $qUser = mysqli_query(
  $koneksi,
  "SELECT username, avatar FROM tb_user WHERE extplayer = '$extplayer' LIMIT 1"
);
 $punya_user = mysqli_fetch_assoc($qUser);

// LOGIC AVATAR (FALLBACK AMAN)
 $avatar = (!empty($punya_user['avatar']) && file_exists("../uploads/avatar/" . $punya_user['avatar']))
  ? "../uploads/avatar/" . $punya_user['avatar']
  : "../uploads/avatar/sm_avatar4.png";

// ================= VIP LOGIC BY DEPOSIT (REAL 1-100) =================
if ($total_deposit < 500000) {
  $vip_name   = 'NEW PLAYER';
  $vip_icon   = 'New Player.svg';
  $vip_min    = 0;
  $vip_max    = 500000;
  $next_tier  = 'SILVER';
} elseif ($total_deposit < 2000000) {
  $vip_name   = 'SILVER';
  $vip_icon   = 'Silver.svg';
  $vip_min    = 500000;
  $vip_max    = 2000000;
  $next_tier  = 'GOLD';
} elseif ($total_deposit < 5000000) {
  $vip_name   = 'GOLD';
  $vip_icon   = 'Gold.svg';
  $vip_min    = 2000000;
  $vip_max    = 5000000;
  $next_tier  = 'PLATINUM';
} else {
  $vip_name   = 'PLATINUM';
  $vip_icon   = 'Platinum.svg';
  $vip_progress = 100;
  $sisa_taruhan = 0;
  $next_tier = null;
}

// HITUNG PROGRESS (REAL)
if ($vip_name !== 'PLATINUM') {
  $vip_progress = (($total_deposit - $vip_min) / ($vip_max - $vip_min)) * 100;
  $sisa_taruhan = $vip_max - $total_deposit;
}

// SAFETY
 $vip_progress = min(100, max(0, $vip_progress));
?>

  <div class="none-header">
    <div class="left" onclick="window.location.href='?page=home'">
      <i class="fas fa-chevron-left"></i>  Akun Saya
    </div>
    
    <div class="right">
      <a href="function/logout.php" class="icon-btn-none">
        <i class="fas fa-sign-out-alt"></i> Keluar
      </a>
      
      <a href="javascript:void(0)" onclick="openLiveChat()" class="button-nav">
        <i class="fas fa-headset m-auto"></i>
      </a>
      
    </div>
  </div>
     
<main id="main-route" class="casino-profile">
  <div class="container">
    <main class="casino-dashboard">

      <!-- ================= HEADER PROFILE ================= -->
      <div class="profile-header">
        <div class="profile-left">
          <div class="avatar-img" data-toggle="modal" data-target="#avatarModal">
            <img src="<?= $avatar ?>" alt="Avatar">
            <span class="edit-avatar">Ubah</span>
          </div>

          <!-- User Info -->
          <div>
            <h4>
              <?= htmlspecialchars($punya_user['username']); ?>
              <span class="vip-level"><?= $vip_name ?></span>
            </h4>
            <p class="uid-row">
              UID:
              <span id="uidText"><?= $extplayer ?></span>
              <i class="fas fa-copy uid-copy" onclick="copyUID()"></i>
            </p>
          </div>
        </div>
      </div>
      <!-- ================= END HEADER PROFILE ================= -->

      <!-- ================= VIP CARD ================= -->
      <div class="vip-premium">
      
        <!-- VIP Icon -->
        <div class="vip-icon">
          <img src="../assets/img/img/<?= $vip_icon ?>">
        </div>
      
        <!-- VIP Information -->
        <div class="vip-info">
      
      <div class="vip-top">
        <strong><?= $vip_name ?></strong>
      
        <!-- INFO BADGE -->
      <span class="vip-info-badge" onclick="openVIPSheet()">?</span>
      
      </div>
      
          <small>
            <?php if ($vip_name !== 'PLATINUM'): ?>
              Perlu taruhan
              <b>Rp <?= number_format($sisa_taruhan, 0, ',', '.') ?></b>
              lagi untuk naik ke
              <b><?= $next_tier ?></b>
            <?php else: ?>
              VIP Tertinggi 🔥
            <?php endif; ?>
          </small>
      
          <!-- VIP Progress Bar -->
          <div class="vip-progress">
            <div style="width:<?= $vip_progress ?>%"></div>
          </div>
      
          <!-- Current Deposit -->
          <span class="vip-amount">
            Total Taruhan: Rp <?= number_format($total_deposit, 0, ',', '.') ?>
          </span>
      
        </div>
      </div>
      <!-- ================= END VIP CARD ================= -->

      <!-- ================= SALDO & ACTION ================= -->
      <div class="saldo-box">
        <!-- Saldo -->
        <div class="saldo-row">
          <span>Saldo</span>
          <strong>Rp <?= number_format($liat['active'], 2, ',', '.') ?></strong>
        </div>

        <!-- Action Buttons -->
        <div class="saldo-action">
          <a href="?page=deposit" class="btn-deposit">
            <i class="fas fa-wallet"></i> Deposit
          </a>

          <a href="?page=withdraw" class="btn-withdraw">
            <i class="fas fa-arrow-down"></i> Withdraw
          </a>
        </div>
      </div>
      <!-- ================= END SALDO & ACTION ================= -->

      <!-- ================= MENU GRID ================= -->
      <div class="menu-grid">
        <a href="?page=transaksi" class="menu-item">
          <i class="fas fa-sync-alt"></i>
          <span>Transaksi</span>
        </a>

        <a href="?page=riwayat" class="menu-item">
          <i class="fas fa-receipt"></i>
          <span>Riwayat</span>
        </a>

        <!-- TOMBOL REKENING DIUBAH JADI BUKA POPUP -->
        <div class="menu-item" data-toggle="modal" data-target="#bankModal">
          <i class="fas fa-university"></i>
          <span>Rekening</span>
        </div>
      </div>
      <!-- ================= END MENU GRID ================= -->

      <!-- ================= MENU LIST ================= -->
      <div class="menu-list">
        <a href="?page=promosi" class="menu-row">
          <i class="fas fa-bell"></i>
          <span>Promosi</span>
          <i class="fas fa-chevron-right"></i>
        </a>

        <a href="?page=referral" class="menu-row">
          <i class="fas fa-users"></i>
          <span>Reffral</span>
          <i class="fas fa-chevron-right"></i>
        </a>

        <div class="menu-row" data-toggle="modal" data-target="#usernameModal">
          <i class="fas fa-user-edit"></i>
          <span>Ubah Username</span>
          <i class="fas fa-chevron-right"></i>
        </div>

        <div class="menu-row" data-toggle="modal" data-target="#passwordModal">
          <i class="fas fa-key"></i>
          <span>Ubah Password</span>
          <i class="fas fa-chevron-right"></i>
        </div>
      </div>
      <!-- ================= END MENU LIST ================= -->

    </main>
  </div>
</main>

<!-- ======================================================
     MODAL BANK (POPUP REKENING)
     ====================================================== -->
<div class="modal fade casino-modal" id="bankModal" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <button class="close" data-dismiss="modal">&times;</button>
      <h3>Rekening Bank</h3>

      <?php if ($bank && $bank['nama_bank'] != '' && $bank['nomor_rekening'] != '') { ?>
        <!-- ===== KALO UDAH ADA REKENING (VIEW DATA) ===== -->
        <p>
          <span>Nama Pemilik</span>
          <strong><?= htmlspecialchars($bank['nama_pemilik']) ?></strong>
        </p>

        <p>
          <span>Bank</span>
          <strong><?= htmlspecialchars($bank['nama_bank']) ?></strong>
        </p>

        <p>
          <span>No Rekening</span>
          <strong><?= htmlspecialchars($bank['nomor_rekening']) ?></strong>
        </p>

        <button class="btn-dark full" disabled>
          TAMBAH REKENING
        </button>
      <?php } else { ?>
        <!-- ===== KALO BELUM ADA REKENING (FORM TAMBAH) ===== -->
        <form method="post" action="function/rekening.php">
          <div class="form-group">
            <select name="nama_bank" class="bank-select" required>
              <option value="">Pilih Bank</option>
              <option>BANK BCA</option>
              <option>BANK BNI</option>
              <option>BANK BRI</option>
              <option>BANK MANDIRI</option>
              <option>SEA BANK</option>
              <option>OVO</option>
              <option>DANA</option>
              <option>GOPAY</option>
            </select>
          </div>

          <div class="form-group">
            <input type="text" name="nama_pemilik" placeholder="Nama Pemilik Rekening" required>
          </div>

          <div class="form-group">
            <input type="text" name="nomor_rekening" placeholder="Nomor Rekening" required>
          </div>

          <button type="submit" name="submit" class="btn-gold full">
            Simpan Rekening
          </button>
        </form>
      <?php } ?>
    </div>
  </div>
</div>

<!-- ======================================================
     MODAL PASSWORD
     ====================================================== -->
<div class="modal fade casino-modal" id="passwordModal" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <button class="close" data-dismiss="modal">&times;</button>
      <h3>Ubah Password</h3>

      <form action="function/profil.php" method="POST">
        <input type="hidden" name="id" value="<?= $id_login ?>">

        <div class="form-group">
          <input type="password" name="password_lama" placeholder="Password Lama" required>
        </div>

        <div class="form-group">
          <input type="password" name="password_baru" placeholder="Password Baru" required minlength="8">
        </div>

        <div class="form-group">
          <input type="password" name="konfirmasi_password_baru" placeholder="Konfirmasi Password" required minlength="8">
        </div>

        <button type="submit" name="submit" class="btn-gold full">
          Simpan Password
        </button>
      </form>
    </div>
  </div>
</div>

<!-- ======================================================
     MODAL AVATAR
     ====================================================== -->
<div class="modal fade casino-modal" id="avatarModal" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <button class="close" data-dismiss="modal">&times;</button>
      <h3>Ubah Foto Profil</h3>

      <form action="function/avatar.php" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="extplayer" value="<?= $extplayer ?>">

        <!-- PREVIEW -->
        <div class="avatar-preview">
          <img id="previewAvatar" src="<?= $avatar ?>">
        </div>

        <!-- FILE INPUT (HIDDEN) -->
        <input type="file" id="avatarInput" name="avatar" accept="image/*" hidden required>

        <!-- BUTTON -->
        <button type="button" class="btn-outline" onclick="document.getElementById('avatarInput').click()">
          Pilih Foto
        </button>

        <button type="submit" class="btn-gold full mt-2">
          Simpan Foto
        </button>
      </form>
    </div>
  </div>
</div>

<!-- ======================================================
     MODAL USERNAME
     ====================================================== -->
<div class="modal fade casino-modal" id="usernameModal" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <button class="close" data-dismiss="modal">&times;</button>
      <h3>Ubah Username</h3>

      <form action="function/username.php" method="POST">
        <input type="hidden" name="extplayer" value="<?= $extplayer ?>">

        <div class="form-group">
          <input
            type="text"
            name="username"
            value="<?= htmlspecialchars($punya_user['username']) ?>"
            required
            minlength="4"
            maxlength="20"
          >
        </div>

        <button type="submit" class="btn-gold full">
          Simpan Username
        </button>
      </form>
    </div>
  </div>
</div>


<!-- ================= VIP INFO SHEET ================= -->
<div class="modal-sheet5-overlay" id="vipSheet">
  <div class="modal-sheet5-box">

    <div class="modal-sheet5-header">
      <div class="modal-sheet5-drag"></div>
      <span>Informasi Bonus VIP</span>
      <span class="modal-sheet5-close" onclick="closeVIPSheet()">✕</span>
    </div>

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

      <div class="step">
        <span class="step-num">🥈</span>
        <div>
          <h4>SILVER</h4>
          <p>Bonus <b>Rp 20.000</b></p>
        </div>
      </div>

      <div class="step">
        <span class="step-num">🥇</span>
        <div>
          <h4>GOLD</h4>
          <p>Bonus <b>Rp 60.000</b></p>
        </div>
      </div>

      <div class="step highlight">
        <span class="step-num">💎</span>
        <div>
          <h4>PLATINUM</h4>
          <p>Bonus <b>Rp 100.000</b></p>
        </div>
      </div>

      <div class="note">
        Claim bonus melalui <b>Livechat</b> atau  
        <b>WhatsApp Admin Official</b>.<br><br>
        Screenshot bukti level VIP kamu dan kirimkan ke admin.
        Bonus diproses maksimal <b>1x24 jam</b>.
      </div>

    </div>

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

  </div>
</div>

<script>
function openModalFromURL() {
  const params = new URLSearchParams(window.location.search);
  const modalId = params.get('modal');

  if (modalId) {
    const modalElement = document.getElementById(modalId);

    if (modalElement) {
      $('#' + modalId).modal({
        backdrop: 'static',
        keyboard: false
      });
    }
  }
}

document.addEventListener('DOMContentLoaded', openModalFromURL);
</script>

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

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

<script>
function copyUID(){
  const uid = document.getElementById("uidText").innerText;
  navigator.clipboard.writeText(uid)
    .then(() => showToast("UID berhasil disalin"));
}
</script>

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