<?php
include 'config.php';
$uid = isset($_GET['uid']) ? trim($_GET['uid']) : '';

// 1. SETTINGS FETCH
$sets = []; 
$q = $conn->query("SELECT * FROM settings"); 
while($r=$q->fetch_assoc()) $sets[$r['setting_key']]=$r['setting_value'];

// 2. USER FETCH
$stmt = $conn->prepare("SELECT * FROM members WHERE uid = ?");
$stmt->bind_param("s", $uid);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();

// 3. RANK CALCULATION
$list_rank = "N/A";
if($user) {
    $rank_res = $conn->query("SELECT COUNT(*) + 1 AS rnk FROM members WHERE weekly_dogtags > " . $user['weekly_dogtags']);
    $list_rank = $rank_res->fetch_assoc()['rnk'];
}

// 4. TENURE BADGE
function getTenureBadge($date) {
    if(!$date) return '<span class="tag-new">NEW</span>';
    try {
        $join = new DateTime($date);
        $now = new DateTime();
        $diff = $join->diff($now);
        
        if($diff->y >= 7) { return '<span class="tag-7y"><i class="fas fa-crown"></i> 7Y OLD</span>'; }
        if($diff->y >= 1) { return '<span class="tag-old"><i class="fas fa-star"></i> LEGEND ('.$diff->y.'Y)</span>'; }
        return '<span class="tag-new">RECENTLY ('.$diff->m.'M)</span>';
    } catch (Exception $e) { return '<span class="tag-new">NEW</span>'; }
}

$logo_path = "uploads/logo.png";
if(!file_exists($logo_path)) $logo_path = "https://cdn-icons-png.flaticon.com/512/2922/2922510.png";
$tourn_img_path = "uploads/tournament.jpg";
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Profile - <?php echo $user ? htmlspecialchars($user['game_name']) : 'Guild Member'; ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="style.css?v=<?php echo time(); ?>">
    <style>
        * { box-sizing: border-box; outline: none; -webkit-tap-highlight-color: transparent; font-family: 'Segoe UI', sans-serif; }
        html, body { margin: 0; padding: 0; background: #020205; color: #fff; min-height: 100vh; overflow-x: hidden; display: flex; flex-direction: column; }
        .container { width: 100%; max-width: 900px; margin: 0 auto; padding: 15px; flex: 1; }
        
        .profile-body-grid { display: flex; flex-direction: column; gap: 20px; }
        @media (min-width: 1024px) { .profile-body-grid { display: grid; grid-template-columns: 1fr 1.2fr; gap: 25px; margin-top:30px; } }

        .top-nav { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
        .back-btn { background: rgba(255, 255, 255, 0.1); color: cyan; padding: 10px 18px; border-radius: 50px; text-decoration: none; font-size: 14px; font-weight: bold; border: 1px solid rgba(0, 255, 255, 0.3); }

        .ban-screen { background: rgba(20, 0, 0, 0.9); border: 2px solid #ff0000; border-radius: 15px; padding: 40px 20px; text-align: center; margin-top: 50px; }
        .ban-icon { font-size: 60px; color: #ff0000; margin-bottom: 20px; animation: blinker 1.5s linear infinite; }
        @keyframes blinker { 50% { opacity: 0; } }

        .profile-card { position: relative; background: rgba(20, 20, 25, 0.9); border-radius: 15px; padding: 30px 15px; text-align: center; border: 1px solid rgba(255, 255, 255, 0.1); backdrop-filter: blur(10px); }
        .logo-box-profile { position: relative; width: 140px; height: 140px; margin: 0 auto 15px auto; display: flex; justify-content: center; align-items: center; }
        #logoCanvas { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 100%; height: 100%; pointer-events: none; z-index: 1; display:none; }
        .user-img { width: 130px; height: 130px; border-radius: 50%; object-fit: cover; border: 3px solid #00f2ff; background: #000; z-index: 5; position: relative; }
        .thunder-active #logoCanvas { display:block; }
        .thunder-active .user-img { border-color: #fff !important; box-shadow: 0 0 20px cyan !important; }

        .corner-position { position: absolute; top: 15px; right: 15px; z-index: 10; }
        .tag-7y { background: linear-gradient(45deg, #ffd700, #ffae00); color: #000; padding: 4px 12px; border-radius: 4px; font-size: 10px; font-weight: 900; box-shadow: 0 0 15px rgba(255, 215, 0, 0.6); display: inline-flex; align-items: center; gap: 5px; border: 1px solid #fff; }
        .tag-old { background: rgba(255, 215, 0, 0.1); color: #ffd700; border: 1px solid #ffd700; padding: 3px 10px; border-radius: 4px; font-size: 10px; font-weight: bold; }
        .tag-new { background: rgba(0, 242, 255, 0.1); color: #00f2ff; border: 1px solid #00f2ff; padding: 3px 10px; border-radius: 4px; font-size: 10px; font-weight: bold; }

        .rank-tag { display: inline-block; padding: 6px 15px; border-radius: 6px; font-size: 11px; font-weight: 800; text-transform: uppercase; margin: 10px 0; border: 1px solid #333; }
        .rnk-Admin { background: linear-gradient(90deg, #800, #f00); border:none !important; box-shadow: 0 0 10px #f00; color: #fff; }
        .rnk-Leader, .rnk-Active-Leader { background: linear-gradient(90deg, #b35900, #ff8c00); border:none !important; box-shadow: 0 0 10px #ff8c00; color: #fff; }
        .rnk-Officer { background: linear-gradient(90deg, #999900, #ffff00); color:black !important; border:none !important; box-shadow: 0 0 10px #ffff00; }

        .join-tg-btn { display: flex; align-items: center; justify-content: center; gap: 10px; width: 100%; padding: 14px; border-radius: 10px; background: linear-gradient(90deg, #0088cc, #00a2ff); color: #fff; text-decoration: none; font-weight: 800; font-size: 13px; text-transform: uppercase; margin-top: 15px; box-shadow: 0 4px 15px rgba(0, 136, 204, 0.3); }
        .c-grid { display: flex; gap: 10px; margin-top: 15px; justify-content: center; }
        .c-btn { flex: 1; padding: 12px 10px; border-radius: 8px; font-weight: bold; text-decoration: none; color: white; font-size: 11px; text-transform: uppercase; display: flex; align-items: center; justify-content: center; gap: 8px; }
        .wa-btn { background: #25D366; } .tg-btn { background: #0088cc; }

        .stats-area { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 15px 0; }
        .stat-box { background: #111; padding: 15px 5px; border-radius: 8px; border: 1px solid #333; text-align: center; }
        .s-val { display: block; font-size: 18px; font-weight: bold; color: #fff; margin-top: 5px; }
        .s-lbl { font-size: 10px; color: #888; text-transform: uppercase; }
        .tourn-card { background: #0f0f0f; border-top: 3px solid #ff0055; padding: 20px; border-radius: 12px; text-align: center; }
        .timer-display { font-family: monospace; font-size: 18px; font-weight: bold; color: #fff; margin: 10px 0; border: 1px dashed #ff0055; padding: 10px; border-radius: 5px; background: rgba(255,0,85,0.05); }
        .btn-view-list { width: 100%; padding: 12px; border: none; border-radius: 5px; background: linear-gradient(90deg, #ff0055, #ff5500); color: #fff; font-weight: bold; cursor: pointer; margin-top: 15px; }
        .nc-box { background: rgba(255,0,0,0.1); border:1px solid #ff3333; padding:15px; border-radius:10px; text-align:center; margin-bottom:15px; animation:pulse 2s infinite; }
        
        footer { text-align: center; padding: 30px 15px; margin-top: auto; background: rgba(10, 10, 15, 0.95); border-top: 1px solid rgba(255,255,255,0.05); }
        .footer-social { display: flex; justify-content: center; gap: 20px; margin-bottom: 15px; }
        .footer-social a { color: #888; font-size: 24px; transition: 0.3s; }
        .footer-social a:hover { color: cyan; transform: scale(1.1); }
        .copy-txt { color: #444; font-size: 11px; letter-spacing: 1px; text-transform: uppercase; }

        .popup-modal { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.95); z-index: 9999; overflow: auto; flex-direction: column; align-items: center; justify-content: flex-start; padding: 20px; }
        .popup-content { position: relative; width: 100%; display: flex; flex-direction: column; align-items: center; margin-top: 40px; transition: transform 0.3s ease; transform-origin: center top; }
        .popup-modal img { width: 100%; max-width: 600px; border: 2px solid cyan; border-radius: 8px; cursor: zoom-in; transition: 0.3s; }
        .popup-modal img.zoomed { max-width: 1500px; width: 250%; cursor: zoom-out; }
        .close-pop { color: #fff; font-size: 35px; position: fixed; top: 15px; right: 20px; cursor: pointer; z-index: 10000; background: rgba(255,0,0,0.5); width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; border-radius: 50%; border: 1px solid #fff; }
        .zoom-hint { color: cyan; font-size: 12px; margin-bottom: 10px; font-weight: bold; text-transform: uppercase; }
    </style>
</head>
<body>

<?php include 'header.php'; ?>

<div class="container">
    <div class="top-nav">
        <a href="index.php" class="back-btn"><i class="fas fa-chevron-left"></i> SEARCH</a>
    </div>

    <?php if($user): ?>
        <?php if($user['is_banned'] == 1): ?>
            <div class="ban-screen">
                <i class="fas fa-user-slash ban-icon"></i>
                <div style="font-size:28px; font-weight:900; color:#ff0000;">YOU ARE BANNED</div>
                <div style="background: #000; padding: 15px; border-radius: 8px; border-left: 4px solid #ff0000; margin-top: 20px; display: inline-block;">
                    <div style="font-size:10px; color:#ff0000; letter-spacing:1px;">REASON OF BAN:</div>
                    <div style="font-size:16px; font-weight:bold;"><?php echo htmlspecialchars($user['ban_reason']); ?></div>
                </div>
            </div>
        <?php else: ?>
            <div class="profile-body-grid">
                <div>
                    <div class="profile-card">
                        <div class="corner-position"><?php echo getTenureBadge($user['join_date']); ?></div>
                        <div id="pLogoBox" class="logo-box-profile">
                            <canvas id="logoCanvas"></canvas>
                            <img src="<?php echo $logo_path; ?>" class="user-img" id="profImg">
                        </div>
                        <div class="u-name"><?php echo htmlspecialchars($user['game_name']); ?></div>
                        <span class="rank-tag rnk-<?php echo str_replace(' ','-',$user['rank']); ?>"><?php echo $user['rank']; ?></span>
                        <div class="uid-txt"><i class="fas fa-fingerprint" style="color:#00f2ff;"></i> UID: <?php echo $user['uid']; ?></div>

                        <?php if(!empty($sets['telegram_link'])): ?>
                        <a href="<?php echo $sets['telegram_link']; ?>" target="_blank" class="join-tg-btn"><i class="fab fa-telegram-plane"></i> JOIN TELEGRAM GROUP</a>
                        <?php endif; ?>

                        <?php if($user['rank'] != 'Member'): ?>
                        <div class="c-grid">
                            <?php if(!empty($user['whatsapp_number'])): ?>
                            <a href="https://wa.me/<?php echo $user['whatsapp_number']; ?>" target="_blank" class="c-btn wa-btn"><i class="fab fa-whatsapp"></i> WhatsApp</a>
                            <?php endif; ?>
                            <?php if(!empty($user['telegram_username'])): ?>
                            <a href="https://t.me/<?php echo str_replace(['@','t.me/'],'',$user['telegram_username']); ?>" target="_blank" class="c-btn tg-btn"><i class="fab fa-telegram-plane"></i> Telegram</a>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>

                <div>
                    <?php if($user['name_change_status'] == 1 && !empty($user['name_change_date'])): ?>
                        <div class="nc-box">
                            <div style="font-size:11px; font-weight:bold; color:#fff;">⚠️ CHANGE NAME BEFORE</div>
                            <div id="ncTimer" style="font-size:22px; font-weight:900; color:#fff; font-family:monospace;">--:--:--</div>
                        </div>
                    <?php endif; ?>
                    
                    <div class="stats-area">
                        <div class="stat-box"><span class="s-lbl">Weekly Score</span><span class="s-val" style="color:#00f2ff;"><?php echo number_format($user['weekly_dogtags']); ?></span></div>
                        <div class="stat-box"><span class="s-lbl">Total Score</span><span class="s-val" style="color:#00ff9d;"><?php echo number_format($user['total_dogtags']); ?></span></div>
                        <div class="stat-box"><span class="s-lbl">Rank</span><span class="s-val" style="color:orange;">#<?php echo $list_rank; ?></span></div>
                    </div>
                    
                    <?php if($user['tournament_status'] == 'yes'): ?>
                    <div class="tourn-card">
                        <div style="color:#888; font-size:11px; text-transform:uppercase;">Tournament Squad Status</div>
                        <div class="timer-display" id="tTime">CHECKING...</div>
                        <button class="btn-view-list" onclick="openTournModal()">VIEW SQUAD LIST</button>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        <?php endif; ?>
    <?php else: ?>
        <div style="text-align:center; padding:50px;"><h2 style="color:#ff4444;">UID Not Found</h2></div>
    <?php endif; ?>
</div>

<footer>
    <div class="footer-social">
        <?php if(!empty($sets['fb_link'])): ?><a href="<?php echo $sets['fb_link']; ?>" target="_blank"><i class="fab fa-facebook"></i></a><?php endif; ?>
        <?php if(!empty($sets['yt_link'])): ?><a href="<?php echo $sets['yt_link']; ?>" target="_blank"><i class="fab fa-youtube"></i></a><?php endif; ?>
        <?php if(!empty($sets['wa_link'])): ?><a href="https://wa.me/<?php echo $sets['wa_link']; ?>" target="_blank"><i class="fab fa-whatsapp"></i></a><?php endif; ?>
        <?php if(!empty($sets['discord_link'])): ?><a href="<?php echo $sets['discord_link']; ?>" target="_blank"><i class="fab fa-discord"></i></a><?php endif; ?>
    </div>
    <div class="copy-txt">&copy; 2026 GUILD SYSTEM | ALL RIGHTS RESERVED</div>
</footer>

<div id="imgPopup" class="popup-modal">
    <div class="close-pop" onclick="closeTournModal()">&times;</div>
    <div class="popup-content">
        <div class="zoom-hint"><i class="fas fa-search-plus"></i> Click Image to Zoom & Pan</div>
        <?php if(file_exists($tourn_img_path)): ?>
            <img src="<?php echo $tourn_img_path; ?>?v=<?php echo time(); ?>" id="tournImg" onclick="toggleZoom(this)">
        <?php else: ?>
            <div style="color:#fff;">No Image Uploaded</div>
        <?php endif; ?>
    </div>
</div>

<script>
    function openTournModal() { document.getElementById('imgPopup').style.display = 'flex'; document.body.style.overflow = 'hidden'; }
    function closeTournModal() { const modal = document.getElementById('imgPopup'); const img = document.getElementById('tournImg'); modal.style.display = 'none'; img.classList.remove('zoomed'); document.body.style.overflow = 'auto'; }
    function toggleZoom(img) { img.classList.toggle('zoomed'); }

    function updateT() {
        const target = "<?php echo $sets['tournament_date'] ?? ''; ?>";
        const el = document.getElementById("tTime");
        if(!el) return;
        const td = new Date(target).getTime();
        if(isNaN(td)) { el.innerHTML = target || "UPCOMING"; return; }
        const timer = setInterval(() => {
            const diff = td - new Date().getTime();
            if(diff < 0) { el.innerHTML = "STARTED"; clearInterval(timer); return; }
            const d = Math.floor(diff/86400000), h = Math.floor((diff%86400000)/3600000), m = Math.floor((diff%3600000)/60000), s = Math.floor((diff%60000)/1000);
            el.innerHTML = d + "d " + h + "h " + m + "m " + s + "s";
        }, 1000);
    }
    updateT();

    <?php if($user && $user['name_change_status'] == 1 && !empty($user['name_change_date'])): ?>
    const ncd = new Date("<?php echo date('M d, Y H:i:s', strtotime($user['name_change_date'])); ?>").getTime();
    const ncInterval = setInterval(() => {
        const now = new Date().getTime();
        const diff = ncd - now;
        if(diff < 0) { document.getElementById("ncTimer").innerHTML = "TIME UP!"; clearInterval(ncInterval); return; }
        const ds = Math.floor(diff / (1000 * 60 * 60 * 24));
        const h = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        const m = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
        const s = Math.floor((diff % (1000 * 60)) / 1000);
        document.getElementById("ncTimer").innerHTML = ds + "d " + h + "h " + m + "m " + s + "s";
    }, 1000);
    <?php endif; ?>

    const pCanvas = document.getElementById('logoCanvas');
    if(pCanvas) {
        const pCtx = pCanvas.getContext('2d');
        const pContainer = document.getElementById('pLogoBox');
        let pRunning = false;
        pCanvas.width = 200; pCanvas.height = 200;
        function rand(min,max){return Math.random()*(max-min)+min;}
        function drawLightning(x,y){
            const angle = rand(0, Math.PI*2);
            let sx = x + Math.cos(angle)*65, sy = y + Math.sin(angle)*65;
            let ex = x + Math.cos(angle)*rand(90,120), ey = y + Math.sin(angle)*rand(90,120);
            pCtx.beginPath(); pCtx.moveTo(sx,sy);
            let steps = rand(3,6), cx=sx, cy=sy;
            for(let i=0; i<steps; i++){
                let mx=cx+(ex-cx)/steps+rand(-5,5), my=cy+(ey-cy)/steps+rand(-5,5);
                pCtx.lineTo(mx,my); cx=mx; cy=my;
            }
            pCtx.lineTo(ex,ey); pCtx.strokeStyle='#fff'; pCtx.lineWidth=rand(1,2);
            pCtx.shadowBlur=10; pCtx.shadowColor='#00f2ff'; pCtx.stroke();
        }
        function loopP() {
            if(!pRunning) { pCtx.clearRect(0,0,pCanvas.width,pCanvas.height); return; }
            pCtx.clearRect(0,0,pCanvas.width,pCanvas.height);
            for(let i=0;i<rand(2,4);i++) drawLightning(100,100);
            setTimeout(() => requestAnimationFrame(loopP), 50);
        }
        setInterval(() => {
            if(typeof player !== 'undefined' && typeof player.getPlayerState === 'function') {
                if(player.getPlayerState() === 1) {
                    if(!pRunning) { pRunning=true; pContainer.classList.add('thunder-active'); loopP(); }
                } else { pRunning=false; pContainer.classList.remove('thunder-active'); }
            }
        }, 1000);
    }
</script>
</body>
</html>