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

$machines = $database->query("SELECT machines.machineid, machines.clientid, machines.model, machines.os, machines.serial, events.date AS eventdate, events.eventid, event_types.eventname
FROM   machines
LEFT OUTER JOIN events ON events.machineid = machines.machineid
LEFT OUTER JOIN event_types ON event_types.eventid = events.eventid
WHERE date=(SELECT MAX(s2.date)
                FROM events s2
                WHERE machines.machineid = s2.machineid
            )
OR NOT EXISTS (SELECT * FROM events WHERE events.machineid = machines.machineid)")->fetchAll();
?>

<div class="btn-group">
    <?php if ($writeaccess) { ?>
        <a href="app.php?page=editmachine" class="btn btn-success"><i class="fas fa-plus"></i> <?php $Strings->get("Add Machine"); ?></a>
    <?php } ?>
</div>
<table id="machinetable" 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-desktop hidden-sm"></i> <?php $Strings->get('ID'); ?></th>
            <th data-priority="2"><i class="fas fa-hdd hidden-sm"></i> <?php $Strings->get('OS/Software'); ?></th>
            <th data-priority="3"><i class="fas fa-hashtag hidden-sm"></i> <?php $Strings->get('Model'); ?></th>
            <th data-priority="3"><i class="fas fa-calendar hidden-sm"></i> <?php $Strings->get('Last Event'); ?></th>
            <th data-priority="1"><i class="fas fa-barcode hidden-sm"></i> <?php $Strings->get('Serial'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($machines as $m) {
            ?>
            <tr>
                <td></td>
                <td>
                    <?php
                    if ($writeaccess) {
                        ?>
                        <a class="btn btn-primary btn-sm" href="app.php?page=editmachine&arg=<?php echo $m['machineid']; ?>"><i class="fas fa-edit"></i> <?php $Strings->get("Edit"); ?></a>
                        <a class="btn btn-success btn-sm" href="app.php?page=addevent&id=<?php echo $m['machineid']; ?>" ><i class="fas fa-history"></i> <?php $Strings->get("Add Event"); ?></a>
                        <?php
                    }
                    ?>
                    <a class="btn btn-info btn-sm" href="app.php?page=viewmachine&id=<?php echo $m['machineid']; ?>"><i class="fas fa-eye"></i> <?php $Strings->get("View"); ?></a>
                </td>
                <td><a href="./app.php?page=viewmachine&id=<?php echo $m['machineid']; ?>"><?php echo $m['machineid']; ?></a></td>
                <td><?php echo $m['os']; ?></td>
                <td><?php echo $m['model']; ?></td>
                <td><span class="d-none"><?php echo $m['eventdate']; ?></span><?php echo empty($m['eventdate']) ? "-" : $m["eventname"] . " " . date($SETTINGS["datetime_format"], strtotime($m['eventdate'])); ?></td>
                <td><?php echo $m['serial']; ?></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-desktop hidden-sm"></i> <?php $Strings->get('ID'); ?></th>
            <th data-priority="2"><i class="fas fa-hdd hidden-sm"></i> <?php $Strings->get('OS/Software'); ?></th>
            <th data-priority="3"><i class="fas fa-hashtag hidden-sm"></i> <?php $Strings->get('Model'); ?></th>
            <th data-priority="3"><i class="fas fa-calendar hidden-sm"></i> <?php $Strings->get('Last Event'); ?></th>
            <th data-priority="1"><i class="fas fa-barcode hidden-sm"></i> <?php $Strings->get('Serial'); ?></th>
        </tr>
    </tfoot>
</table>