<?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();

$where = [];
$filter = $Strings->get("Filter", false);
if (!empty($VARS['filter']) && preg_match("/(camper|adult|youth)/", $VARS['filter'])) {
    $where = [
        $VARS['filter'] . 'id[!]' => null
    ];
    $filter = $Strings->get(ucfirst($VARS['filter'] . ($VARS['filter'] == "youth" ? "" : "s")), false);
}
?>

<div class="btn-toolbar mb-2">
    <div class="btn-group mr-2">
        <div class="dropdown">
            <button id="addpersonbtn" type="button" class="btn btn-green dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="fas fa-plus"></i> <?php $Strings->get("New"); ?>
            </button>
            <div class="dropdown-menu" aria-labelledby="addpersonbtn">
                <a href="app.php?page=editperson&type=camper" class="dropdown-item"><i class="fas fa-campground fa-fw"></i> <?php $Strings->get("Camper"); ?></a>
                <a href="app.php?page=editperson&type=adult" class="dropdown-item"><i class="fas fa-hiking fa-fw"></i> <?php $Strings->get("Adult"); ?></a>
                <a href="app.php?page=editperson&type=youth" class="dropdown-item"><i class="fas fa-walking fa-fw"></i> <?php $Strings->get("Youth"); ?></a>
            </div>
        </div>
    </div>
    <div class="btn-group">
        <div class="dropdown">
            <button id="filterdropbtn" type="button" class="btn btn-blue-grey dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="fas fa-filter"></i> <?php echo $filter; ?>
            </button>
            <div class="dropdown-menu" aria-labelledby="filterdropbtn">
                <a class="dropdown-item" href="./app.php?page=people"><i class="fas fa-users fa-fw"></i> <?php $Strings->get("All"); ?></a>
                <a class="dropdown-item" href="./app.php?page=people&amp;filter=camper"><i class="fas fa-campground fa-fw"></i> <?php $Strings->get("Campers"); ?></a>
                <a class="dropdown-item" href="./app.php?page=people&amp;filter=adult"><i class="fas fa-hiking fa-fw"></i> <?php $Strings->get("Adults"); ?></a>
                <a class="dropdown-item" href="./app.php?page=people&amp;filter=youth"><i class="fas fa-walking fa-fw"></i> <?php $Strings->get("Youth"); ?></a>
            </div>
        </div>
    </div>
</div>

<table id="campertable" class="table table-bordered table-hover table-sm">
    <thead>
        <tr>
            <th data-priority="0"></th>
            <th data-priority="1"><?php $Strings->get('Actions'); ?></th>
            <th data-priority="1"><i class="fas fa-user hidden-sm"></i> <?php $Strings->get('First'); ?></th>
            <th data-priority="2"><i class="fas fa-users hidden-sm"></i> <?php $Strings->get('Last'); ?></th>
            <th data-priority="2"><i class="fas fa-user-tag hidden-sm"></i> <?php $Strings->get('Type'); ?></th>
            <th data-priority="3"><i class="fas fa-phone hidden-sm"></i> <?php $Strings->get('Phone'); ?></th>
            <th data-priority="4"><i class="fas fa-at hidden-sm"></i> <?php $Strings->get('Email'); ?></th>
            <th data-priority="5"><i class="fas fa-hashtag hidden-sm"></i> <?php $Strings->get('Unit'); ?></th>
            <th data-priority="5"><i class="fas fa-tshirt hidden-sm"></i> <?php $Strings->get('Shirt'); ?></th>
            <th data-priority="5"><i class="fas fa-restroom hidden-sm"></i> <?php $Strings->get('Sex'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        $people = $database->select(
                "people", [
            'personid (id)',
            'camperid',
            'adultid',
            'youthid',
            'firstname',
            'lastname',
            'address',
            'zip',
            'phone1',
            'phone2',
            'email',
            'unit',
            'shirt',
            'sex'
                ], $where);

        foreach ($people as $person) {
            ?>
            <tr>
                <td></td>
                <td>
                    <span class="d-none"><?php echo $person['id']; ?></span>
                    <a class="btn btn-primary btn-sm" href="app.php?page=editperson&id=<?php echo $person['id']; ?>"><i class="fas fa-edit"></i> <?php $Strings->get("Edit"); ?></a>
                </td>
                <td><?php echo $person['firstname']; ?></td>
                <td><?php echo $person['lastname']; ?></td>
                <td><?php
                    if (!is_null($person['camperid'])) {
                        $Strings->get("Camper");
                    } else if (!is_null($person['adultid'])) {
                        $Strings->get("Adult");
                    } else if (!is_null($person['youthid'])) {
                        $Strings->get("Youth");
                    }
                    ?></td>
                <td><?php echo $person['phone1']; ?></td>
                <td><?php echo $person['email']; ?></td>
                <td><?php echo $person['unit']; ?></td>
                <td><?php echo $person['shirt']; ?></td>
                <td><?php echo $person['sex']; ?></td>
            </tr>
            <?php
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <th data-priority="0"></th>
            <th data-priority="1"><?php $Strings->get('Actions'); ?></th>
            <th data-priority="1"><i class="fas fa-user hidden-sm"></i> <?php $Strings->get('First'); ?></th>
            <th data-priority="2"><i class="fas fa-users hidden-sm"></i> <?php $Strings->get('Last'); ?></th>
            <th data-priority="2"><i class="fas fa-user-tag hidden-sm"></i> <?php $Strings->get('Type'); ?></th>
            <th data-priority="3"><i class="fas fa-phone hidden-sm"></i> <?php $Strings->get('Phone'); ?></th>
            <th data-priority="4"><i class="fas fa-at hidden-sm"></i> <?php $Strings->get('Email'); ?></th>
            <th data-priority="5"><i class="fas fa-hashtag hidden-sm"></i> <?php $Strings->get('Unit'); ?></th>
            <th data-priority="5"><i class="fas fa-tshirt hidden-sm"></i> <?php $Strings->get('Shirt'); ?></th>
            <th data-priority="5"><i class="fas fa-restroom hidden-sm"></i> <?php $Strings->get('Sex'); ?></th>
        </tr>
    </tfoot>
</table>