<?php


use WoWPress\Api\BattleNet;
use WoWPress\Database\Cache;
use WoWPress\Frontend\Icon;
use WoWPress\Models\Character;
use WoWPress\Models\User;

global $widget_area;
set_sidebar_status('top', false);

$characters = Character::orderBy('rank')->get();
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'] ?>
    </div>
</div>

<section id="primary">
    <main id="main">
        <div class="bg-glass shadow p-auto">
            <table class="table-auto w-full">
                <thead class="bg-glass">
                    <tr>
                        <td class="p-auto">Name</td>
                        <td class="p-auto hidden lg:table-cell">Klasse</td>
                        <td class="p-auto hidden lg:table-cell">Rang</td>
                        <td class="p-auto hidden lg:table-cell">Server</td>
                        <td class="p-auto hidden lg:table-cell">Gilde</td>

                        <?php if (current_user_can('wowpress_edit_characters')) : ?>
                            <td class="p-auto hidden lg:table-cell">Raider</td>

                            <td class="p-auto hidden lg:table-cell">
                                User
                            </td>
                            <td class="p-auto">Aktionen</td>
                        <?php endif; ?>
                    </tr>
                </thead>
                <tbody class="bg-slate-900">
                    <?php foreach ($characters as $char) : ?>
                        <tr class="text-<?= $char->color ?> even:bg-slate-800">
                            <td class="p-auto">
                                <div class="flex flex-row gap-2 items-center">
                                    <img class="w-[42px] aspect-1 rounded-full" src="<?= $char->avatar ?>" alt="">
                                    <span><?= $char->name ?></span>
                                </div>
                            </td>
                            <td class="p-auto hidden lg:table-cell">
                                <div class="flex flex-row gap-2 items-center">
                                    <img class="w-[30px] aspect-1 rounded-full" src="<?= $char->class_icon ?>" alt="">
                                    <img class="w-[30px] aspect-1 rounded-full" src="<?= $char->spec_icon ?>" alt="">
                                    <span><?= translate_string($char->class) ?> -
                                        <?= translate_string($char->spec) ?></span>
                                </div>
                            </td>
                            <td class="p-auto hidden lg:table-cell"><?= $char->rank ?></td>
                            <td class="p-auto hidden lg:table-cell"><?= $char->realm ?></td>
                            <td class="p-auto hidden lg:table-cell"><?= $char->guild ?></td>

                            <?php if (current_user_can('wowpress_edit_characters')) : ?>
                                <td class="p-auto hidden lg:table-cell">
                                <form action="/request" method="POST">
                                            <?php wp_nonce_field('toggleRaidchar', 'toggleRaidchar_nonce'); ?>
                                            <input type="hidden" name="action" value="toggleRaidchar">
                                            <input type="hidden" name="id" value="<?= $char->ID ?>">
                                    <?php if ($char->raidchar) : ?>
                                        <button type="submit" class="btn btn-<?=$char->color?>"><?=Icon::get('o-check-circle')?> Ja</button>
                                    <?php else : ?>
                                        <button type="submit" class="btn btn-gray"><?=Icon::get('o-no-symbol')?> Nein</button>
                                    <?php endif; ?>
                                </form>
                                </td>

                                <td class="p-auto hidden lg:table-cell">
                                    <form action="/request" method="POST">
                                        <input type="hidden" name="char_id" value="<?= $char->ID ?>">
                                        <?php wp_nonce_field('changeUser', 'changeUser_nonce'); ?>
                                        <input type="hidden" name="action" value="changeUser">
                                        <div class="flex flex-row">
                                            <div x-data="{ open: false, uid: '<?= $char->user ? $char->user->ID : "0" ?>' , selected: '<?= $char->user ? $char->user->login : "" ?>' }" @click.away="open = false" class="relative">

                                                <input type="hidden" name="uid" x-model="uid">

                                                <!-- Button -->
                                                <button type="button" @click="open = !open" class="min-w-[180px] max-w-[180px] h-full px-4 py-2 border-e-0 border border-<?= $char->color ?> flex items-center justify-between" :class="{'text-<?= $char->color ?>': selected !== '', 'text-gray-500': selected === ''}">
                                                    <span class="max-w-[120px] overflow-hidden" x-text="selected === '' ? 'Kein User' : selected"></span>
                                                    <svg class="ml-2 w-4 h-4" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" />
                                                    </svg>
                                                </button>

                                                <!-- Dropdown Menu -->
                                                <div x-show="open" class="absolute mt-2 bg-glass border border-<?= $char->color ?> w-full z-10" x-cloak>
                                                    <ul class="max-h-[240px] overflow-auto [&>li]:text-slate-200  [&>li]:px-4 [&>li]:py-2 hover:[&>li]:bg-slate-600 [&>li]:cursor-pointer">
                                                        <li @click="selected = ''; uid='0'; open = false;" value="0">Kein User</li>
                                                        <?php foreach (User::all() as $user) : ?>
                                                            <li @click="selected = '<?= $user->login ?>'; uid = '<?= $user->ID ?>'; open = false;" value="<?= $user->ID ?>"><?= $user->login ?></li>
                                                        <?php endforeach; ?>
                                                    </ul>
                                                </div>
                                            </div>
                                            <button type="submit" class="btn btn-<?= $char->color ?>"><?= Icon::get('o-user-plus') ?></button>
                                        </div>
                                    </form>
                                </td>
                                <td class="p-auto">
                                    <div class="flex flex-row justify-end gap-2">
                                        <form action="/request" method="POST">
                                            <?php wp_nonce_field('updateCharacter', 'updateCharacter_nonce'); ?>
                                            <input type="hidden" name="action" value="updateCharacter">
                                            <input type="hidden" name="id" value="<?= $char->ID ?>">
                                            <button type="submit" class="btn btn-outline button-green"><?= Icon::get('o-arrow-path') ?></button>
                                        </form>
                                        <form action="/request" method="POST">
                                            <?php wp_nonce_field('deleteCharacter', 'deleteCharacter_nonce'); ?>
                                            <input type="hidden" name="action" value="deleteCharacter">
                                            <input type="hidden" name="id" value="<?= $char->ID ?>">
                                            <button type="submit" class="btn btn-outline button-red"><?= Icon::get('o-trash') ?></button>
                                        </form>
                                    </div>
                                </td>
                            <?php endif; ?>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </main><!-- #main -->
</section><!-- #primary -->
<?php if (current_user_can('wowpress_edit_characters')) : ?>
    <div x-data="{addChar:true}">
        <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('addCharacter', 'addCharacter_nonce'); ?>
                        <input type="hidden" name="action" value="addCharacter">
                        <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();
