<?php
/**
 * ✅ যারা সরাসরি এই Page-এ আসে (কোনো Agent-Link/URL Code
 * ছাড়া), তাদের জন্য Admin-এর সেট করা Default Invitation Code
 * এখানে বসিয়ে দেওয়া হচ্ছে।
 */
include __DIR__ . '/deepanshu/conn.php';

$defaultInviteCode = '';
$dcRes = $conn->query("SELECT setting_value FROM site_settings WHERE setting_key = 'default_invitation_code' LIMIT 1");
if ($dcRes) {
    $dcRow = $dcRes->fetch_assoc();
    if ($dcRow) $defaultInviteCode = $dcRow['setting_value'];
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
  <meta charset="UTF-8">
  <title>Register - Cv111</title>
  <meta name="description" content="Cv111-এ বিনামূল্যে Register করো এবং শুরু করো Online Gaming। Mobile Number দিয়ে মাত্র কয়েক Second-এই Account তৈরি করো।">
  <meta name="keywords" content="Cv111 register, online casino registration bangladesh, casino sign up, free registration">
  <link rel="canonical" href="https://cv111.site/newregister.php">
  <meta property="og:type" content="website">
  <meta property="og:title" content="Register - Cv111">
  <meta property="og:description" content="বিনামূল্যে Register করো এবং শুরু করো Online Gaming।">
  <meta property="og:url" content="https://cv111.site/newregister.php">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    }

    body {
      background: linear-gradient(rgba(1, 38, 30, 0.88), rgba(1, 38, 30, 0.88)),
                  url('https://images.unsplash.com/photo-1540747913346-19e32dc3e97e?auto=format&fit=crop&q=80&w=1000') no-repeat center center/cover;
      min-height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .container {
      width: 100%;
      max-width: 410px;
      padding: 20px 24px;
      position: relative;
    }

    .back-btn {
      color: #fca000;
      font-size: 20px;
      text-decoration: none;
      position: absolute;
      top: 0px;
      left: 20px;
    }

    .logo-container {
      text-align: center;
      margin-top: 10px;
      margin-bottom: 25px;
    }

    .logo {
      font-size: 34px;
      font-weight: 900;
      letter-spacing: -0.5px;
      display: inline-flex;
      align-items: center;
    }

    .logo .tk { color: #00e676; }
    .logo .divider { color: #2a664a; font-weight: 300; margin: 0 2px; }
    .logo .wintk { color: #ffffff; }

    h2.title {
      color: #fca000;
      text-align: center;
      font-size: 26px;
      font-weight: 700;
      margin-bottom: 6px;
    }

    .subtitle {
      text-align: center;
      color: #ffffff;
      font-size: 14px;
      margin-bottom: 22px;
      font-weight: 400;
    }

    .subtitle a {
      color: #00e676;
      text-decoration: underline;
      font-weight: 600;
    }

    .input-group {
      position: relative;
      margin-bottom: 14px;
    }

    .input-group i.left-icon {
      position: absolute;
      left: 18px;
      top: 50%;
      transform: translateY(-50%);
      color: #00e676;
      font-size: 17px;
    }

    .input-group i.right-icon {
      position: absolute;
      right: 18px;
      top: 50%;
      transform: translateY(-50%);
      color: #00e676;
      font-size: 17px;
      cursor: pointer;
    }

    .input-group input {
      width: 100%;
      padding: 13px 45px 13px 48px;
      background-color: #021a13;
      border: 1.5px solid #014d38;
      border-radius: 30px;
      color: #00e676;
      font-size: 15px;
      font-weight: 600;
      outline: none;
    }

    .input-group input.no-right-icon { padding-right: 18px; }

    .input-group input::placeholder {
      color: #00e676;
      opacity: 0.9;
      font-weight: 600;
    }

    .checkbox-container {
      display: flex;
      align-items: flex-start;
      margin-top: 14px;
      margin-bottom: 12px;
      cursor: pointer;
    }

    .checkbox-icon {
      width: 20px;
      height: 20px;
      min-width: 20px;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-right: 10px;
      margin-top: 1px;
      transition: background-color .15s ease;
    }

    .checkbox-icon.checked { background-color: #fca000; color: #01261e; }
    .checkbox-icon.unchecked { border: 1.5px solid #00e676; background-color: transparent; color: transparent; }

    .checkbox-icon i { font-size: 11px; font-weight: 900; }

    .checkbox-label {
      color: #ffffff;
      font-size: 12px;
      line-height: 1.35;
      font-weight: 500;
    }

    .register-btn {
      width: 100%;
      padding: 12px;
      margin-top: 15px;
      background: linear-gradient(to bottom, #ffcc00, #ff8800);
      border: none;
      border-radius: 30px;
      color: #632600;
      font-size: 18px;
      font-weight: 700;
      cursor: pointer;
      box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    }
    .register-btn:disabled { opacity: .7; cursor: not-allowed; }

    .error-box {
      display: none;
      background: rgba(255,92,92,.12);
      border: 1px solid rgba(255,92,92,.4);
      color: #ff8a8a;
      font-size: 13px;
      font-weight: 600;
      padding: 10px 14px;
      border-radius: 12px;
      margin-bottom: 14px;
    }

    .social-login {
      display: flex;
      justify-content: center;
      gap: 16px;
      margin-top: 22px;
    }

    .social-btn {
      width: 44px;
      height: 44px;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      text-decoration: none;
      border: none;
      cursor: pointer;
    }

    .facebook { background-color: #3b5998; color: #ffffff; font-size: 22px; }
    .google { background-color: #ffffff; }
    .google-icon { width: 22px; height: 22px; }

    .toast {
      position: fixed;
      top: 20px;
      left: 50%;
      transform: translateX(-50%);
      background: #021a13;
      border: 1px solid #00e676;
      color: #fff;
      padding: 12px 20px;
      border-radius: 12px;
      font-size: 13px;
      z-index: 99;
      display: none;
    }
  </style>
</head>
<body>
<?php $pt = __DIR__ . '/page_transition.php'; if (file_exists($pt)) { include $pt; } ?>

  <div class="container">
    <a href="/newhome.php" class="back-btn"><i class="fa-solid fa-chevron-left"></i></a>

    <div class="logo-container">
      <div class="logo">
        <span class="tk">CV</span><span class="divider">|</span><span class="wintk">111</span>
      </div>
    </div>

    <h2 class="title">Register</h2>
    <p class="subtitle">Already have an account? <a href="/newlogin.php">Login</a></p>

    <div id="errorBox" class="error-box"></div>

    <form onsubmit="return false;">
      <div class="input-group">
        <i class="fa-solid fa-user left-icon"></i>
        <input type="text" id="fullName" class="no-right-icon" placeholder="Full name">
      </div>

      <div class="input-group">
        <i class="fa-solid fa-phone left-icon"></i>
        <input type="tel" id="username" class="no-right-icon" placeholder="Phone number">
      </div>

      <div class="input-group">
        <i class="fa-solid fa-lock left-icon"></i>
        <input type="password" id="password" placeholder="Password">
        <i class="fa-solid fa-eye-slash right-icon" id="eyeIcon1" onclick="togglePwd('password','eyeIcon1')"></i>
      </div>

      <div class="input-group">
        <i class="fa-solid fa-lock left-icon"></i>
        <input type="password" id="confirmPassword" placeholder="Confirm password">
        <i class="fa-solid fa-eye-slash right-icon" id="eyeIcon2" onclick="togglePwd('confirmPassword','eyeIcon2')"></i>
      </div>

      <div class="input-group">
        <i class="fa-solid fa-ticket left-icon"></i>
        <input type="text" id="invitecode" class="no-right-icon" placeholder="Invitation Code (optional)">
      </div>

      <div class="checkbox-container" onclick="toggleCheck('ageCheck')">
        <div class="checkbox-icon checked" id="ageCheckIcon">
          <i class="fa-solid fa-check"></i>
        </div>
        <div class="checkbox-label">
          I am over 18 years of age and have read and accepted Terms &amp; Conditions, Privacy Policy &amp; Betting Rules as published on the site.
        </div>
      </div>

      <div class="checkbox-container" onclick="toggleCheck('promoCheck')">
        <div class="checkbox-icon unchecked" id="promoCheckIcon">
          <i class="fa-solid fa-check"></i>
        </div>
        <div class="checkbox-label">
          I would like to receive details of special offers, free bets and other promotions.
        </div>
      </div>

      <button type="submit" class="register-btn" id="registerBtn" onclick="doRegister()">Register</button>
    </form>

    <div class="social-login">
      <button class="social-btn facebook" onclick="comingSoon()" type="button">
        <i class="fa-brands fa-facebook-f"></i>
      </button>
      <button class="social-btn google" onclick="comingSoon()" type="button">
        <svg class="google-icon" viewBox="0 0 24 24">
          <path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"/>
          <path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/>
          <path fill="#FBBC05" d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.06H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.94l2.85-2.22.81-.63z"/>
          <path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.06l3.66 2.84c.87-2.6 3.3-4.52 6.16-4.52z"/>
        </svg>
      </button>
    </div>
  </div>

  <div class="toast" id="toast">শীঘ্রই আসছে! আপাতত Mobile দিয়ে Register করো।</div>

<script>
// ✅ Referral-Link (যেমন ?code=XXXX) থেকে Invitation-Code
// স্বয়ংক্রিয়ভাবে বসানো, না থাকলে Admin-এর Default Code
(function() {
    const urlParams = new URLSearchParams(window.location.search);
    const refCode = urlParams.get('code') || urlParams.get('r_code') || urlParams.get('invitecode');
    const defaultCode = <?php echo json_encode($defaultInviteCode); ?>;
    if (refCode) {
        document.getElementById('invitecode').value = refCode;
    } else if (defaultCode) {
        document.getElementById('invitecode').value = defaultCode;
    }
})();

let ageChecked = true;
let promoChecked = false;

function toggleCheck(name) {
    if (name === 'ageCheck') {
        ageChecked = !ageChecked;
        const el = document.getElementById('ageCheckIcon');
        el.className = 'checkbox-icon ' + (ageChecked ? 'checked' : 'unchecked');
    } else {
        promoChecked = !promoChecked;
        const el = document.getElementById('promoCheckIcon');
        el.className = 'checkbox-icon ' + (promoChecked ? 'checked' : 'unchecked');
    }
}

function comingSoon() {
    const toast = document.getElementById('toast');
    toast.style.display = 'block';
    setTimeout(() => toast.style.display = 'none', 2500);
}

function togglePwd(inputId, iconId) {
    const input = document.getElementById(inputId);
    const icon = document.getElementById(iconId);
    if (input.type === 'password') {
        input.type = 'text';
        icon.className = 'fa-solid fa-eye right-icon';
    } else {
        input.type = 'password';
        icon.className = 'fa-solid fa-eye-slash right-icon';
    }
}

function md5(str) {
    function rotateLeft(n, s) { return (n << s) | (n >>> (32 - s)); }
    function toHex(n) { let s = '', v; for (let i = 0; i < 4; i++) { v = (n >>> (i * 8)) & 255; s += ('0' + v.toString(16)).slice(-2); } return s; }
    function utf8Encode(str) { return unescape(encodeURIComponent(str)); }
    str = utf8Encode(str);
    const msgLen = str.length;
    const numBlocks = (((msgLen + 8) >>> 6) + 1) * 16;
    const M = new Array(numBlocks).fill(0);
    for (let i = 0; i < msgLen; i++) M[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
    M[msgLen >> 2] |= 0x80 << ((msgLen % 4) * 8);
    M[numBlocks - 2] = msgLen * 8;
    let a = 1732584193, b = -271733879, c = -1732584194, d = 271733878;
    const S = [7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21];
    const K = [];
    for (let i = 0; i < 64; i++) K[i] = (Math.floor(Math.abs(Math.sin(i + 1)) * 4294967296)) | 0;
    for (let i = 0; i < numBlocks; i += 16) {
        let A = a, B = b, C = c, D = d;
        for (let j = 0; j < 64; j++) {
            let F, g;
            if (j < 16) { F = (B & C) | (~B & D); g = j; }
            else if (j < 32) { F = (D & B) | (~D & C); g = (5 * j + 1) % 16; }
            else if (j < 48) { F = B ^ C ^ D; g = (3 * j + 5) % 16; }
            else { F = C ^ (B | ~D); g = (7 * j) % 16; }
            F = (F + A + K[j] + (M[i + g] | 0)) | 0;
            A = D; D = C; C = B;
            B = (B + rotateLeft(F, S[j])) | 0;
        }
        a = (a + A) | 0; b = (b + B) | 0; c = (c + C) | 0; d = (d + D) | 0;
    }
    return toHex(a) + toHex(b) + toHex(c) + toHex(d);
}

async function doRegister() {
    const fullName = document.getElementById('fullName').value.trim();
    const username = document.getElementById('username').value.trim();
    const password = document.getElementById('password').value;
    const confirmPassword = document.getElementById('confirmPassword').value;
    const invitecode = document.getElementById('invitecode').value.trim();
    const errorBox = document.getElementById('errorBox');
    const btn = document.getElementById('registerBtn');

    errorBox.style.display = 'none';

    if (!fullName || !username || !password || !confirmPassword) {
        errorBox.style.display = 'block';
        errorBox.innerHTML = '<i class="fa-solid fa-circle-exclamation"></i> নাম, Phone, Password, Confirm Password — সব দাও।';
        return;
    }

    if (password !== confirmPassword) {
        errorBox.style.display = 'block';
        errorBox.innerHTML = '<i class="fa-solid fa-circle-exclamation"></i> Password আর Confirm Password মিলছে না।';
        return;
    }

    if (!ageChecked) {
        errorBox.style.display = 'block';
        errorBox.innerHTML = '<i class="fa-solid fa-circle-exclamation"></i> Terms & Conditions মেনে নিতে হবে।';
        return;
    }

    btn.disabled = true;
    btn.textContent = 'তৈরি হচ্ছে...';

    const domainurl = window.location.hostname;
    const language = 1, phonetype = 2, registerType = 'mobile';
    const random = Math.random().toString(36).substring(2, 15);
    const shonustr = `{"domainurl":"${domainurl}","invitecode":"${invitecode}","language":${language},"phonetype":${phonetype},"pwd":"${password}","random":"${random}","registerType":"${registerType}","username":"${username}"}`;
    const signature = md5(shonustr).toUpperCase();

    try {
        const res = await fetch('/deepanshu/api/webapi/Register.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ domainurl, invitecode, language, phonetype, pwd: password, random, registerType, signature, username, name: fullName, timestamp: Date.now() })
        });
        const data = await res.json();

        if (data.code === 0) {
            alert('✅ Account তৈরি হয়েছে! এখন Login করো।');
            window.location.href = '/newlogin.php';
        } else {
            errorBox.style.display = 'block';
            errorBox.innerHTML = '<i class="fa-solid fa-circle-exclamation"></i> ' + (data.msg || 'Register ব্যর্থ হয়েছে।');
            btn.disabled = false;
            btn.textContent = 'Register';
        }
    } catch (e) {
        errorBox.style.display = 'block';
        errorBox.innerHTML = '<i class="fa-solid fa-circle-exclamation"></i> একটা সমস্যা হয়েছে, আবার চেষ্টা করো।';
        btn.disabled = false;
        btn.textContent = 'Register';
    }
}
</script>

</body>
</html>
