<main id="main-route">
  
    <div class="none-header">
        <div class="left" onclick="window.location.href='?page=home'">
            <i class="fas fa-chevron-left"></i> Back
        </div>
        <div class="right">
            <a href="#" class="icon-btn-none" data-toggle="modal" data-target="#loginModal">
                <i class="fas fa-user"></i> Login
            </a>
            <a href="javascript:void(0)" onclick="openLiveChat()" class="button-nav">
                <i class="fas fa-headset m-auto"></i>
            </a>
        </div>
    </div>
    
    <div class="register-page">
        <div class="register-box">

            <div class="register-header">
                <img src="../assets/img/<?php echo $logo ?>" class="register-logo">
                <h3>Daftar Akun Baru</h3>
                <p>Lengkapi data dengan benar</p>
            </div>
            
            <div class="register-info">
                <i class="fas fa-info-circle"></i>
                <span>
                    Pastikan <b>nama rekening</b> sesuai KTP untuk verifikasi Withdraw.
                </span>
            </div>

            <form method="POST" action="function/daftar_akun.php" class="register-form" id="regForm">

                <div class="reg-input username-wrap">
                    <i class="fas fa-user"></i>
                    <input type="text" id="username" name="username" minlength="4" maxlength="15" placeholder="Nama Pengguna" required>
                    <span id="usernameStatus" class="input-status"></span>
                </div>
                
                <div class="reg-input phone-wrap">
                    <i class="fas fa-phone"></i>
                    <input type="text" id="no_whatsapp" name="no_whatsapp" placeholder="08xxxxxxxxxx" maxlength="13" required>
                    <span id="phoneStatus" class="input-status"></span>
                </div>
            
                <div class="reg-input">
                    <i class="fas fa-envelope"></i>
                    <input type="email" name="email" placeholder="Email Aktif" required>
                </div> 

                <div class="reg-input password-wrap">
                    <i class="fas fa-lock"></i>
                    <input type="password" id="password" name="password" minlength="8" maxlength="25" placeholder="Kata Sandi" required>
                    <span class="toggle-pass" data-target="password"><i class="fas fa-eye"></i></span>
                </div>
                
                <div class="reg-input password-wrap">
                    <i class="fas fa-lock"></i>
                    <input type="password" id="konfirmasi_pass" name="konfirmasi_pass" minlength="8" maxlength="25" placeholder="Konfirmasi Kata Sandi" required>
                    <span class="toggle-pass" data-target="konfirmasi_pass"><i class="fas fa-eye"></i></span>
                </div>

                <?php if (isset($_GET['reff'])) { $reff = $_GET['reff']; } ?>
                
                <div class="reg-input">
                    <i class="fas fa-users"></i>
                    <input type="text" name="refferal" value="<?php echo $reff ?? ''; ?>" readonly placeholder="Referral">
                </div>

                <!-- ================= TAMBAHAN REKENING BANK ================= -->
                <div class="reg-input">
                    <i class="fas fa-university"></i>
                    <select name="nama_bank" 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="reg-input">
                    <i class="fas fa-id-card"></i>
                    <input type="text" name="nama_pemilik" placeholder="Nama Pemilik Rekening" required>
                </div>

                <div class="reg-input">
                    <i class="fas fa-credit-card"></i>
                    <input type="text" name="nomor_rekening" placeholder="Nomor Rekening" required>
                </div>
                <!-- ================= END TAMBAHAN REKENING BANK ================= -->

                <!-- CAPTCHA -->
                <div class="captcha-bar">
                    <div class="captcha-display">
                        <div class="captcha-code" id="captchaCode"><?php echo rand(100000, 999999); ?></div>
                        <button type="button" class="captcha-refresh" id="refreshCaptcha" title="Refresh">
                            <i class="fas fa-sync-alt"></i>
                        </button>
                    </div>
                    <input type="number" name="captcha" id="captchaInput" placeholder="Masukkan kode di atas" required>
                    <input type="hidden" name="captcha_asli" id="captchaAsli" value="">
                </div>

                <!-- TERMS -->
                <label class="reg-terms">
                    <span class="terms-checkbox">
                        <input type="checkbox" id="termsCheck" required>
                        <span class="checkmark"></span>
                    </span>
                    <span>Saya berusia 18+ dan setuju dengan syarat & ketentuan</span>
                </label>

                <button type="submit" name="submit" class="register-btn" id="regBtn">Daftar Sekarang</button>

                <div class="register-footer">
                    Sudah punya akun? <a href="#" data-toggle="modal" data-target="#loginModal">Masuk</a>
                </div>
            </form>
        </div>
    </div>
</main>

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

<style>
/* Input status */
.input-status {
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
    opacity: 0;
    transition: opacity 0.2s;
}
.input-status:empty { opacity: 0 !important; }
.input-status:not(:empty) { opacity: 1; }
.input-status.ok { color: #2dd4a0; }
.input-status.error { color: #f04060; }
.input-status.loading { color: #888; }
.input-status.loading::before {
    content: '';
    width: 12px; height: 12px;
    border: 2px solid #333;
    border-top-color: #888;
    border-radius: 50%;
    animation: spinS 0.6s linear infinite;
    display: inline-block;
}
@keyframes spinS { to { transform: rotate(360deg); } }

/* Select Dropdown Fix */
.reg-input select {
    width: 100%;
    background: transparent;
    border: none;
    color: #fff;
    font-size: 14px;
    outline: none;
    appearance: none;
    -webkit-appearance: none;
    cursor: pointer;
}
.reg-input select option {
    background: #1a1a2e;
    color: #fff;
}

/* Captcha */
.captcha-bar {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.captcha-display {
    display: flex;
    gap: 10px;
    height: 48px;
}

.captcha-code {
    flex: 1;
    height: 100%;
    background: #fff;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: 'Courier New', monospace;
    font-size: 22px;
    font-weight: 700;
    letter-spacing: 8px;
    color: #111;
    user-select: none;
}

.captcha-refresh {
    width: 48px;
    height: 100%;
    border-radius: 10px;
    border: 1px solid rgba(255,255,255,0.1);
    background: rgba(255,255,255,0.06);
    color: #aaa;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}
.captcha-refresh:hover { background: rgba(255,255,255,0.1); color: #fff; }
.captcha-refresh:active { transform: scale(0.92); }
.captcha-refresh.spinning i { animation: spinR 0.4s ease; }
@keyframes spinR { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

.captcha-bar input[type="number"] {
    width: 100%;
    height: 48px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 10px;
    padding: 0 16px;
    font-size: 14px;
    color: #fff;
    outline: none;
    transition: border-color 0.2s;
    box-sizing: border-box;
    -moz-appearance: textfield;
}
.captcha-bar input[type="number"]::-webkit-outer-spin-button,
.captcha-bar input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.captcha-bar input[type="number"]:focus { border-color: rgba(255,255,255,0.25); }
.captcha-bar input[type="number"]::placeholder { color: #555; }

/* Terms checkbox */
.reg-terms {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    cursor: pointer;
    font-size: 12px;
    color: #888;
    line-height: 1.5;
    user-select: none;
}
.terms-checkbox {
    position: relative;
    display: flex;
    align-items: center;
    flex-shrink: 0;
    margin-top: 1px;
}
.terms-checkbox input { position: absolute; opacity: 0; width: 0; height: 0; }
.checkmark {
    width: 18px; height: 18px;
    border-radius: 5px;
    border: 2px solid #444;
    background: rgba(255,255,255,0.03);
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}
.terms-checkbox input:checked + .checkmark { background: #fff; border-color: #fff; }
.terms-checkbox input:checked + .checkmark::after {
    content: '';
    width: 5px; height: 9px;
    border: solid #111; border-width: 0 2px 2px 0;
    transform: rotate(45deg) translate(-1px, -1px);
}
.terms-checkbox input:focus-visible + .checkmark { box-shadow: 0 0 0 2px rgba(255,255,255,0.2); }
</style>

<script>
function generateCaptcha() {
    const code = Math.floor(100000 + Math.random() * 900000).toString();
    document.getElementById('captchaCode').textContent = code;
    document.getElementById('captchaAsli').value = code;
    document.getElementById('captchaInput').value = '';
}

document.getElementById('refreshCaptcha').addEventListener('click', function() {
    this.classList.add('spinning');
    setTimeout(() => { generateCaptcha(); this.classList.remove('spinning'); }, 200);
});

generateCaptcha();

document.getElementById('regForm').addEventListener('submit', function() {
    document.getElementById('captchaAsli').value = document.getElementById('captchaCode').textContent;
});

/* Username check */
const usernameInput = document.getElementById('username');
const usernameStatus = document.getElementById('usernameStatus');
let checkTimer = null;

usernameInput.addEventListener('input', function() {
    const u = this.value.trim();
    usernameStatus.textContent = '';
    usernameStatus.className = 'input-status';
    if (!u) return;
    if (/[^a-zA-Z0-9_]/.test(u)) { usernameStatus.textContent = 'Hanya huruf, angka, _'; usernameStatus.className = 'input-status error'; return; }
    if (/maxwin/i.test(u)) { usernameStatus.textContent = 'Kata dilarang'; usernameStatus.className = 'input-status error'; return; }
    if (u.length < 6) { usernameStatus.textContent = 'Min 6 karakter'; usernameStatus.className = 'input-status error'; return; }
    usernameStatus.className = 'input-status loading';
    clearTimeout(checkTimer);
    checkTimer = setTimeout(() => {
        fetch('function/ajax_check_username.php', { method: 'POST', headers: {'Content-Type':'application/x-www-form-urlencoded'}, body: 'username=' + encodeURIComponent(u) })
        .then(r => r.json())
        .then(res => {
            usernameStatus.textContent = res.available ? 'Tersedia' : 'Sudah dipakai';
            usernameStatus.className = 'input-status ' + (res.available ? 'ok' : 'error');
        })
        .catch(() => { usernameStatus.textContent = 'Gagal cek'; usernameStatus.className = 'input-status error'; });
    }, 400);
});

/* Phone validation */
const phoneInput = document.getElementById('no_whatsapp');
const phoneStatus = document.getElementById('phoneStatus');

phoneInput.addEventListener('input', function() {
    let v = this.value.replace(/\D/g, '');
    this.value = v;
    phoneStatus.textContent = '';
    phoneStatus.className = 'input-status';
    if (!v) return;
    if (!v.startsWith('08')) { phoneStatus.textContent = 'Gunakan 08xxxx'; phoneStatus.className = 'input-status error'; return; }
    if (v.length < 10) { phoneStatus.textContent = 'Terlalu pendek'; phoneStatus.className = 'input-status error'; return; }
    if (v.length > 13) { phoneStatus.textContent = 'Terlalu panjang'; phoneStatus.className = 'input-status error'; return; }
    if (/^08(\d)\1{6,}$/.test(v)) { phoneStatus.textContent = 'Tidak valid'; phoneStatus.className = 'input-status error'; return; }
    phoneStatus.textContent = 'Valid'; phoneStatus.className = 'input-status ok';
});

/* Toggle password */
document.querySelectorAll('.toggle-pass').forEach(btn => {
    btn.addEventListener('click', function() {
        const inp = document.getElementById(this.getAttribute('data-target'));
        const ico = this.querySelector('i');
        if (inp.type === 'password') { inp.type = 'text'; ico.classList.replace('fa-eye', 'fa-eye-slash'); }
        else { inp.type = 'password'; ico.classList.replace('fa-eye-slash', 'fa-eye'); }
    });
});
</script>