<?php

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

global $widget_area;

global $year;
global $month;
set_sidebar_status('top', false);

switch ($month) {
    case "01":
        $prev_month = 12;
        $prev_year = $year - 1;
        $next_month = $month + 1;
        $next_year = $year;
        break;
    case "12":
        $prev_month = $month - 1;
        $prev_year = $year;
        $next_month = "01";
        $next_year = $year + 1;
        break;
    default:
        $prev_month = $month - 1;
        $prev_year = $year;
        $next_month = $month + 1;
        $next_year = $year;
        break;
}

$raids = Raid::where("start", ">", "$year-$month-01 00:00:00")->where("start", "<", "$year-$next_month-01 00:00:00")->orderBy('start')->get();

get_header();
?>
<div class="top-title flex flex-row gap-2" style="margin-top:calc(-1 * var(--wowp-gap))">
    <a href="/raids/<?= $prev_year ?>/<?= $prev_month ?>" class="bg-glass shadow p-3 flex flex-col items-center justify-center"><?= Icon::get('o-chevron-double-left') ?></a>
    <div class="text-3xl font-bold bg-glass shadow p-3 text-center w-full">
        Raids
    </div>
    <a href="/raids/<?= $next_year ?>/<?= $next_month ?>" class="bg-glass shadow p-3 flex flex-col items-center justify-center"><?= Icon::get('o-chevron-double-right') ?></a>

</div>

<section id="primary">
    <main id="main">
        <div class="bg-glass shadow p-auto hidden lg:block">
            <div class="text-xl font-bold bg-glass text-white text-center"><?= format_date("$year-$month-1", 'MMMM yyyy') ?></div>
            <div class="grid grid-cols-7 gap-1 pt-1" style="grid-auto-rows: 1fr">
                <?php
                for ($i = 1; $i <= date("t", strtotime("$year-$month-01")); $i++) {
                    $has_raid = [];
                    foreach ($raids as $raid) {
                        if (format_date($raid->start) == format_date("$year-$month-$i")) {
                            $has_raid[] = $raid;
                        }
                    }
                ?>
                    <div class="bg-glass p-auto text-center aspect-[1/0.8] <?php if (count($has_raid) > 0) { ?> glow shadow-<?= $has_raid[0]->color ?> <?php } ?> grid grid-cols-2 grid-rows-2" <?php if (count($has_raid) > 0) { ?> style="background-position:center;background-size: cover;background-image:url('<?=$has_raid[0]->thumbnail?>')" <?php } ?>>
                        <div class="text-left"><?= date('d', strtotime("$year-$month-$i")) ?></div>
                        <div class="text-right"><?= format_date("$year-$month-$i", 'E') ?></div>

                        <?php
                        foreach ($has_raid as $raid) {
                        ?>
                            <a href="<?= $raid->link ?>" class="group" style="grid-area: 2 / 1 / 3 / 3;">
                                <div class="group-hover:text-<?= $raid->color ?> bg-<?= $raid->color ?>-200"><?= $raid->title ?></div>
                                <?php if ($raid->difficulty) : ?><div class="text-sm text-slate-500">(<?= $raid->difficulty ?>)</div><?php endif; ?>
                            </a>
                        <?php

                        }
                        ?>
                    </div>
                <?php
                }
                ?>
            </div>
        </div>
        <div class="bg-glass shadow p-auto block lg:hidden">
            <div class="text-xl font-bold bg-glass text-white text-center"><?= format_date("$year-$month-1", 'MMMM yyyy') ?></div>
            <div class="flex flex-col gap-2">
                <?php foreach ($raids as $raid) : ?>
                    <a href="<?= $raid->link ?>" class="group text-center p-2 border border-<?= $raid->color ?>">
                        <div class="group-hover:text-<?= $raid->color ?> bg-<?= $raid->color ?>-200"><?= $raid->title ?></div>
                        <div><?= format_date($raid->start, 'dd.MM.yyyy HH:mm') ?></div>

                        <?php if ($raid->difficulty) : ?><div class="text-sm text-slate-500">(<?= $raid->difficulty ?>)</div><?php endif; ?>
                    </a>
                <?php endforeach; ?>
            </div>
        </div>
    </main>
</section>
<?php if (current_user_can('wowpress_edit_characters')) : ?>
    <div x-data="{addRaid:true}">
        <template x-teleport="#sidebar_right">
            <div class="bg-glass shadow p-auto order-1 hidden lg:block">
                <div class="flex flex-col gap-2">
                    <h4 class="text-xl font-bold text-center mb-2">Raid importieren</h4>
                    <form action="/request" method="POST">
                        <div class="flex flex-col gap-2">
                            <?php wp_nonce_field('importRaid', 'importRaid_nonce'); ?>
                            <input type="hidden" name="action" value="importRaid">
                            <input type="text" name="id_wowaudit" placeholder="WoW-Audit ID" class="text-black">
                            <button type="submit" class="btn btn-outline btn-hunter">Importieren</a>
                        </div>
                    </form>
                    <!--
                <div class="text-sm text-center">- oder -</div>
                <h4 class="text-xl font-bold text-center mb-2 mt-2">Termin erstellen</h4>
                <form action="/request" method="POST">
                    <div class="flex flex-col gap-2">
                        <?php wp_nonce_field('addRaid', 'addRaid_nonce'); ?>
                        <input type="hidden" name="action" value="addRaid">
                        <input type="text" name="name" placeholder="Titel" class="text-black">
                        <input type="text" name="difficulty" placeholder="Schwierigkeit" class="text-black">
                        <label for="start_date">Datum</label>
                        <input type="date" id="start_date" name="start_date"  class="text-black">
                        <label for="start_time">Start</label>
                        <input type="time" name="start_time" id="start_time" class="text-black">
                        <label for="end_time">Datum</label>
                        <input type="time" name="end_time" id="end_time" class="text-black">


                        <button type="submit" class="btn btn-outline btn-hunter">Hinzufügen</a>
                    </div>
                </form>
                </div>
-->
                </div>
        </template>
    </div>

<?php
endif;

get_footer();