<?php
/*
 * Copyright 2019 Netsyms Technologies.
 * 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("MACHINEMANAGER_VIEW")) {
    header("Location: ./app.php?msg=no_permission");
    die();
}

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

$clients = Clients::getAll();
?>

<div class="btn-group">
    <?php if ($writeaccess) { ?>
        <a href="app.php?page=editclient" class="btn btn-success"><i class="fas fa-plus"></i> <?php $Strings->get("Add Client"); ?></a>
    <?php } ?>
</div>
<table id="clienttable" class="table table-bordered table-hover">
    <thead>
        <tr>
            <th data-priority="0"></th>
            <th data-priority="1"><i class="fas fa-user hidden-sm"></i> <?php $Strings->get('Name'); ?></th>
            <th data-priority="2"><i class="fas fa-phone hidden-sm"></i> <?php $Strings->get('Phone'); ?></th>
            <th data-priority="2"><i class="fas fa-envelope hidden-sm"></i> <?php $Strings->get('Email'); ?></th>
            <th data-priority="3"><i class="fas fa-file-invoice hidden-sm"></i> <?php $Strings->get('Billing Address'); ?></th>
            <th data-priority="3"><i class="fas fa-mail-bulk hidden-sm"></i> <?php $Strings->get('Mailing Address'); ?></th>
            <th data-priority="4"><i class="far fa-comment-dots hidden-sm"></i> <?php $Strings->get('Public Notes'); ?></th>
            <th data-priority="4"><i class="fas fa-comment-dots hidden-sm"></i> <?php $Strings->get('Private Notes'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($clients as $c) {
            ?>
            <tr>
                <td></td>
                <td>
                    <?php
                    if ($c->isLocal() && $writeaccess) {
                        ?>
                        <a class="btn btn-primary btn-sm" href="app.php?page=editclient&arg=<?php echo $c->getID(); ?>"><i class="fas fa-edit"></i> <?php $Strings->get("Edit"); ?></a>
                        <a href="app.php?page=editclient&arg=<?php echo $c->getID(); ?>"><?php echo htmlspecialchars($c->getName()); ?></a>
                        <?php
                    } else {
                        ?>
                        <?php echo htmlspecialchars($c->getName()); ?>
                        <?php
                    }
                    ?>
                </td>
                <td>
                    <?php if (!empty($c->getPhone())) { ?>
                        <a href="tel:<?php echo htmlspecialchars(preg_replace("/[^0-9]/", "", $c->getPhone())); ?>">
                            <?php echo htmlspecialchars($c->getPhone()); ?>
                        </a>
                    <?php } ?>
                </td>
                <td>
                    <?php if (!empty($c->getEmail())) { ?>
                        <a href="mailto:<?php echo htmlspecialchars($c->getEmail()); ?>">
                            <?php echo htmlspecialchars($c->getEmail()); ?>
                        </a>
                    <?php } ?>
                </td>
                <td><?php echo implode("<br />", explode("\n", $c->getBillingAddress())); ?></td>
                <td><?php echo implode("<br />", explode("\n", $c->getMailingAddress())); ?></td>
                <td><?php echo htmlspecialchars($c->getPublicNotes()); ?></td>
                <td><?php echo htmlspecialchars($c->getPrivateNotes()); ?></td>
            </tr>
            <?php
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <th data-priority="0"></th>
            <th data-priority="1"><i class="fas fa-user hidden-sm"></i> <?php $Strings->get('Name'); ?></th>
            <th data-priority="2"><i class="fas fa-phone hidden-sm"></i> <?php $Strings->get('Phone'); ?></th>
            <th data-priority="2"><i class="fas fa-envelope hidden-sm"></i> <?php $Strings->get('Email'); ?></th>
            <th data-priority="3"><i class="fas fa-file-invoice hidden-sm"></i> <?php $Strings->get('Billing Address'); ?></th>
            <th data-priority="3"><i class="fas fa-mail-bulk hidden-sm"></i> <?php $Strings->get('Mailing Address'); ?></th>
            <th data-priority="4"><i class="far fa-comment-dots hidden-sm"></i> <?php $Strings->get('Public Notes'); ?></th>
            <th data-priority="4"><i class="fas fa-comment-dots hidden-sm"></i> <?php $Strings->get('Private Notes'); ?></th>
        </tr>
    </tfoot>
</table>