<?php
/* ===== TURNOVER ANTI-ABUSE (VIEW) ===== */

include '../play/game/functions.php';

 $qTrans = mysqli_query($koneksi, "
  SELECT COUNT(*) AS total
  FROM tb_transaksi
  WHERE id_user = '$extplayer'
  AND transaksi IN ('Top Up','Withdraw')
");
 $dTrans = mysqli_fetch_assoc($qTrans);
 $totalTransaksi = (int)$dTrans['total'];

// ===== CEK PERNAH DEPOSIT =====
 $qCekDeposit = mysqli_query($koneksi, "
  SELECT 1 FROM tb_transaksi
  WHERE id_user='$extplayer'
  AND transaksi='Top Up'
  AND status='Sukses' LIMIT 1
");
 $pernahDeposit = mysqli_num_rows($qCekDeposit) > 0;

// ===== SALDO =====
 $qSaldoFull = mysqli_query($koneksi, "
  SELECT active, pending FROM tb_saldo WHERE id_user='$extplayer'
");
 $sf = mysqli_fetch_assoc($qSaldoFull);
 $saldoAvailable = (int)$sf['active'];
 $saldoTahan     = (int)$sf['pending'];

// ===== STATUS =====
 $statusWD = 'AMAN';

 $bankKosong = (
  empty($b['nama_bank']) ||
  empty($b['nomor_rekening']) ||
  empty($b['nama_pemilik'])
);
?>

<main class="withdraw-page">
  <!-- HEADER -->

  <div class="none-header">
    <div class="left" onclick="window.location.href='?page=home'">
      <i class="fas fa-chevron-left"></i> Withdraw
    </div>
    <div class="right">
      <a href="?page=riwayat&start_date=2025-01-01" class="icon-btn-none">
        <i class="fas fa-history"></i> Riwayat
      </a>
      <a href="?page=deposit" class="icon-btn-none">
        <i class="fas fa-exchange-alt"></i> Deposit
      </a>
      <a href="javascript:void(0)" onclick="openLiveChat()" class="button-nav">
        <i class="fas fa-headset m-auto"></i>
      </a>
    </div>
  </div>

  <form method="post" action="function/withdraw.php">
    
    <!-- ================= REKENING ================= -->
<div class="card">
  <div class="card-title">Rekening Bank</div>

  <?php if (
    empty($b['nama_bank']) ||
    empty($b['nomor_rekening']) ||
    empty($b['nama_pemilik'])
  ): ?>
    <div class="bank-item">
      <div class="bank-info">
        <div class="bank-number">Belum ada rekening</div>
        <div class="bank-name">Tambahkan rekening terlebih dahulu</div>
      </div>
      <a href="?page=profile&modal=bankModal" class="btn-add">Tambah</a>
    </div>
  <?php else: ?>
    <input type="hidden" name="bank" value="<?= $b['id']; ?>">

    <div class="bank-item">
      <div class="bank-info">
        <div class="bank-number">
          <?= $b['nama_bank']; ?> -
          <?= substr($b['nomor_rekening'], 0, 4); ?>****<?= substr($b['nomor_rekening'], -3); ?>
        </div>
        <div class="bank-name">
          A.n <?= $b['nama_pemilik']; ?>
        </div>
      </div>
      <i class="fas fa-chevron-right"></i>
    </div>
  <?php endif; ?>
</div>

  <div class="card info-balance">
  <div class="card-title">Informasi Akun</div>

      <div class="info-row">
        <span>Saldo</span>
        <b class="text-success">
          Rp <?= number_format($saldoAvailable,0,',','.') ?>
        </b>
      </div>
      
        <div class="info-row">
        <span>Saldo Pending</span>
        <b class="text-warning">
          Rp <?= number_format($saldoTahan,0,',','.') ?>
        </b>
      </div>
    </div>

    <!-- ================= JUMLAH ================= -->
    <div class="card">
      <div class="card-title">Withdraw Jumlah</div>

      <div class="wd-amount">
        <span class="wd-prefix">Rp</span>
        <input type="number"
               name="jumlah"
               class="wd-input"
               min="100000"
               max="<?= (int)$liat['active'] ?>"
               placeholder="Minimal 100.000"
               required>

        <button type="button"
                class="wd-all"
                onclick="
                  const saldo = <?= (int)$liat['active']; ?>;
                  const input = document.querySelector('.wd-input');
                  input.value = saldo;
                  input.dispatchEvent(new Event('input'));
                ">
          ALL
        </button>
      </div>

      <div class="fee-row total">
        <span>Mendapatkan</span>
        <span id="totalText">Rp 0</span>
      </div>

      <div id="loadingOverlay" class="circle-loading">
        <div class="circle-box">
          <div class="circle-spinner"></div>
          <div class="loading-text">
            Mohon tunggu sebentar
          </div>
          <div class="loading-text" id="loadingText">
            Memvalidasi akun...
          </div>

          <button type="submit"
                  name="submit"
                  id="btnLanjutkan"
                  style="display:none; margin-top:16px;"
                  class="btn-next">
            Lanjutkan
          </button>
        </div>
      </div>
      

      <div class="withdraw-footer">
      <button type="button"
              class="btn-next"
              id="btnWithdraw"
              <?= $bankKosong ? 'disabled' : '' ?>
              onclick="<?= $bankKosong ? '' : 'cekDepositSebelumWithdraw()' ?>">
        <?= $bankKosong ? 'SILAHKAN TAMBAH BANK' : 'Berikutnya' ?>
      </button>
      </div>

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

<div id="modalDepositWarning" class="wd-modal">
  <div class="wd-modal-box">
    <div class="wd-modal-icon">
      <i class="fas fa-exclamation-triangle"></i>
    </div>

    <h2>Akses Withdraw Ditolak</h2>

    <p>
      Sistem tidak pernah menemukan <b>riwayat deposit</b> pada akun Anda.
      <br><br>
      <span class="text-danger">
        Dari mana saldo chips Anda bertambah?
      </span>
      <br><br>
      Jika ini kesalahan sistem, silakan hubungi admin.
    </p>

    <div class="wd-modal-actions">
      <button class="btn-secondary" onclick="tutupModal()">Tutup</button>
      <a href="javascript:void(0)" onclick="openLiveChat()" class="btn-primary">
        <i class="fas fa-headset"></i> Hubungi Admin
      </a>
    </div>
  </div>
</div>

<div id="modalTurnoverWarning" class="wd-modal">
  <div class="wd-modal-box">
    <div class="wd-modal-icon">
      <i class="fas fa-sync-alt"></i>
    </div>

    <h2>Abuse bank</h2>

    <p>
      anda terdeteksi<b>deposit mengunakan rekening atas nama yang berbeda</b><br>
      silahkan hubungi admin untuk menindaklanjuti keamanan akun anda
      <br><br>
      jika bukan anda yang melakukan silahkan tindaklanjuti 
.jika itu anda abaikan dan hub admin untuk proses buka
    </p>

    <div class="wd-modal-actions">
      <button class="btn-secondary" onclick="tutupModal()">Tutup</button>
    </div>
  </div>
</div>

<script>
const STATUS_WD = "<?= $statusWD ?>";
const PERNAH_DEPOSIT = <?= $pernahDeposit ? 'true' : 'false' ?>;

function cekDepositSebelumWithdraw() {
  const overlay = document.getElementById('loadingOverlay');
  const text = document.getElementById('loadingText');
  const btnLanjut = document.getElementById('btnLanjutkan');
  
  overlay.style.display = 'flex';
  btnLanjut.style.display = 'none';
  
  const texts = [
    'Memvalidasi akun...',
    'Memeriksa transaksi terakhir...',
    'Menganalisis aktivitas akun...',
    'Mengonfirmasi keamanan akun...'
  ];
  
  let i = 0;
  text.innerText = texts[i];
  
  const interval = setInterval(() => {
    i = (i + 1) % texts.length;
    text.innerText = texts[i];
  }, 1500);
  
  setTimeout(() => {
    clearInterval(interval);
    overlay.style.display = 'none';
    
    if (STATUS_WD === 'NO_DEPOSIT') {
      document.getElementById('modalDepositWarning').style.display = 'flex';
      return;
    }
    
    overlay.style.display = 'flex';
    btnLanjut.style.display = 'block';
    text.innerText = 'Akun valid, klik Lanjutkan untuk withdraw';
    
  }, 6000);
}

function tutupModal() {
  const modals = document.querySelectorAll('.wd-modal');
  modals.forEach(m => m.style.display = 'none');
}
</script>

<script>
const SALDO = <?= (int)$liat['active']; ?>;
const MIN_WD = 50000;

const inputJumlah = document.querySelector('.wd-input');
const totalText   = document.getElementById('totalText');
const btnWithdraw = document.getElementById('btnWithdraw');

function cekNominal() {
  let jumlah = parseInt(inputJumlah.value) || 0;

  if (jumlah > SALDO) {
    jumlah = SALDO;
    inputJumlah.value = SALDO;
  }

  totalText.innerText = 'Rp ' + jumlah.toLocaleString('id-ID');

  if (jumlah >= MIN_WD && jumlah <= SALDO) {
    btnWithdraw.disabled = false;
  } else {
    btnWithdraw.disabled = true;
  }
}

inputJumlah.addEventListener('input', cekNominal);
</script>

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