<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<title>Security Center - CV111</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
    body { background: #0b1d1a; color: #d8d3c8; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; margin: 0; }
    .header-bg { background: linear-gradient(160deg, #133a2e 0%, #0b1d1a 80%); padding: 18px 16px; border-bottom: 1px solid rgba(0,230,118,.15); position: sticky; top: 0; display: flex; align-items: center; gap: 12px; }
    .header-bg a { color: #ffd54f; text-decoration: none; font-size: 20px; }
    .header-bg span { color: #fff; font-weight: 800; font-size: 17px; }
    main { max-width: 600px; margin: 0 auto; padding: 20px 16px 50px; }
    .card { background: #102620; border: 1px solid rgba(0,230,118,.15); border-radius: 14px; padding: 20px; margin-bottom: 16px; }
    h2 { color: #ffd54f; font-size: 16px; margin: 0 0 14px; display: flex; align-items: center; gap: 8px; }
    label { display: block; color: #9fb8ae; font-size: 12.5px; margin-bottom: 6px; }
    input { width: 100%; padding: 12px; border-radius: 10px; border: 1px solid rgba(0,230,118,.2); background: #0b1d1a; color: #fff; font-size: 14px; margin-bottom: 14px; }
    button { width: 100%; padding: 13px; border-radius: 10px; border: none; font-weight: 800; font-size: 14px; cursor: pointer; }
    .btn-primary { background: linear-gradient(135deg, #00c853, #00e676); color: #06251c; }
    .link-item { display: flex; align-items: center; justify-content: space-between; padding: 14px 0; border-bottom: 1px solid rgba(255,255,255,.06); text-decoration: none; color: #d8d3c8; }
    .link-item:last-child { border-bottom: none; }
    .msg-box { border-radius: 10px; padding: 12px; margin-bottom: 14px; font-size: 12.5px; font-weight: 700; text-align: center; display: none; }
</style>
</head>
<body>

<div class="header-bg">
    <a href="/newprofile.php"><i class="fa-solid fa-arrow-left"></i></a>
    <span>Security Center</span>
</div>

<main>
    <div id="msgBox" class="msg-box"></div>

    <div class="card">
        <h2><i class="fa-solid fa-key"></i> Password বদলাও</h2>
        <label>বর্তমান Password</label>
        <input type="password" id="oldPwd">
        <label>নতুন Password</label>
        <input type="password" id="newPwd">
        <label>নতুন Password আবার লিখো</label>
        <input type="password" id="confirmPwd">
        <button class="btn-primary" onclick="changePassword()" id="pwdBtn">Password Update করো</button>
    </div>

    <div class="card">
        <h2><i class="fa-solid fa-shield-heart"></i> নিরাপত্তা সংক্রান্ত</h2>
        <a href="/newresponsiblegambling.php" class="link-item">
            <span>Deposit Limit / Self-Exclusion</span>
            <i class="fa-solid fa-chevron-right"></i>
        </a>
        <a href="/newprivacy.php" class="link-item">
            <span>Privacy Policy</span>
            <i class="fa-solid fa-chevron-right"></i>
        </a>
    </div>
</main>

<script>
const token = localStorage.getItem('token');
if (!token) window.location.href = '/newlogin.php';

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 changePassword() {
    const oldPwd = document.getElementById('oldPwd').value;
    const newPwd = document.getElementById('newPwd').value;
    const confirmPwd = document.getElementById('confirmPwd').value;
    const msgBox = document.getElementById('msgBox');
    const btn = document.getElementById('pwdBtn');

    if (!oldPwd || !newPwd || !confirmPwd) {
        msgBox.style.display = 'block'; msgBox.style.background = 'rgba(232,50,60,.1)'; msgBox.style.color = '#e8323c';
        msgBox.textContent = 'সব Field পূরণ করো।'; return;
    }
    if (newPwd !== confirmPwd) {
        msgBox.style.display = 'block'; msgBox.style.background = 'rgba(232,50,60,.1)'; msgBox.style.color = '#e8323c';
        msgBox.textContent = 'নতুন Password দুটো মিলছে না।'; return;
    }

    btn.textContent = 'Update হচ্ছে...';
    const random = Math.random().toString(36).substring(2, 15);
    const language = 1;
    const shonustr = `{"confirmNewPwd":"${confirmPwd}","language":${language},"newPwd":"${newPwd}","oldPwd":"${oldPwd}","random":"${random}"}`;
    const signature = md5(shonustr).toUpperCase();

    try {
        const res = await fetch('/deepanshu/api/webapi/ResetPassword.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
            body: JSON.stringify({ confirmNewPwd: confirmPwd, language, newPwd, oldPwd, random, signature, timestamp: Date.now() })
        });
        const data = await res.json();
        msgBox.style.display = 'block';
        if (data.code === 0) {
            msgBox.style.background = 'rgba(0,230,118,.1)'; msgBox.style.color = '#00e676';
            msgBox.textContent = '✅ Password সফলভাবে বদলেছে!';
            document.getElementById('oldPwd').value = '';
            document.getElementById('newPwd').value = '';
            document.getElementById('confirmPwd').value = '';
        } else {
            msgBox.style.background = 'rgba(232,50,60,.1)'; msgBox.style.color = '#e8323c';
            msgBox.textContent = '❌ ' + (data.msg || 'ব্যর্থ হয়েছে।');
        }
    } catch (e) {
        msgBox.style.display = 'block'; msgBox.style.background = 'rgba(232,50,60,.1)'; msgBox.style.color = '#e8323c';
        msgBox.textContent = 'একটা সমস্যা হয়েছে।';
    }
    btn.textContent = 'Password Update করো';
}
</script>

</body>
</html>
