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

$writeaccess = $user->hasPermission("HACHEPORTAL_EDIT");

$payments = $database->select("payments", ['paymentid (id)', 'familyid', 'amount', 'paid', 'date', 'type']);
?>

<div class="btn-group">
    <?php if ($writeaccess) { ?>
        <a href="app.php?page=editpayment" class="btn btn-success"><i class="fas fa-plus"></i> <?php $Strings->get("Manual Entry"); ?></a>
    <?php } ?>
</div>
<table id="paytable" 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-users hidden-sm"></i> <?php $Strings->get('Family'); ?></th>
            <th data-priority="1"><i class="fas fa-dollar-sign hidden-sm"></i> <?php $Strings->get('Amount'); ?></th>
            <th data-priority="2"><i class="fas fa-calendar hidden-sm"></i> <?php $Strings->get('Date'); ?></th>
            <th data-priority="2"><i class="far fa-check-square hidden-sm"></i> <?php $Strings->get('Status'); ?></th>
            <th data-priority="3"><i class="fas fa-bars hidden-sm"></i> <?php $Strings->get('Type'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($payments as $p) {
            ?>
            <tr>
                <td></td>
                <td>
                    <?php
                    if ($writeaccess) {
                        ?>
                        <a class="btn btn-primary btn-sm" href="app.php?page=editpayment&id=<?php echo $p['id']; ?>"><i class="fas fa-edit"></i> <?php $Strings->get("Edit"); ?></a>
                        <?php
                    }
                    ?>
                </td>
                <td><a href="./app.php?page=viewfamily&id=<?php echo $p['familyid']; ?>"><?php echo (new Family())->load($p['familyid'])->getName(); ?></a></td>
                <td>$<?php echo number_format($p['amount'], 2); ?></td>
                <td><?php echo date("Y-m-d H:i:s", strtotime($p['date'])); ?></td>
                <td><?php $p['paid'] ? $Strings->get("Paid") : $Strings->get("Unpaid"); ?></td>
                <td><?php $Strings->get($p["type"]); ?></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-users hidden-sm"></i> <?php $Strings->get('Family'); ?></th>
            <th data-priority="1"><i class="fas fa-dollar-sign hidden-sm"></i> <?php $Strings->get('Amount'); ?></th>
            <th data-priority="2"><i class="fas fa-calendar hidden-sm"></i> <?php $Strings->get('Date'); ?></th>
            <th data-priority="2"><i class="far fa-check-square hidden-sm"></i> <?php $Strings->get('Status'); ?></th>
            <th data-priority="3"><i class="fas fa-bars hidden-sm"></i> <?php $Strings->get('Type'); ?></th>
        </tr>
    </tfoot>
</table>