<?php
require_once("backend/functions.php");
dbconn();
loggedinonly();

stdhead("Team Battle Event");
begin_frame("Team Battle - Choose Your Faction");


if (isset($_GET["join_team"]) && $CURUSER["team_id"] == 0) {
    $chosen_team = (int)$_GET["join_team"];

    if (in_array($chosen_team, [1, 2, 3])) {
        SQL_Query_exec("UPDATE users SET team_id = $chosen_team WHERE id = " . $CURUSER["id"]);
        $CURUSER['team_id'] = $chosen_team;
        
        $res = SQL_Query_exec("SELECT name, color FROM teams WHERE id = $chosen_team");
        $row = mysqli_fetch_assoc($res);
        echo "<div style='background: rgba(0, 255, 0, 0.1); border: 1px solid #00ff00; padding: 15px; text-align: center; margin-bottom: 20px; border-radius: 5px;'>";
        echo "<h3>Welcome to <span style='color: " . $row["color"] . ";'>" . htmlspecialchars($row["name"]) . "</span>! Let the battle begin!</h3>";
        echo "</div>";
    }
}

echo "<div style='background: rgba(0,0,0,0.3); padding: 20px; border: 1px solid rgba(255,255,255,0.1); border-radius: 5px; margin-bottom: 20px;'>";
echo "<h3 style='margin-top: 0; color: #FFBF00;'><i class='fa fa-trophy'></i> Event Rules & Prizes</h3>";
echo "<p>Welcome to the ultimate <b>Team Battle</b>! Choose your faction and fight for supremacy. Every byte counts, and the winning team takes the glory at the end of the month!</p>";
echo "<ul style='line-height: 1.6;'>
        <li><b>Volume Points:</b> Earn <b>1 Point</b> for every 10 MB you upload (perfect for fast connections).</li>
        <li><b>Dedication Points:</b> Earn <b>1 Point</b> on every tracker update just by keeping your torrents seeding at 100% (perfect for home connections).</li>
      </ul>";
echo "<p style='color: #ff4444; font-size: 13px;'><i><i class='fa fa-exclamation-triangle'></i> <b>Note:</b> To be eligible for the end-of-month rewards, you must actively contribute to your team. The minimum requirement is <b>10 GB uploaded</b> OR <b>100 seed points (pings)</b> during the event. Freeloaders get nothing!</i></p>";
echo "<p><b>Rewards (Distributed automatically on the 1st of every month):</b></p>";
echo "<p>
        <span style='color: gold;'>🥇 1st Place: 1 Month VIP + 50,000 BP</span> | 
        <span style='color: silver;'>🥈 2nd Place: 25,000 BP</span> | 
        <span style='color: #cd7f32;'>🥉 3rd Place: 10,000 BP</span>
      </p>";
      
echo "<hr style='border: 0; border-top: 1px solid rgba(255,255,255,0.1); margin: 15px 0;'>";
echo "<p><b style='color: #FFBF00;'>🌟 Co-op Community Goal:</b> If all 3 teams accumulate <b>500,000 Points</b> combined, a Global Freeleech will be activated for everyone for the entire weekend! Keep seeding to help the community!</p>";
echo "</div>";

$user_team_id = isset($CURUSER['team_id']) ? (int)$CURUSER['team_id'] : 0;

if ($user_team_id == 0) {
    echo "<div style='text-align: center; margin-top: 30px; padding: 20px; background: rgba(255,0,0,0.1); border: 1px solid #ff4444; border-radius: 5px;'>";
    echo "<h2 style='color: #ff4444; margin-top: 0;'><i class='fa fa-lock'></i> Battlefield Classified</h2>";
    echo "<p>You cannot see which users are in which team, nor can you see the current points.</p>";
    echo "<p>Choose wisely based on your instinct. Once you join a faction, the live standings and member lists will be revealed to you!</p>";
    echo "</div>";

    echo "<h2 style='text-align: center; color: #ff4444; margin-bottom: 20px; margin-top: 30px;'>Choose your faction to join the battle!</h2>";
    echo "<div style='display: flex; justify-content: space-around; flex-wrap: wrap; gap: 15px; margin-bottom: 30px;'>";
    
    $res_teams = SQL_Query_exec("SELECT id, name, color FROM teams ORDER BY id ASC");
    while ($team = mysqli_fetch_assoc($res_teams)) {
        echo "<div style='flex: 1; min-width: 250px; text-align: center; padding: 20px; border: 2px solid " . $team['color'] . "; border-radius: 8px; background: rgba(0,0,0,0.5);'>";
        echo "<h2 style='color: " . $team['color'] . "; margin-top: 0;'>" . htmlspecialchars($team['name']) . "</h2>";
        echo "<form method='GET' action='teams.php'>";
        echo "<input type='hidden' name='join_team' value='" . $team['id'] . "' />";
        echo "<button type='submit' style='padding: 10px 20px; font-size: 16px; font-weight: bold; background: " . $team['color'] . "; color: #000; border: none; border-radius: 4px; cursor: pointer; transition: 0.2s;'>JOIN " . strtoupper($team['name']) . "</button>";
        echo "</form>";
        echo "</div>";
    }
    echo "</div>";
    
    echo "<div style='text-align: center; margin-top: 20px; padding: 15px; border: 1px solid #ff4444; border-radius: 5px; background: rgba(255,0,0,0.1);'>";
    echo "<h3 style='color: #ff4444; margin-top: 0;'><i class='fa fa-user-secret'></i> Classified Rosters</h3>";
    echo "<p>The member lists are hidden! You must pledge allegiance to a faction before you can see who is fighting for which team.</p>";
    echo "</div>";

} else {

    $res = SQL_Query_exec("SELECT name, color FROM teams WHERE id = " . $user_team_id);
    $row = mysqli_fetch_assoc($res);
    
    $my_upload = mksize($CURUSER['event_upload']);
    $my_seed_pings = number_format($CURUSER['event_seed_time']);
    $my_estimated_points = floor($CURUSER['event_upload'] / 10485760) + $CURUSER['event_seed_time'];

    echo "<div style='text-align: center; padding: 20px; border: 2px solid " . $row['color'] . "; border-radius: 5px; margin-bottom: 30px; background: rgba(0,0,0,0.4);'>";
    echo "<h2 style='margin-top: 0;'>Your Faction: <span style='color: " . $row["color"] . "; text-transform: uppercase;'>" . htmlspecialchars($row["name"]) . "</span></h2>";
    echo "<p style='font-size: 14px;'><b>Your Personal Contribution:</b></p>";
    echo "<div style='display: flex; justify-content: center; gap: 30px; margin-top: 10px;'>";
    echo "<div>Event Uploaded<br><b style='font-size: 18px; color: #0077CC;'>" . $my_upload . "</b></div>";
    echo "<div>Seed Time (Pings)<br><b style='font-size: 18px; color: #8A2BE2;'>" . $my_seed_pings . "</b></div>";
    echo "<div>Points Generated<br><b style='font-size: 18px; color: #00ff00;'>" . number_format($my_estimated_points) . "</b></div>";
    echo "</div></div>";

    echo "<h3 style='text-align: center; margin-bottom: 15px;'>Team Rosters & Top Contributors</h3>";
    echo "<div style='display: flex; justify-content: space-between; flex-wrap: wrap; gap: 15px;'>";

    $res_teams = SQL_Query_exec("SELECT id, name, color, points FROM teams ORDER BY points DESC");
    while ($team = mysqli_fetch_assoc($res_teams)) {
        $team_id = $team['id'];
        echo "<div style='flex: 1; min-width: 30%; background: rgba(0,0,0,0.2); border: 1px solid " . $team['color'] . "; padding: 10px; border-radius: 5px;'>";
        echo "<h3 style='text-align: center; color: " . $team['color'] . "; margin-top: 0; border-bottom: 1px solid rgba(255,255,255,0.1); padding-bottom: 10px;'>" . htmlspecialchars($team['name']) . " <br><small style='color: #ccc;'>" . number_format($team['points']) . " Pts</small></h3>";

        $res_users = SQL_Query_exec("SELECT id, username, event_upload, event_seed_time FROM users WHERE team_id = $team_id ORDER BY (FLOOR(event_upload / 10485760) + event_seed_time) DESC LIMIT 50");
        
        if (mysqli_num_rows($res_users) > 0) {
            echo "<table width='100%' style='font-size: 12px; text-align: left;'>";
            echo "<tr><th>User</th><th style='text-align:right;'>Uploaded</th></tr>";
            while ($user = mysqli_fetch_assoc($res_users)) {
                
                $user_formatted = class_user($user['username']);
                echo "<tr>";
                echo "<td style='padding: 3px 0; border-bottom: 1px dotted rgba(255,255,255,0.05);'>$user_formatted</td>";
                echo "<td style='padding: 3px 0; border-bottom: 1px dotted rgba(255,255,255,0.05); text-align:right; color: #aaa;'>" . mksize($user['event_upload']) . "</td>";
                echo "</tr>";
            }
            echo "</table>";
        } else {
            echo "<p style='text-align: center; color: #777;'>No members yet.</p>";
        }
        echo "</div>";
    }
    echo "</div>";
}

end_frame();
stdfoot();
?>