<?php

use Wenprise\Eloquent\Database;
use Wenprise\Eloquent\Facades\DB;
use WoWPress\Frontend\Icon;
use WoWPress\Models\Log;
use WoWPress\Models\SKS;



set_sidebar_status('top', false);

$liste = $GLOBALS['wowpress']['liste'];
if (!empty($liste)) {
    $liste = SKS::where('list_name', $liste)->first();
    if (empty($liste->list_name)) {
        $liste = SKS::first();
    }
}


if (empty($liste->list_name)) {

    $listen = SKS::getLists();

    get_header();
?>
    <div class="top-title flex flex-row gap-2" style="margin-top:calc(-1 * var(--wowp-gap))">
        <div class="text-3xl font-bold bg-glass shadow p-3 text-center w-full">
            <?= $GLOBALS['wowpress']['page_title'] ?>n
        </div>
    </div>
    <section id="primary">
        <main id="main">
            <div class="bg-glass shadow p-auto">
                <?php foreach ($listen as $list) : ?>
                    <a class="btn btn-outline" href="/sks/<?= $list->list_name ?>"><?= $list->list_name ?></a>
                <?php endforeach; ?>
            </div>
        </main>
    </section>
<?php
} else {
    get_header();
?>
    <div class="top-title flex flex-row gap-2" style="margin-top:calc(-1 * var(--wowp-gap))">
        <div class="text-3xl font-bold bg-glass shadow p-3 text-center w-full">
            <?= $GLOBALS['wowpress']['page_title'] ?> "<?= $liste->list_name ?>"
        </div>
    </div>
    <section id="primary">
        <main id="main">
            <div class="bg-glass shadow p-auto">
                <table class="table-auto w-full">
                    <thead>
                        <tr class="bg-glass">
                            <th class="p-auto w-40">Rang</th>
                            <th class="p-auto">Charakter</th>
                            <th class="p-auto hidden lg:table-cell">Server</th>
                            <?php if (current_user_can('wowpress_edit_raids')) : ?>
                                <th class="p-auto hidden lg:table-cell">Status</th>

                                <th class="p-auto w-0">Aktion</th>
                            <?php endif; ?>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach (SKS::getList($liste->list_name) as $sk) :
                        ?>
                            <tr class="text-<?= $sk->active ? $sk->character->color : "slate-500" ?> odd:bg-slate-900">
                                <?php if (current_user_can('wowpress_edit_raids')) : ?>
                                    <td class="p-auto text-center">
                                        <form action="/request" method="POST">
                                            <div class="flex flex-row">

                                                <input type="text" class="bg-glass w-full text-center border-0 border-b border-<?= $sk->character->color ?>" name="rank" value="<?= $sk->rank ?>">
                                                <button class="btn btn-outline btn-alliance" title="Manueller Eingriff">
                                                    <?= Icon::get('o-wrench') ?>
                                                </button>

                                                <?php wp_nonce_field('fixSKS', 'fixSKS_nonce'); ?>
                                                <input type="hidden" name="action" value="fixSKS">
                                                <input type="hidden" name="sks_id" value="<?= $sk->ID ?>">

                                            </div>

                                        </form>


                                    </td>
                                <?php else : ?>
                                    <td class="p-auto text-center"><?= $sk->rank ?></td>
                                <?php endif; ?>
                                <td class="p-auto text-center"><?= $sk->character->name ?></td>
                                <td class="p-auto text-center hidden lg:table-cell"><?= $sk->character->realm ?></td>

                                <?php if (current_user_can('wowpress_edit_raids')) : ?>
                                    <td class="p-auto text-center hidden lg:table-cell">
                                        <form action="/request" method="POST">
                                            <?php wp_nonce_field('toggleSKS', 'toggleSKS_nonce'); ?>
                                            <input type="hidden" name="action" value="toggleSKS">
                                            <input type="hidden" name="sks_id" value="<?= $sk->ID ?>">
                                            <button type="submit" class="btn w-full btn-outline btn-<?= $sk->active ? $sk->character->color : "slate" ?>"><?= $sk->active ? "Aktiv" : "Inaktiv" ?></button>
                                        </form>
                                    </td>
                                    <td class="p-auto">
                                        <div class="flex gap-2 flex-col lg:flex-row justify-end">
                                            <?php if ($sk->active) : ?>
                                                <form action="/request" method="POST">
                                                    <?php wp_nonce_field('lootSKS', 'lootSKS_nonce'); ?>
                                                    <input type="hidden" name="action" value="lootSKS">
                                                    <input type="hidden" name="sks_id" value="<?= $sk->ID ?>">
                                                    <button type="submit" title="Looten" class="btn btn-outline btn-<?= $sk->character->color ?>"><?= Icon::get('o-sparkles') ?></button>
                                                </form>
                                            <?php else : ?>
                                                <form action="/request" method="POST">
                                                    <?php wp_nonce_field('deleteSKS', 'deleteSKS_nonce'); ?>
                                                    <input type="hidden" name="action" value="deleteSKS">
                                                    <input type="hidden" name="sks_id" value="<?= $sk->ID ?>">
                                                    <button type="submit" title="Löschen" class="btn btn-outline btn-red"><?= Icon::get('o-trash') ?></button>
                                                </form>
                                            <? endif; ?>

                                        </div>

                                    </td>
                                <?php endif; ?>
                            </tr>
            </div>
        <?php
                        endforeach;
        ?>
        </tbody>
        </table>

        </div>
        </main>
    </section>
<?php
}

if (current_user_can('wowpress_edit_raids')) : ?>
    <div x-data>
        <template x-teleport="#sidebar_right">
            <div class="bg-glass shadow p-auto order-1">
                <h4 class="text-xl font-bold text-center mb-2">Charakter Hinzufügen</h4>
                <form action="/request" method="POST">
                    <div class="flex flex-col gap-2">
                        <?php wp_nonce_field('addSKS', 'addSKS_nonce'); ?>
                        <input type="hidden" name="action" value="addSKS">
                        <input type="text" name="name" placeholder="Name" class="text-black">
                        <input type="text" name="realm" placeholder="Server" class="text-black">
                        <input type="text" name="list_name" placeholder="Listenname" value="<?= !empty($liste->list_name) ? $liste->list_name : "" ?>" class="text-black">

                        <button type="submit" class="btn btn-outline btn-hunter">Hinzufügen</a>
                    </div>
                </form>
            </div>
        </template>
        <?php if (!empty($liste->list_name)) : ?>
            <template x-teleport="#sidebar_right">
                <div class="bg-glass shadow p-auto order-1">
                    <h4 class="text-xl font-bold text-center mb-2">Liste neu Würfeln</h4>
                    <form action="/request" method="POST">
                        <div class="flex flex-col gap-2">
                            <?php wp_nonce_field('rollSKS', 'rollSKS_nonce'); ?>
                            <input type="hidden" name="action" value="rollSKS">
                            <input type="hidden" name="list_name" value="<?= $liste->list_name ?>">
                            <button type="submit" class="btn btn-outline btn-orange"><?= Icon::get('o-arrow-path') ?> Würfeln</a>
                        </div>
                    </form>
                </div>
            </template>
        <?php endif; ?>

    </div>

<?php
endif;

get_footer();



?>