<?php

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


set_sidebar_status('top', false);

$liste = $GLOBALS['wowpress']['liste'];

#$sks = new SKS();
#$sks->list_name = "Normal";
#$sks->char_name  = "Gasti";
#$sks->realm_name = "McGaston";
#$sks->rank = 4;
#$sks->active = true;
#$sks->save();


if (!$liste) {

    $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->ID ?>"><?= $list->list_name ?></a>
                <?php endforeach; ?>
            </div>
        </main>
    </section>
<?php
    get_footer();
    exit;
} else {
    $liste = SKS::find($liste);



    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>
                            <th class="p-auto hidden lg:table-cell">Status</th>
                            <?php if (current_user_can('wowpress_edit_raids')) : ?>
                                <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">
                                        <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>
                                        </div>

                                    </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>
                                <td class="p-auto text-center hidden lg:table-cell">
                                    <button class="btn w-full btn-outline btn-<?= $sk->active ? $sk->character->color : "slate" ?>"><?= $sk->active ? "Aktiv" : "Inaktiv" ?></button>
                                </td>
                                <?php if (current_user_can('wowpress_edit_raids')) : ?>
                                    <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', 'lootSKSnonce'); ?>
                                                    <input type="hidden" name="action" value="lootSKS">
                                                    <input type="hidden" name="id" value="<?= $sk->ID ?>">
                                                    <button type="submit" 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="id" value="<?= $sk->ID ?>">
                                                    <button type="submit" class="btn btn-outline btn-gray"><?= 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_characters')) : ?>
        <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">
                            <button type="submit" class="btn btn-outline btn-hunter">Hinzufügen</a>
                        </div>
                    </form>
                </div>
            </template>
        </div>

<?php
    endif;

    get_footer();
}
?>