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

$containers = $database->select("containers", ["containerid", "barcode"]);

$addto = false;
if (!empty($VARS["addto"]) && Machine::exists($VARS["addto"])) {
    $addto = $VARS["addto"];
}
?>

<?php if ($addto != false) { ?>
    <div class="alert alert-info" role="alert">
        Select a container to place machine #<?php echo htmlspecialchars($VARS["addto"]); ?> into.
    </div>
<?php } else { ?>
    <div class="btn-group">
        <?php if ($writeaccess) { ?>
            <a href="app.php?page=addcontainer" class="btn btn-success"><i class="fas fa-plus"></i> <?php $Strings->get("Add Container"); ?></a>
        <?php } ?>
    </div>
<?php } ?>
<table id="containertable" 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-barcode hidden-sm"></i> <?php $Strings->get('Barcode'); ?></th>
            <th data-priority="3"><i class="fas fa-desktop hidden-sm"></i> <?php $Strings->get('Machines'); ?></th>
            <th data-priority="1"><i class="fas fa-memory hidden-sm"></i> <?php $Strings->get('Components'); ?></th>
            <th data-priority="2"><i class="fas fa-cubes hidden-sm"></i> <?php $Strings->get('Others'); ?></th>
            <th data-priority="2"><i class="fas fa-hashtag hidden-sm"></i> <?php $Strings->get('Total'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($containers as $c) {
            ?>
            <tr>
                <td></td>
                <td>
                    <?php
                    if ($addto != 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-barcode hidden-sm"></i> <?php $Strings->get('Barcode'); ?></th>
            <th data-priority="3"><i class="fas fa-desktop hidden-sm"></i> <?php $Strings->get('Machines'); ?></th>
            <th data-priority="1"><i class="fas fa-memory hidden-sm"></i> <?php $Strings->get('Components'); ?></th>
            <th data-priority="2"><i class="fas fa-cubes hidden-sm"></i> <?php $Strings->get('Others'); ?></th>
            <th data-priority="2"><i class="fas fa-hashtag hidden-sm"></i> <?php $Strings->get('Total'); ?></th>
        </tr>
    </tfoot>
</table>