<?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']);

$where = [];
$filter = $Strings->get("Filter", false);
if (!empty($VARS['filter']) && preg_match("/(giver|receiver|merchant)/", $VARS['filter'])) {
    $filtertype = 0;
    switch ($VARS['filter']) {
        case "giver":
            $filtertype = 1;
            break;
        case "receiver":
            $filtertype = 2;
            break;
        case "merchant":
            $filtertype = 3;
            break;
    }
    $where = [
        "type" => $filtertype
    ];
    $filter = $Strings->get(ucfirst($VARS['filter']), 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=editaccount&type=giver" class="dropdown-item"><i class="fas fa-arrow-circle-up fa-fw"></i> <?php $Strings->get("Giver"); ?></a>
                <a href="app.php?page=editaccount&type=receiver" class="dropdown-item"><i class="fas fa-arrow-circle-down fa-fw"></i> <?php $Strings->get("Receiver"); ?></a>
                <a href="app.php?page=editaccount&type=merchant" class="dropdown-item"><i class="fas fa-cash-register fa-fw"></i> <?php $Strings->get("Merchant"); ?></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=accounts"><i class="fas fa-users fa-fw"></i> <?php $Strings->get("All"); ?></a>
                <a class="dropdown-item" href="./app.php?page=accounts&amp;filter=giver"><i class="fas fa-arrow-circle-up fa-fw"></i> <?php $Strings->get("Giver"); ?></a>
                <a class="dropdown-item" href="./app.php?page=accounts&amp;filter=receiver"><i class="fas fa-arrow-circle-down fa-fw"></i> <?php $Strings->get("Receiver"); ?></a>
                <a class="dropdown-item" href="./app.php?page=accounts&amp;filter=merchant"><i class="fas fa-cash-register fa-fw"></i> <?php $Strings->get("Merchant"); ?></a>
            </div>
        </div>
    </div>
</div>

<table id="accounttable" 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-id-card hidden-sm"></i> <?php $Strings->get('Username'); ?></th>
            <th data-priority="2"><i class="fas fa-user hidden-sm"></i> <?php $Strings->get('Name'); ?></th>
            <th data-priority="1"><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="1"><i class="fas fa-money-bill hidden-sm"></i> <?php $Strings->get('Balance'); ?></th>
            <th data-priority="5"><i class="fas fa-qrcode hidden-sm"></i> <?php $Strings->get('Public ID'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        $people = $database->select(
                "accounts", ["[>]people_types" => "type"], [
            'accountid (id)',
            'name',
            'accounts.type',
            'people_types.typename',
            'username',
            'phone',
            'email',
            'balance',
            'publicid'
                ], $where);

        foreach ($people as $person) {
            ?>
            <tr>
                <td></td>
                <td>
                    <a class="btn btn-primary btn-sm" href="app.php?page=editaccount&id=<?php echo $person['id']; ?>"><i class="fas fa-edit"></i> <?php $Strings->get("Edit"); ?></a>
                </td>
                <td><?php echo $person['username']; ?></td>
                <td><?php echo $person['name']; ?></td>
                <td><i class="fas fa-fw <?php
                    switch ($person['type']) {
                        case 1:
                            echo "fa-arrow-circle-up text-green";
                            break;
                        case 2:
                            echo "fa-arrow-circle-down text-blue";
                            break;
                        case 3:
                            echo "fa-cash-register";
                            break;
                    }
                    ?>"></i> <?php echo $person['typename']; ?></td>
                <td><?php echo $person['phone']; ?></td>
                <td><?php echo $person['email']; ?></td>
                <td><?php echo $person['balance']; ?></td>
                <td><?php echo $person['publicid']; ?></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-id-card hidden-sm"></i> <?php $Strings->get('Username'); ?></th>
            <th data-priority="2"><i class="fas fa-user hidden-sm"></i> <?php $Strings->get('Name'); ?></th>
            <th data-priority="1"><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="1"><i class="fas fa-money-bill hidden-sm"></i> <?php $Strings->get('Balance'); ?></th>
            <th data-priority="5"><i class="fas fa-qrcode hidden-sm"></i> <?php $Strings->get('Public ID'); ?></th>
        </tr>
    </tfoot>
</table>