<?php
/**
 * Template Name: BlueSky - Team
 */
get_header(); bluesky_navbar('team');
$server_ip = get_option('bluesky_server_ip','mc.example.com');
$header_bg = bluesky_get_image('bluesky_header_bg_url','images/header-background.jpg');
?>
<section id="page-header" style="background-image:url('<?php echo esc_url($header_bg); ?>')">
    <div class="content">
        <p class="minecraft-server-ip"><?php echo esc_html($server_ip); ?></p>
        <h1 class="title"><span>Team</span></h1>
        <p class="description">Liste aller Server Team-Mitglieder.</p>
    </div>
</section>

<section id="admin-team">
    <div class="content at-content">
    <?php
    $q = new WP_Query([
        'post_type'      => 'bluesky_team',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
        'meta_key'       => '_bluesky_sort_order',
        'orderby'        => 'meta_value_num',
        'order'          => 'ASC',
    ]);

    $groups = [];
    if ($q->have_posts()) {
        while ($q->have_posts()) {
            $q->the_post();
            $id  = get_the_ID();
            $g   = get_post_meta($id, '_bluesky_team_group', true) ?: 'Team';
            $groups[$g][] = [
                'in_game_name' => get_post_meta($id, '_bluesky_in_game_name', true),
                'rank'         => get_post_meta($id, '_bluesky_rank', true),
                'rank_color'   => get_post_meta($id, '_bluesky_rank_color', true),
                'skin_url'     => get_post_meta($id, '_bluesky_skin_url', true),
            ];
        }
        wp_reset_postdata();
    }

    $skin_type = get_option('bluesky_skin_type', 'bust');
    // Steve UUID als Fallback-Platzhalter
    $placeholder = 'https://crafthead.net/bust/ec561538f3fd461daff5086b22154bce/256';

    if (!empty($groups)):
        foreach ($groups as $gname => $members):
    ?>
    <div class="group <?php echo esc_attr(sanitize_html_class($gname)); ?>">
        <h2 class="rank-title"><?php echo esc_html(ucfirst($gname)); ?></h2>
        <div class="users">
        <?php foreach ($members as $m):
            // Wenn eigene URL → direkt nutzen, sonst Platzhalter bis JS lädt
            $initial_src = !empty($m['skin_url']) ? $m['skin_url'] : $placeholder;
            $rank_color  = !empty($m['rank_color']) ? $m['rank_color'] : 'rgba(57,190,255,0.6)';
            $rank_label  = !empty($m['rank']) ? $m['rank'] : '—';
        ?>
        <div class="user"
             data-ingame="<?php echo esc_attr($m['in_game_name']); ?>"
             data-skin="<?php echo esc_url($m['skin_url']); ?>"
             data-skintype="<?php echo esc_attr($skin_type); ?>">

            <div class="skin-wrapper">
                <img src="<?php echo esc_url($initial_src); ?>"
                     alt="<?php echo esc_attr($m['in_game_name']); ?>"
                     class="member-skin"
                     loading="lazy">
            </div>

            <div class="user-info">
                <span class="name"><?php echo esc_html($m['in_game_name']); ?></span>
                <span class="rank" style="background: <?php echo esc_attr($rank_color); ?>">
                    <?php echo esc_html($rank_label); ?>
                </span>
            </div>
        </div>
        <?php endforeach; ?>
        </div>
    </div>
    <?php
        endforeach;
    else:
    ?>
    <p style="color:var(--description-color);text-align:center;padding:60px 0">
        Noch keine Team-Mitglieder vorhanden.<br>
        <a href="<?php echo admin_url('post-new.php?post_type=bluesky_team'); ?>" style="color:var(--main-color)">→ Jetzt hinzufügen</a>
    </p>
    <?php endif; ?>
    </div>
</section>

<?php bluesky_footer(); get_footer(); ?>
