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

$attachto = false;
if (!empty($VARS["attachto"]) && Machine::exists($VARS["attachto"])) {
    $attachto = $VARS["attachto"];
    $components = $database->select("components", ["compid", "serial", "machineid", "model", "manufacturer", "capacity"], ["machineid" => null]);
} else {
    $components = $database->select("components", ["compid", "serial", "machineid", "model", "manufacturer", "capacity"]);
}
?>

<?php if ($attachto != false) { ?>
    <div class="alert alert-info" role="alert">
        Select an unused component to attach to machine #<?php echo htmlspecialchars($VARS["attachto"]); ?>
    </div>
<?php } else { ?>
    <div class="btn-group">
        <?php if ($writeaccess) { ?>
            <a href="app.php?page=editcomponent" class="btn btn-success"><i class="fas fa-plus"></i> <?php $Strings->get("Add Component"); ?></a>
        <?php } ?>
    </div>
<?php } ?>
<table id="componenttable" 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="2"><i class="fas fa-memory hidden-sm"></i> <?php $Strings->get('ID'); ?></th>
            <th data-priority="3"><i class="fas fa-desktop hidden-sm"></i> <?php $Strings->get('Machine'); ?></th>
            <th data-priority="1"><i class="fas fa-hashtag hidden-sm"></i> <?php $Strings->get('Model'); ?></th>
            <th data-priority="2"><i class="fas fa-barcode hidden-sm"></i> <?php $Strings->get('Serial'); ?></th>
            <th data-priority="2"><i class="fas fa-database hidden-sm"></i> <?php $Strings->get('Capacity'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($components as $c) {
            ?>
            <tr>
                <td></td>
                <td>
                    <?php
                    if ($attachto != false) {
                        ?>
                        <a class="btn btn-success btn-sm" href="app.php?page=editcomponent&id=<?php echo $c['compid']; ?>&machine=<?php echo $attachto; ?>"><i class="fas fa-plus"></i> <?php $Strings->get("Attach"); ?></a>
                        <?php
                    } else if ($writeaccess) {
                        ?>
                        <a class="btn btn-primary btn-sm" href="app.php?page=editcomponent&id=<?php echo $c['compid']; ?>"><i class="fas fa-edit"></i> <?php $Strings->get("Edit"); ?></a>
                        <?php
                    }
                    ?>
                </td>
                <td><?php echo $c['compid']; ?></td>
                <td><?php
                    if (!empty($c['machineid'])) {
                        ?>
                        <a href="./app.php?page=viewmachine&id=<?php echo $c['machineid']; ?>"><?php echo $c['machineid']; ?></a>
                        <?php
                    }
                    ?></td>
                <td><?php echo $c['model'] ?? ""; ?></td>
                <td><?php echo $c['serial'] ?? ""; ?></td>
                <td><?php echo $c['capacity'] ?? ""; ?></td>
            </tr>
            <?php
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <th data-priority="0"></th>
            <th data-priority="1"><?php $Strings->get('Actions'); ?></th>
            <th data-priority="2"><i class="fas fa-memory hidden-sm"></i> <?php $Strings->get('ID'); ?></th>
            <th data-priority="3"><i class="fas fa-desktop hidden-sm"></i> <?php $Strings->get('Machine'); ?></th>
            <th data-priority="1"><i class="fas fa-hashtag hidden-sm"></i> <?php $Strings->get('Model'); ?></th>
            <th data-priority="2"><i class="fas fa-barcode hidden-sm"></i> <?php $Strings->get('Serial'); ?></th>
            <th data-priority="2"><i class="fas fa-database hidden-sm"></i> <?php $Strings->get('Capacity'); ?></th>
        </tr>
    </tfoot>
</table>