<?php
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

redirectIfNotLoggedIn();
$user = new User($_SESSION['uid']);
if (!$user->hasPermission("HACHEPORTAL_VIEW")) {
    header("Location: ./app.php?msg=no_permission");
    die();
}
$editpermission = $user->hasPermission("HACHEPORTAL_EDIT");
$editable = ($editpermission && !empty($_GET['edit']));

$events = $database->select('events', ['eventid (id)', 'event (name)']);
?>

<div class="row">

    <div class="col-sm-6">
        <div class="card">
            <div class="card-header">
                <h4>
                    <i class="fas fa-sort-amount-up"></i>
                    <?php $Strings->get("Popularity"); ?>
                </h4>
            </div>

            <ul class="list-group list-group-flush">
                <li class="list-group-item d-flex justify-content-between">
                    <b><?php $Strings->get("Event"); ?></b>
                    <b><?php $Strings->get("Families"); ?></b>
                </li>
                <?php
                $interests = $database->select("interests", ["[>]events" => ['eventid' => 'eventid']], ['interests.eventid (id)', 'events.event (name)']);
                $ranking = [];
                $rankingtitles = [];
                foreach ($interests as $ev) {
                    if (empty($ranking[$ev['id']])) {
                        $ranking[$ev['id']] = 1;
                        $rankingtitles[$ev['id']] = $ev['name'];
                    } else {
                        $ranking[$ev['id']] ++;
                    }
                }
                arsort($ranking);
                foreach ($ranking as $k => $v) {
                    ?>
                    <li class="list-group-item d-flex justify-content-between">
                        <span><?php echo $rankingtitles[$k]; ?></span>
                        <span><?php echo $v; ?></span>
                    </li>
                    <?php
                }
                ?>
            </ul>
        </div>
    </div>

    <div class="col-sm-6">
        <?php if ($editable) { ?>
            <form action="action.php" method="POST">
                <input type="hidden" name="action" value="editevents" />
                <input type="hidden" name="source" value="events" />
            <?php } ?>

            <div class="card">
                <div class="card-header">
                    <h4 class="d-flex justify-content-between">
                        <span>
                            <i class="fas fa-list-ul"></i>
                            <?php $Strings->get("Event List"); ?>
                        </span>
                        <?php if ($editpermission && !$editable) { ?>
                            <span>
                                <a class="btn btn-primary btn-sm" href="./app.php?page=events&edit=1">
                                    <i class="fas fa-edit"></i>
                                    <?php $Strings->get("Edit"); ?>
                                </a>
                            </span>
                        <?php } ?>
                    </h4>
                </div>
                <ul class="list-group list-group-flush">
                    <?php
                    foreach ($events as $ev) {
                        ?>
                        <li class="list-group-item py-0 px-1">
                            <?php
                            if ($editable) {
                                ?>
                                <input type="text" class="form-control form-control-sm" name="events[<?php echo $ev['id']; ?>]" value="<?php echo htmlspecialchars($ev['name']); ?>" />
                                <?php
                            } else {
                                echo $ev['name'];
                            }
                            ?>
                        </li>
                        <?php
                    }
                    ?>
                    <?php
                    if ($editable) {
                        for ($i = 0; $i < 5; $i++) {
                            ?>
                            <li class="list-group-item py-0 px-1">
                                <input type="text" class="form-control form-control-sm" name="events[]" value="" />
                            </li>
                            <?php
                        }
                    }
                    ?>
                </ul>
                <?php if ($editable) { ?>
                    <div class="card-footer">
                        <button type="submit" class="btn btn-success"><i class="fas fa-save"></i> <?php $Strings->get("Save"); ?></button>
                    </div>
                <?php } ?>
            </div>

            <?php if ($editable) { ?>
            </form>
        <?php } ?>
    </div>

</div>