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

$families = $database->select("families", ['familyid (id)', 'familyname', 'phone', 'email', 'father_name (father)', 'mother_name (mother)']);
?>

<div class="btn-group">
    <?php if ($writeaccess) { ?>
        <a href="app.php?page=editfamily" class="btn btn-success"><i class="fas fa-plus"></i> <?php $Strings->get("Add Family"); ?></a>
    <?php } ?>
</div>
<table id="famtable" 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('Name'); ?></th>
            <th data-priority="3"><i class="fas fa-male hidden-sm"></i> <?php $Strings->get('Father'); ?></th>
            <th data-priority="3"><i class="fas fa-female hidden-sm"></i> <?php $Strings->get('Mother'); ?></th>
            <th data-priority="2"><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>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($families as $f) {
            ?>
            <tr>
                <td></td>
                <td>
                    <a class="btn btn-info btn-sm" href="app.php?page=viewfamily&id=<?php echo $f['id']; ?>"><i class="fas fa-eye"></i> <?php $Strings->get("View"); ?></a>
                    <?php
                    if ($writeaccess) {
                        ?>
                        <a class="btn btn-primary btn-sm" href="app.php?page=editfamily&id=<?php echo $f['id']; ?>"><i class="fas fa-edit"></i> <?php $Strings->get("Edit"); ?></a>
                        <?php
                    }
                    ?>
                </td>
                <td><?php echo $f['familyname']; ?></td>
                <td><?php echo $f['father']; ?></td>
                <td><?php echo $f['mother']; ?></td>
                <td><?php echo $f['phone']; ?></td>
                <td><?php echo $f['email']; ?></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('Name'); ?></th>
            <th data-priority="3"><i class="fas fa-male hidden-sm"></i> <?php $Strings->get('Father'); ?></th>
            <th data-priority="3"><i class="fas fa-female hidden-sm"></i> <?php $Strings->get('Mother'); ?></th>
            <th data-priority="2"><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>
        </tr>
    </tfoot>
</table>