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

if (empty($VARS['id']) || !$database->has('families', ['familyid' => $VARS['id']])) {
    header("Location: ./app.php?page=families&msg=family_doesnt_exist");
}

$famid = $VARS['id'];

$family = (new Family())->load($famid);
?>

<div class="card">
    <div class="card-body">
        <div class="card-title d-flex flex-wrap justify-content-between">
            <h3>
                <a href="app.php?page=families" class="text-body">
                    <i class="fas fa-arrow-left"></i>
                </a>
                <?php echo $family->getName(); ?> <?php $Strings->get("Family"); ?>
            </h3>
            <div>
                <?php
                if ($writeaccess) {
                    ?>
                    <a href="app.php?page=editfamily&id=<?php echo $family->getID(); ?>&source=viewfamily" class="btn btn-primary"><i class="fas fa-edit"></i> <?php $Strings->get('Edit Family'); ?></a>
                    <?php
                }
                ?>
            </div>
        </div>

        <div class="d-flex justify-content-around flex-wrap">
            <?php
            $newsletter = "Email";
            switch ($family->getNewsletter()) {
                case 1:
                    $newsletter = $Strings->get("Email", false);
                    break;
                case 2:
                    $newsletter = $Strings->get("Print", false);
                    break;
                case 3:
                    $newsletter = $Strings->get("Email and Print", false);
                    break;
            }
            $items = [
                [
                    "value" => $family->getFather(),
                    "icon" => "fas fa-male",
                    "label" => "Father"
                ],
                [
                    "value" => $family->getMother(),
                    "icon" => "fas fa-female",
                    "label" => "Mother"
                ],
                [
                    "value" => $family->getPhone(),
                    "icon" => "fas fa-phone",
                    "label" => "Phone"
                ],
                [
                    "value" => $family->getEmail(),
                    "icon" => "fas fa-at",
                    "label" => "Email"
                ],
                [
                    "value" => $family->getAddress(),
                    "icon" => "fas fa-home",
                    "label" => "Address"
                ],
                [
                    "value" => $family->getCity(),
                    "icon" => "fas fa-city",
                    "label" => "City"
                ],
                [
                    "value" => $family->getState(),
                    "icon" => "fas fa-flag",
                    "label" => "State"
                ],
                [
                    "value" => $family->getZip(),
                    "icon" => "fas fa-mail-bulk",
                    "label" => "ZIP Code"
                ],
                [
                    "value" => $family->getPhotoPermission() ? $Strings->get("Yes", false) : $Strings->get("No", false),
                    "icon" => "fas fa-camera",
                    "label" => "Photo Permission"
                ],
                [
                    "value" => $newsletter,
                    "icon" => "fas fa-newspaper",
                    "label" => "Newsletter"
                ],
            ];
            foreach ($items as $i) {
                ?>
                <div class="list-group-item h5 mb-2">
                    <i class="<?php echo $i['icon']; ?>"></i> <?php
                    $Strings->get($i['label']);
                    echo ": ";
                    echo $i['value'];
                    ?>
                </div>
                <?php
            }
            ?>
        </div>

        <h4 class="mt-4"><?php $Strings->get("Children"); ?></h4>
        <div class="table-responsive">
            <table class="table table-striped table-bordered">
                <thead>
                    <tr>
                        <th><i class="fas fa-user-graduate"></i> <?php $Strings->get("Name"); ?></th>
                        <th><i class="fas fa-birthday-cake"></i> <?php $Strings->get("Born"); ?></th>
                        <th><i class="fas fa-graduation-cap"></i> <?php $Strings->get("Graduated"); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($family->getChildren() as $c) {
                        ?>
                        <tr>
                            <td><?php echo $c->getName(); ?></td>
                            <td><?php echo date("F Y", $c->getBirthday()); ?></td>
                            <td><?php $c->isGraduated() ? $Strings->get("Yes") : $Strings->get("No"); ?></td>
                        </tr>
                        <?php
                    }
                    ?>
                </tbody>
            </table>
        </div>

        <h4 class="mt-4"><?php $Strings->get("Interests"); ?></h4>
        <?php
        $events = $database->select('events', ["[>]interests" => ['eventid' => 'eventid']], ['event (name)'], ['familyid' => $family->getID()]);

        $eventcount = count($events);

        if ($eventcount > 0) {

            $cola = [];
            $colb = [];

            for ($i = 0; $i < $eventcount; $i++) {
                if ($i % 2 === 0) {
                    $cola[] = $events[$i];
                } else {
                    $colb[] = $events[$i];
                }
            }
            ?>
            <div class="row">

                <div class="col-12 col-md-6">
                    <ul class="list-group">
                        <?php
                        foreach ($cola as $ev) {
                            ?>
                            <li class="list-group-item">
                                <?php echo $ev['name']; ?>
                            </li>
                            <?php
                        }
                        ?>
                    </ul>
                </div>

                <div class="col-12 col-md-6">
                    <ul class="list-group">
                        <?php
                        foreach ($colb as $ev) {
                            ?>
                            <li class="list-group-item">
                                <?php echo $ev['name']; ?>
                            </li>
                            <?php
                        }
                        ?>
                    </ul>
                </div>
            </div>
            <?php
        } else {
            ?>
            <i class="fas fa-info-circle"></i> <?php $Strings->get("No interests selected."); ?>
            <?php
        }
        ?>
    </div>
</div>