<?php

use WoWPress\Frontend\Icon;
use WoWPress\Frontend\ToggleButton;
use WoWPress\Models\Character;
use WoWPress\Models\Raid;



$raid = $GLOBALS['wowpress']['raid'];
set_sidebar_status('top', false);
set_sidebar_status('left', false);
set_sidebar_status('right', false);

global $user;

if ($raid->id_wowaudit) {
    if($raid->sync(true)){
        $raid->save();
    }else{
        $raid->status = "Cancelled";
        $raid->save();
    }
    
}


$unknowns = [];

get_header();
?>
<div class="top-title flex flex-col lg:flex-row gap-auto" style="margin-top:calc(-1 * var(--wowp-gap))">
    <?php if ($raid->previous) : ?>
        <a href="<?= $raid->previous->link ?>" class="btn btn-outline hidden lg:flex">Vorheriger</a>
    <?php endif; ?>
    <div class="text-3xl font-bold bg-glass shadow p-3 text-center w-full">
        <?= $raid->title ?>
    </div>
    <?php if ($raid->next) : ?>
        <a href="<?= $raid->next->link ?>" class="btn btn-outline hidden lg:flex">Nächster</a>
    <?php endif; ?>
</div>
<section id="primary">
<?php if($raid->status == "Cancelled"): ?>
    <main id="main">
    <div class="flex flex-col lg:flex-row gap-auto">
    <div class="bg-glass shadow p-auto w-full text-center">
        <h1 class="text-3xl font-bold text-red">Raid abgesagt!</h1>
    </div>
    </div>
    </main>
<?php else: ?>
    <main id="main">
        <div class="flex flex-col lg:flex-row gap-auto" x-data="{encounter:<?= $raid->encounters[0]['id'] ?>}">
            <div class="bg-glass shadow p-auto w-full lg:w-1/5 order-2 lg:order-1">
                <h3 class="text-xl font-bold text-center">Bosse</h3>
                <div class="grid gap-auto">
                    <?php foreach ($raid->encounters as $encounter) : ?>
                        <button <?= $encounter['enabled'] ? "" : "disabled" ?> x-on:click="encounter=<?= $encounter['id'] ?>" class="btn btn-outline <?= $encounter['enabled'] ? "btn-green" : "btn-slate" ?>" :class="{'current-menu-item' : encounter==<?= $encounter['id'] ?>  }"><?= $encounter['name'] ?></button>
                    <?php endforeach; ?>
                </div>
            </div>
            <div class="w-full flex flex-col gap-auto order-3 lg:order-2">
                <div class="flex flex-col lg:flex-row gap-auto">
                    <div class="bg-glass shadow p-auto w-full lg:w-1/5">
                        <div class="grid text-center gap-1">
                            <div class="bg-<?= $raid->color ?> text-black text-2xl "><?= format_date($raid->start, "EEEE") ?></div>
                            <div class="border border-<?= $raid->color ?>">
                                <div class="text-3xl p-auto "><?= format_date($raid->start, "dd. MMMM") ?></div>
                            </div>
                            <div class="border border-<?= $raid->color ?> p-2">Start: <?= format_date($raid->start, "HH:mm") ?></div>
                            <div class="border border-<?= $raid->color ?> p-2">Ende: <?= format_date($raid->end, "HH:mm") ?></div>
                            <?php if ($raid->difficulty) : ?>
                                <div class="border border-<?= $raid->color ?> p-2"><?= $raid->difficulty ?></div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="bg-glass shadow p-auto w-full">
                        <?php foreach ($raid->encounters as $encounter) : ?>
                            <div x-cloak x-show="encounter==<?= $encounter['id'] ?>">
                                <h3 class="text-xl font-bold">Informationen für : <?= $encounter['name'] ?></h3>
                                <div>
                                    <?= $encounter['notes'] ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>


                <div class="flex flex-col lg:flex-row gap-auto">
                    <?php foreach ($raid->encounters as $encounter) : ?>
                        <div x-cloak x-show="encounter==<?= $encounter['id'] ?>" class="w-full">

                            <div class="w-full grid grid-cols-1 lg:grid-cols-4 gap-auto">
                                <?php
                                foreach ($encounter['selections']->groupBy('role') as $role => $signups) :
                                    if ($role != "Unknown") :
                                ?>
                                        <div class="bg-glass shadow p-auto flex flex-col gap-auto">
                                            <h3 class="text-xl font-bold text-center"><?= $role ?></h3>
                                            <?php foreach ($signups as $signup) : ?>
                                                <?= $signup->showForm(false, true, false, $encounter['id'] == 0,true) ?>
                                            <?php endforeach; ?>
                                        </div>
                                <?php
                                    else : if ($encounter['id'] == 0) : $unknowns = $signups;
                                        endif;
                                    endif;
                                endforeach;
                                ?>
                            </div>

                        </div>
                    <?php endforeach; ?>

                </div>
            </div>
            <div class="flex flex-col gap-auto w-full lg:w-1/5 order-1 lg:order-3">
                <div class="bg-glass shadow p-auto">
                    <div class="flex flex-col gap-auto">
                        <h3 class="text-xl font-bold text-center">Status</h3>
                        <?= $raid->showSignup($user->characters->first(), true, true, true) ?>
                    </div>
                </div>
            </div>
        </div>
        <div class="w-full order-4 bg-glass shadow p-auto flex flex-col gap-auto">
            <h3 class="text-xl font-bold text-center">Keine Rückmeldung</h3>
            <div class="grid grid-cols-1 lg:grid-cols-5  flex-wrap gap-auto">
                <?php foreach ($unknowns as $signup) : ?>
                    <?= $signup->showForm() ?>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>
    </main>
</section>
<?php
get_footer();
