<?php
// ================= SESSION FIX =================
$session_save_path = __DIR__ . '/sessions';
if (!file_exists($session_save_path)) { mkdir($session_save_path, 0777, true); }
session_save_path($session_save_path);
session_start();

include 'config.php';

$tab = isset($_GET['tab']) ? $_GET['tab'] : 'weekly';

// ================= SECURITY =================
$access_granted = false;
$is_user_banned = false;
$ban_reason = "";
$error_msg = "";

if(isset($_SESSION['guild_member_uid'])) {
    $s_uid = $_SESSION['guild_member_uid'];
    if(isset($conn)) {
        $check_ban = $conn->prepare("SELECT is_banned, ban_reason FROM members WHERE uid = ?");
        $check_ban->bind_param("s", $s_uid);
        $check_ban->execute();
        $ban_res = $check_ban->get_result()->fetch_assoc();
        
        if($ban_res && $ban_res['is_banned'] == 1) {
            $is_user_banned = true; $ban_reason = $ban_res['ban_reason'];
            unset($_SESSION['guild_member_uid']); 
        } else { $access_granted = true; }
    }
}

if(isset($_POST['verify_uid']) && !$is_user_banned) {
    $v_uid = trim($_POST['v_uid']);
    $chk = $conn->prepare("SELECT is_banned, ban_reason FROM members WHERE uid = ?");
    $chk->bind_param("s", $v_uid);
    $chk->execute();
    $res = $chk->get_result();
    
    if($res->num_rows > 0) {
        $user_row = $res->fetch_assoc();
        if($user_row['is_banned'] == 1) {
            $is_user_banned = true; $ban_reason = $user_row['ban_reason'];
        } else {
            $_SESSION['guild_member_uid'] = $v_uid; $access_granted = true;
        }
    } else { $error_msg = "UID not found in Guild List!"; }
}

if($tab == 'officers' && !$is_user_banned) { $access_granted = true; }

// ================= DATA =================
if($access_granted) {
    if($tab == 'officers') {
        $query = "SELECT * FROM members WHERE rank IN ('Admin', 'Leader', 'Active Leader', 'Officer') AND is_banned = 0 ORDER BY FIELD(rank, 'Admin', 'Leader', 'Active Leader', 'Officer')";
        $title_text = "GUILD STAFF";
        $title_icon = '<i class="fas fa-user-shield" style="color:cyan;"></i>';
    } elseif($tab == 'total') {
        $query = "SELECT * FROM members WHERE is_banned = 0 ORDER BY total_dogtags DESC, behavior_score DESC";
        $title_text = "ALL TIME LEGENDS";
        $title_icon = '<i class="fas fa-medal" style="color:#00ff9d;"></i>';
    } elseif($tab == 'old') {
        $query = "SELECT * FROM members WHERE is_banned = 0 AND join_date <= DATE_SUB(CURDATE(), INTERVAL 1 YEAR) ORDER BY join_date ASC";
        $title_text = "VETERAN SQUAD";
        $title_icon = '<i class="fas fa-crown" style="color:#ffd700;"></i>';
    } else {
        $query = "SELECT * FROM members WHERE is_banned = 0 ORDER BY weekly_dogtags DESC, total_dogtags DESC";
        $title_text = "WEEKLY TOP CHART";
        $title_icon = '<i class="fas fa-fire" style="color:#ff3333;"></i>';
        $check = $conn->query("SELECT MAX(weekly_dogtags) as max_s FROM members WHERE is_banned = 0")->fetch_assoc();
        $max_score = $check['max_s'] ?? 0;
    }
    $result = $conn->query($query);
}

function getTenureStyle($date) {
    if(!$date) return ['class'=>'bd-def', 'txt'=>'NEW'];
    $diff = date_diff(date_create($date), date_create());
    if($diff->y >= 7) return ['class' => 'bd-7y', 'txt' => 'LEGEND'];
    elseif($diff->y >= 1) return ['class' => 'bd-1y', 'txt' => 'OLD'];
    return ['class' => 'bd-def', 'txt' => 'MEMBER'];
}
?>

<!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>Guild Area</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>
        html, body { height: 100%; margin: 0; padding: 0; background: #050505; color: #fff; font-family: 'Segoe UI', sans-serif; overflow-y: auto; }
        .lb-container { width: 100%; max-width: 650px; margin: 0 auto; padding: 15px; padding-bottom: 80px; min-height: 100vh; }
        .lb-header { text-align: center; margin: 25px 0; }
        .lb-header h2 { font-size: 20px; margin: 5px 0; letter-spacing: 2px; text-transform: uppercase; text-shadow: 0 0 10px rgba(0,255,255,0.3); }
        .tab-box { display: flex; gap: 8px; background: #111; padding: 8px; border-radius: 12px; border: 1px solid #333; margin-bottom: 25px; overflow-x: auto; white-space: nowrap; scrollbar-width: none; }
        .t-link { flex: 1; text-align: center; padding: 10px 15px; border-radius: 8px; text-decoration: none; color: #888; font-weight: bold; font-size: 11px; min-width: 80px; transition: 0.3s; }
        .t-link.active { background: #00f2ff !important; color: #000 !important; box-shadow: 0 0 15px rgba(0, 242, 255, 0.4) !important; border: 1px solid #00f2ff; }
        
        .badge { font-size: 9px; padding: 3px 8px; border-radius: 4px; text-transform: uppercase; border: 1px solid #444; font-weight: bold; letter-spacing: 0.5px; }
        .bg-Admin { color: #ff3333; border-color: #ff3333; background: rgba(255, 51, 51, 0.15); box-shadow: 0 0 5px rgba(255, 51, 51, 0.2); }
        .bg-Leader { color: #ffaa00; border-color: #ffaa00; background: rgba(255, 170, 0, 0.15); box-shadow: 0 0 5px rgba(255, 170, 0, 0.2); }
        .bg-Active-Leader { color: #00f2ff; border-color: #00f2ff; background: rgba(0, 242, 255, 0.15); box-shadow: 0 0 5px rgba(0, 242, 255, 0.2); }
        .bg-Officer { color: #ffff00; border-color: #ffff00; background: rgba(255, 255, 0, 0.15); }
        .bg-Elder { color: #cc00ff; border-color: #cc00ff; background: rgba(204, 0, 255, 0.15); }
        .bg-Member { color: #dddddd; border-color: #666; background: rgba(255, 255, 255, 0.05); }

        .ban-screen { text-align: center; padding: 60px 20px; border: 2px solid #ff3333; border-radius: 15px; background: rgba(30,0,0,0.8); margin-top: 30px; animation: shake 0.5s ease-in-out; }
        .ban-screen i { font-size: 70px; color: #ff3333; margin-bottom: 20px; }
        .ban-reason { background: #000; padding: 15px; border-radius: 8px; border-left: 4px solid #ff3333; margin-top: 20px; display: inline-block; min-width: 80%; }
        @keyframes shake { 0%, 100% {transform: translateX(0);} 25% {transform: translateX(-5px);} 75% {transform: translateX(5px);} }

        .rank-card { display: flex; align-items: center; background: rgba(20,20,20,0.8); margin-bottom: 10px; border-radius: 12px; padding: 15px; border: 1px solid #222; position: relative; transition: 0.2s; backdrop-filter: blur(5px); }
        .bd-7y { border-left: 4px solid #ffd700; background: linear-gradient(90deg, #1a1500, #000); }
        .bd-1y { border-left: 4px solid #cd7f32; }
        .bd-def { border-left: 1px solid #444; }
        .r-num { width: 30px; text-align: center; font-size: 16px; font-weight: 800; color: #555; }
        .top-1 { font-size: 24px; color: gold; filter: drop-shadow(0 0 5px gold); }
        .top-2 { font-size: 20px; color: silver; }
        .top-3 { font-size: 18px; color: #cd7f32; }
        .r-info { flex: 1; padding-left: 12px; }
        .r-name { display: block; font-size: 15px; font-weight: bold; color: #fff; margin-bottom: 4px; }
        .r-score { text-align: right; min-width: 80px; }
        .sc-val { font-size: 17px; font-weight: 800; color: #fff; font-family: monospace; }
        .sc-lbl { font-size: 9px; color: #aaa; }

        .lock-screen { text-align: center; padding: 50px 20px; border: 1px solid #333; border-radius: 15px; background: rgba(20,20,20,0.5); margin-top: 30px; }
        .lock-input { width: 80%; padding: 12px; margin-top: 20px; background: #000; border: 1px solid #555; color: cyan; text-align: center; border-radius: 8px; font-weight: bold; letter-spacing: 2px; font-size: 16px; outline: none; }
        .lock-btn { width: 50%; padding: 12px; margin-top: 15px; background: linear-gradient(45deg, #00f2ff, #0088ff); border: none; color: #000; font-weight: 900; border-radius: 50px; cursor: pointer; transition: 0.3s; box-shadow: 0 0 15px rgba(0, 242, 255, 0.3); }

        .footer-nav { text-align: center; margin-top: 40px; padding-bottom: 20px; }
        .btn-home { display: inline-flex; align-items: center; justify-content: center; gap: 10px; padding: 12px 35px; background: rgba(255, 255, 255, 0.05); color: #fff; border: 1px solid rgba(255, 255, 255, 0.2); border-radius: 50px; text-decoration: none; font-size: 13px; font-weight: 800; letter-spacing: 1px; transition: all 0.3s ease; backdrop-filter: blur(5px); }
        .btn-home:hover { background: #00f2ff; color: #000; border-color: #00f2ff; box-shadow: 0 0 20px rgba(0, 242, 255, 0.5); }
        .empty-state { text-align:center; padding:40px; color:#555; border:1px dashed #333; border-radius:10px; margin-top:20px; }
    </style>
</head>
<body>

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

<div class="lb-container">
    <?php if($is_user_banned): ?>
        <div class="ban-screen">
            <i class="fas fa-user-slash"></i>
            <h2 style="color:#ff3333; margin:0;">ACCESS DENIED</h2>
            <p style="color:#ccc; font-size:14px; margin-top:10px;">আপনার একাউন্টটি ব্যান্ড করা হয়েছে।</p>
            <div class="ban-reason">
                <span style="display:block; font-size:10px; color:#ff3333; text-transform:uppercase;">Reason:</span>
                <span style="font-weight:bold; font-size:16px;"><?php echo htmlspecialchars($ban_reason); ?></span>
            </div>
            <div style="margin-top:30px;">
                <a href="index.php" class="btn-home" style="padding:10px 20px;">Contact Support</a>
            </div>
        </div>
    <?php else: ?>
        <div class="lb-header">
            <?php if($access_granted): ?>
                <div style="font-size:35px; margin-bottom:10px; filter: drop-shadow(0 0 10px rgba(0,255,255,0.3));"><?php echo $title_icon; ?></div>
                <h2><?php echo $title_text; ?></h2>
            <?php else: ?>
                <h2>MEMBERS AREA</h2>
            <?php endif; ?>
        </div>

        <div class="tab-box">
            <a href="?tab=officers" class="t-link <?php echo $tab=='officers'?'active':''; ?>">OFFICIALS</a>
            <a href="?tab=weekly" class="t-link <?php echo $tab=='weekly'?'active':''; ?>"><?php if(!isset($_SESSION['guild_member_uid'])) echo '<i class="fas fa-lock" style="font-size:10px; margin-right:3px;"></i>'; ?> WEEKLY</a>
            <a href="?tab=total" class="t-link <?php echo $tab=='total'?'active':''; ?>"><?php if(!isset($_SESSION['guild_member_uid'])) echo '<i class="fas fa-lock" style="font-size:10px; margin-right:3px;"></i>'; ?> TOTAL</a>
            <a href="?tab=old" class="t-link <?php echo $tab=='old'?'active':''; ?>"><?php if(!isset($_SESSION['guild_member_uid'])) echo '<i class="fas fa-lock" style="font-size:10px; margin-right:3px;"></i>'; ?> LEGENDS</a>
        </div>

        <div class="content-scroll">
            <?php if($access_granted): ?>
                <?php if($tab == 'weekly' && isset($max_score) && $max_score == 0): ?>
                    <div class="empty-state"><i class="fas fa-clock" style="font-size:40px; margin-bottom:15px; color:#ff3333;"></i><h3 style="color:#fff; margin:0;">SEASON RESET</h3><p style="font-size:12px;">Scoring starts soon...</p></div>
                <?php else: ?>
                    <?php if($result->num_rows > 0): $count = 1; while($row = $result->fetch_assoc()): 
                            $r_cls = ""; $r_txt = $count;
                            if($count==1){$r_cls="top-1"; $r_txt='<i class="fas fa-crown"></i>';}
                            elseif($count==2){$r_cls="top-2"; $r_txt='<i class="fas fa-medal"></i>';}
                            elseif($count==3){$r_cls="top-3"; $r_txt='<i class="fas fa-medal"></i>';}
                            $is_member = ($row['rank']=='Member');
                            $href = $is_member ? 'javascript:void(0)' : 'result.php?uid='.$row['uid'];
                            $tenure = getTenureStyle($row['join_date']);
                    ?>
                    <a href="<?php echo $href; ?>" class="rank-card <?php echo $tenure['class']; ?>" style="text-decoration:none;">
                        <div class="r-num <?php echo $r_cls; ?>"><?php echo $r_txt; ?></div>
                        <div class="r-info">
                            <span class="r-name"><?php echo $row['game_name']; ?></span>
                            <span class="badge bg-<?php echo str_replace(' ','-',$row['rank']); ?>"><?php echo $row['rank']; ?></span>
                        </div>
                        <div class="r-score">
                            <?php if($tab == 'officers'): ?>
                                <span class="sc-val" style="color:cyan; font-size:11px;">PROFILE <i class="fas fa-arrow-right"></i></span>
                            <?php else: ?>
                                <?php if($tab=='weekly'): ?>
                                    <span class="sc-val" style="color:#ff3333;"><?php echo $row['weekly_dogtags']; ?></span><span class="sc-lbl">W</span>
                                <?php else: ?>
                                    <span class="sc-val" style="color:#00ff9d;"><?php echo number_format($row['total_dogtags']); ?></span><span class="sc-lbl">T</span>
                                <?php endif; ?>
                            <?php endif; ?>
                        </div>
                    </a>
                    <?php $count++; endwhile; else: ?>
                        <div class="empty-state">NO DATA FOUND</div>
                    <?php endif; ?>
                <?php endif; ?>
            <?php else: ?>
                <div class="lock-screen">
                    <i class="fas fa-fingerprint" style="font-size:60px; color:#ff3333; margin-bottom:20px; filter: drop-shadow(0 0 10px #ff3333);"></i>
                    <h3 style="color:#fff; margin:0; letter-spacing:1px;">ACCESS LOCKED</h3>
                    <form method="post">
                        <input type="number" name="v_uid" class="lock-input" placeholder="Type UID..." required autocomplete="off"><br>
                        <button type="submit" name="verify_uid" class="lock-btn">UNLOCK ACCESS</button>
                    </form>
                    <?php if($error_msg): ?>
                        <div style="color:#ff3333; font-size:12px; margin-top:15px; font-weight:bold;"><i class="fas fa-times-circle"></i> <?php echo $error_msg; ?></div>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        </div>
    <?php endif; ?>

    <div class="footer-nav">
        <a href="index.php" class="btn-home"><i class="fas fa-home"></i> RETURN TO HOME</a>
    </div>
</div>
</body>
</html>