<?php

use Illuminate\Support\Facades\Auth;
use WoWPress\Frontend\Icon;
use WoWPress\Models\Complaint;
use WoWPress\Models\User;

set_sidebar_status('top', false);


get_header();

$can_edit = current_user_can('wowpress_edit_complaints');
$forceShow = false;


$complaints = Complaint::getAll();


#$complaints = [];

?>


<section id="primary" style="margin-top:calc(-1 * var(--wowp-gap))">
    <main id="main">
        <div class="bg-black bg-opacity-60 backdrop-blur-md shadow p-auto text-center flex flex-col gap-2">
            <h2 class="text-3xl font-bold">Willkommen in unserem Kummerkasten</h2>
            <p>
                Du hast etwas auf dem Herzen oder uns einfach nur so etwas mitteilen?<br>
                Dann ist hier der richtige Ort dafür!
            </p>
            <p>
                Keiner außer dir (und den Offis) kann sehen was du hier schreibst.
            </p>
            <p>
                Wenn du "unerkannt" bleiben möchtest kannst du das Formular auch "anonym" absenden. Allerdings kann man in der Datenbank trotzdem sehen wer das Formular abgeschickt hat, um Missbrauch zu vermeiden.<br>
            </p>
        </div>
        <?php if (!$can_edit || true) : ?>
            <form id="addComplaint" method="POST" action="/request" class="hidden">
                <input type="hidden" name="action" value="addComplaint">
                <?php wp_nonce_field('addComplaint', 'addComplaint_nonce'); ?>
            </form>
            <div class="bg-black bg-opacity-60 backdrop-blur-md shadow p-auto flex flex-col gap-2">
                <h3 class="text-2xl font-bold text-center">Neue Einsendung</h3>
                <textarea form="addComplaint" name="complaint" id="complaint" class="bg-black bg-opacity-60 backdrop-blur-md text-white border-white focus:ring-alliance focus:border-alliance" rows="5"></textarea>
                <div class="flex flex-row gap-2">
                    <button form="addComplaint" type="submit" name="show" value="1" class="btn btn-outline btn-alliance flex-grow bg-black bg-opacity-60 backdrop-blur-md">Mit Charnamen senden <?= Icon::get('o-paper-airplane') ?></button>
                    <button form="addComplaint" type="submit" name="hide" value="1" class="btn btn-outline btn-alliance flex-grow bg-black bg-opacity-60 backdrop-blur-md">Anonym senden <?= Icon::get('o-paper-airplane') ?></button>
                </div>
            </div>
        <?php endif; ?>
        <?php if (!empty($complaints->first())) : ?>
            <div class="bg-black bg-opacity-60 backdrop-blur-md shadow p-auto flex flex-col gap-2">
                <h3 class="text-2xl font-bold text-center">Einsendungen</h3>
            </div>
            <?php
            foreach ($complaints as $complaint) :
            ?>

                <form id="trashComplaint<?= $complaint->ID ?>" method="POST" action="/request" class="hidden">
                    <input type="hidden" name="action" value="trashComplaint">
                    <input type="hidden" name="complaint_id" value="<?= $complaint->ID ?>">
                    <?php wp_nonce_field('trashComplaint', 'trashComplaint_nonce'); ?>
                </form>
                <div class="shadow p-auto flex flex-col gap-2 <?= strtotime($complaint->deleted_at."") > 0 ? "border border-dashed border-white bg-green-800 bg-opacity-60 backdrop-blur-md" : "bg-black bg-opacity-60 backdrop-blur-md" ?>">
                    <?php if (strtotime($complaint->deleted_at."") > 0) : ?>
                        <div class="text-end">Eintrag gelöscht</div>
                    <?php endif; ?>
                    <div class="flex flex-col gap-2">
                        <div class="flex flex-col xl:flex-row gap-2 items-stretch">
                            <?php
                            if ($complaint->canEdit() && (!$complaint->anonymous || $forceShow)) :
                            ?>
                                <div class="flex flex-col items-center">
                                    <img src="<?= $complaint->user->avatar ?>" alt="" class="">
                                    <div class="p-2 bg-glass w-full text-center"><?= $complaint->user->user_name ?></div>
                                </div>
                            <?php
                            endif;
                            if (!$complaint->canEdit() && $complaint->anonymous) :
                            ?>
                                <form id="unhideComplaint<?= $complaint->ID ?>" method="POST" action="/request" class="hidden">
                                    <input type="hidden" name="action" value="unhideComplaint">
                                    <input type="hidden" name="complaint_id" value="<?= $complaint->ID ?>">
                                    <?php wp_nonce_field('unhideComplaint', 'unhideComplaint_nonce'); ?>
                                </form>
                                <button type="submit" form="unhideComplaint<?= $complaint->ID ?>" class="btn btn-outline btn-orange">
                                    <?= Icon::get('o-user-plus') ?>
                                    <div>Anonymität aufheben</div>
                                </button>
                            <?php
                            endif;
                            ?>
                            <div class="border border-white p-2 text-white w-full">
                                <?= $complaint->complaint ?>
                            </div>
                            <?php if (!(strtotime($complaint->deleted_at."") > 0)) : ?>
                                <button type="submit" form="trashComplaint<?= $complaint->ID ?>" class="btn btn-outline btn-red">
                                    <?= Icon::get('o-trash') ?>
                                </button>
                            <?php
                            elseif ($complaint->canEdit()) :
                            ?>
                                <form id="restoreComplaint<?= $complaint->ID ?>" method="POST" action="/request" class="hidden">
                                    <input type="hidden" name="action" value="restoreComplaint">
                                    <input type="hidden" name="complaint_id" value="<?= $complaint->ID ?>">
                                    <?php wp_nonce_field('restoreComplaint', 'restoreComplaint_nonce'); ?>
                                </form>
                                <button type="submit" form="restoreComplaint<?= $complaint->ID ?>" class="btn btn-outline btn-green">
                                    <?= Icon::get('o-arrow-path') ?>
                                </button>
                            <?php
                            endif;
                            if ($complaint->canEdit()) :
                            ?>
                                <form id="deleteComplaint<?= $complaint->ID ?>" method="POST" action="/request" class="hidden">
                                    <input type="hidden" name="action" value="deleteComplaint">
                                    <input type="hidden" name="complaint_id" value="<?= $complaint->ID ?>">
                                    <?php wp_nonce_field('deleteComplaint', 'deleteComplaint_nonce'); ?>
                                </form>
                                <button type="submit" form="deleteComplaint<?= $complaint->ID ?>" class="btn btn-outline btn-red">
                                    <?= Icon::get('o-x-circle') ?>
                                </button>
                            <?php
                            endif;
                            ?>
                        </div>
                        <?php
                        if (!empty($complaint->user_comment) || $complaint->canEdit()) :
                        ?>
                            <div class="flex flex-row gap-2 items-center">
                                <div class="text-alliance">
                                    <?= Icon::get('o-shield-check') ?>
                                </div>
                                <input <?= $complaint->canEdit() ? "placeholder='Rückmeldung eingeben...'" : "disabled" ?> name="user_comment" form="commentComplaint<?= $complaint->ID ?>" type="text" class="border border-alliance p-2 text-white flex-grow bg-blue-50 bg-opacity-0 focus:ring-alliance focus:border-alliance" value="<?= $complaint->user_comment ?>">
                                <?php
                                if ($complaint->canEdit()) :
                                ?>
                                    <form id="commentComplaint<?= $complaint->ID ?>" method="POST" action="/request" class="hidden">
                                        <input type="hidden" name="action" value="commentComplaint">
                                        <input type="hidden" name="complaint_id" value="<?= $complaint->ID ?>">
                                        <?php wp_nonce_field('commentComplaint', 'commentComplaint_nonce'); ?>
                                    </form>
                                    <button type="submit" form="commentComplaint<?= $complaint->ID ?>" class="btn btn-outline btn-alliance"><?= Icon::get('o-paper-airplane') ?></button>
                                <?php
                                endif;
                                ?>
                            </div>
                        <?php
                        endif;
                        if ($complaint->canEdit()) :
                        ?>
                            <form id="slanderComplaint<?= $complaint->ID ?>" method="POST" action="/request" class="hidden">
                                <input type="hidden" name="action" value="slanderComplaint">
                                <input type="hidden" name="complaint_id" value="<?= $complaint->ID ?>">
                                <?php wp_nonce_field('slanderComplaint', 'slanderComplaint_nonce'); ?>
                            </form>
                            <div class="flex flex-row gap-2 items-center">
                                <div class="text-deathknight">
                                    <?= Icon::get('o-shield-exclamation') ?>
                                </div>
                                <input placeholder="Offi-Kommentar eingeben..." form="slanderComplaint<?= $complaint->ID ?>" name="admin_comment" type="text" class="border border-dashed border-deathknight p-2 text-white flex-grow bg-opacity-0 bg-blue-50 focus:ring-deathknight focus:border-deathknight" value="<?= $complaint->admin_comment ?>">
                                <button type="submit" form="slanderComplaint<?= $complaint->ID ?>" class="btn btn-outline btn-deathknight"><?= Icon::get('o-paper-airplane') ?></button>

                            </div>
                        <?php
                        endif;
                        ?>
                    </div>
                </div>
            <?php
            endforeach;
            ?>
        <?php endif; ?>
    </main>
</section>

<?php
get_footer();
