<?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();
}

if (!empty($_GET["id"])) {
    $machineid = $_GET["id"];
} else if (!empty($_GET["arg"])) {
    $machineid = $_GET["arg"];
}
if (!Machine::exists($machineid)) {
    header("Location: ./app.php?msg=no_such_machine");
}

$machine = new Machine($machineid);
?>
<div class="card">
    <h3 class="card-header d-flex">
        <div>
            <i class="<?php echo $machine->getIcon(); ?>"></i> <?php echo $machine->getTypeLabel(); ?> #<?php echo htmlspecialchars($machine->getID()); ?>
        </div>
        <?php if ($user->hasPermission("MACHINEMANAGER_EDIT")) { ?>
            <div class="ml-auto">
                <a href="./app.php?page=editmachine&arg=<?php echo htmlspecialchars($machine->getID()); ?>" class="btn btn-primary btn-sm"><i class="fas fa-edit"></i> <?php $Strings->get("Edit"); ?></a>
                <a href="./app.php?page=addevent&id=<?php echo htmlspecialchars($machine->getID()); ?>" class="btn btn-success btn-sm"><i class="fas fa-history"></i> <?php $Strings->get("Add Event"); ?></a>
                <div class="btn-group">
                    <a href="./app.php?page=editcomponent&machine=<?php echo htmlspecialchars($machine->getID()); ?>" class="btn btn-success btn-sm"><i class="fas fa-memory"></i> <?php $Strings->get("Add Component"); ?></a>
                    <button type="button" class="btn btn-success btn-sm dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <span class="sr-only">Toggle Dropdown</span>
                    </button>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="./app.php?page=components&attachto=<?php echo htmlspecialchars($machine->getID()); ?>"><?php $Strings->get("Attach Existing"); ?></a>
                    </div>
                </div>
                <a href="./app.php?page=printlabel&id=<?php echo htmlspecialchars($machine->getID()); ?>" class="btn btn-info btn-sm"><i class="fas fa-print"></i> <?php $Strings->get("Print Labels"); ?></a>
                <a href="./app.php?page=machines" class="btn btn-danger btn-sm"><i class="fas fa-arrow-left"></i> <?php $Strings->get("Back"); ?></a>
            </div>
        <?php } ?>
    </h3>
    <div class="card-body">
        <div class="row">
            <div class="col-sm-6 mb-3">
                <h6>Device Info:</h6>
                <?php
                echo $machine->toHTMLListGroup(false);

                $history = $machine->getEvents();

                if (count($history) > 0) {
                    ?>
                    <div class="mt-4">
                        <h6>Events:</h6>
                        <div class="list-group">
                            <?php
                            foreach ($history as $h) {
                                ?>
                                <div class="list-group-item">
                                    <?php echo $h->toHTML(false); ?>
                                </div>
                                <?php
                            }
                            ?>
                        </div>
                    </div>
                    <?php
                }
                ?>
            </div>

            <?php
            $components = $machine->getComponents();

            if (count($components) > 0) {
                ?>
                <div class="col-sm-6 mb-3">
                    <h6>Components:</h6>
                    <div class="list-group">
                        <?php
                        foreach ($components as $c) {
                            ?>
                            <div class="list-group-item">
                                <?php echo $c->toHTML(false); ?>
                            </div>
                            <?php
                        }
                        ?>
                    </div>
                </div>
                <?php
            }
            ?>
        </div>
    </div>
</div>