<?php
/**
 * Template Name: BlueSky - Regeln
 */
get_header(); bluesky_navbar('rules');
$server_ip  = get_option('bluesky_server_ip','mc.example.com');
$header_bg  = bluesky_get_image('bluesky_header_bg_url','images/header-background.jpg');
$warn_text  = get_option('bluesky_rules_warning_text','Achtung: Bei einer Abwesenheit von mehr als 90 Tagen werden alle Spielerinformationen und Daten gelöscht.');
?>
<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>Regeln</span></h1>
        <p class="description">Liste aller Minecraft-Regeln.</p>
    </div>
</section>

<section id="rules">
    <div class="content">
        <div class="warning">
            <p><strong><?php echo esc_html($warn_text); ?></strong></p>
            <div class="icon"><img src="<?php echo get_template_directory_uri(); ?>/images/warnung.png" alt="Warnung" width="40" height="40"></div>
        </div>
        <?php
        $rq = new WP_Query(['post_type'=>'bluesky_rule','posts_per_page'=>-1,'post_status'=>'publish','meta_key'=>'_bluesky_rule_sort_order','orderby'=>'meta_value_num','order'=>'ASC']);
        if ($rq->have_posts()): ?>
        <div class="rules-listing">
            <?php while($rq->have_posts()): $rq->the_post();
                $sid = get_post_meta(get_the_ID(),'_bluesky_rule_section_id',true)?:'regel-'.get_the_ID();
                $items_raw = get_post_meta(get_the_ID(), '_bluesky_rule_items', true);
                $rule_items = array_values(array_filter(array_map('trim', preg_split('/\r\n|\r|\n/', (string) $items_raw))));
                $panel_id = 'rule-panel-' . get_the_ID();
            ?>
            <article id="<?php echo esc_attr($sid); ?>" class="rule-card">
                <button type="button" class="rule-toggle" aria-expanded="false" aria-controls="<?php echo esc_attr($panel_id); ?>">
                    <span class="rule-toggle-icon" aria-hidden="true"></span>
                    <span class="rules-title"><?php the_title(); ?></span>
                </button>
                <div class="rule-content-panel" id="<?php echo esc_attr($panel_id); ?>">
                    <?php if (!empty($rule_items)): ?>
                    <ul class="rule-items">
                        <?php foreach ($rule_items as $item): ?>
                        <li class="rule-item-line"><?php echo esc_html($item); ?></li>
                        <?php endforeach; ?>
                    </ul>
                    <?php else: ?>
                    <ul class="rule-items">
                        <li class="rule-item-line">Noch keine Regeln hinterlegt.</li>
                    </ul>
                    <?php endif; ?>
                </div>
            </article>
            <?php endwhile; wp_reset_postdata(); ?>
        </div>
        <?php else: ?>
        <div class="rules-listing">
            <article id="regel-1" class="rule-card">
                <button type="button" class="rule-toggle" aria-expanded="false" aria-controls="rule-panel-fallback-1">
                    <span class="rule-toggle-icon" aria-hidden="true"></span>
                    <span class="rules-title">&sect;1. Vorwort</span>
                </button>
                <div class="rule-content-panel" id="rule-panel-fallback-1">
                    <ul class="rule-items"><li class="rule-item-line">Alle Spieler müssen sich an die folgenden Regeln halten.</li></ul>
                </div>
            </article>
            <article id="regel-2" class="rule-card">
                <button type="button" class="rule-toggle" aria-expanded="false" aria-controls="rule-panel-fallback-2">
                    <span class="rule-toggle-icon" aria-hidden="true"></span>
                    <span class="rules-title">&sect;2. Inhalte</span>
                </button>
                <div class="rule-content-panel" id="rule-panel-fallback-2">
                    <ul class="rule-items"><li class="rule-item-line">Beleidigungen, Mobbing und diskriminierende Aussagen sind verboten.</li></ul>
                </div>
            </article>
        </div>
        <?php endif; ?>
    </div>
</section>
<?php bluesky_footer(); get_footer(); ?>
