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

$machine = new Machine($machineid);
?>
<div class="card">
    <h3 class="card-header d-flex">
        <div>
            <i class="fas fa-desktop"></i> <?php $Strings->get("Machine"); ?> #<?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>
                <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>
            </div>
        <?php } ?>
    </h3>
    <div class="card-body">
        <div class="row">
            <div class="col-sm-6 mb-3">
                <h6>Machine Info:</h6>
                <div class="list-group">
                    <div class="list-group-item">
                        <b><?php $Strings->get("OS/Software"); ?></b>: <?php echo htmlspecialchars($machine->getOS()); ?>
                    </div>
                    <div class="list-group-item">
                        <b><?php $Strings->get("Serial"); ?></b>: <?php echo htmlspecialchars($machine->getSerial()); ?>
                    </div>
                    <div class="list-group-item">
                        <b><?php $Strings->get("Manufacturer"); ?></b>: <?php echo htmlspecialchars($machine->getManufacturer()); ?>
                    </div>
                    <div class="list-group-item">
                        <b><?php $Strings->get("Model"); ?></b>: <?php echo htmlspecialchars($machine->getModel()); ?>
                    </div>
                    <div class="list-group-item">
                        <b><?php $Strings->get("Condition"); ?></b>: <?php
                        $val = $machine->getCondition();
                        $filled = floor($val);
                        $empty = 10;
                        while ($filled > 0) {
                            $filled--;
                            $empty--;
                            echo "<i class=\"fas fa-star\"></i> ";
                        }
                        if ($val - floor($val) > 0.75) {
                            $empty--;
                            echo "<i class=\"fas fa-star\"></i> ";
                        } else if ($val - floor($val) > 0.25) {
                            $empty--;
                            echo "<i class=\"fas fa-star-half-alt\"></i> ";
                        }
                        while ($empty > 0) {
                            $empty--;
                            echo "<i class=\"far fa-star\"></i> ";
                        }
                        echo " ($val/10)";
                        ?>
                    </div>
                    <div class="list-group-item">
                        <b><?php $Strings->get("Price"); ?></b>: $<?php echo number_format($machine->getPrice(), 2); ?>
                    </div>
                </div>

                <?php
                $history = $machine->getEvents();

                if (count($history) > 0) {
                    ?>
                    <div class="mt-4">
                        <h6>Events:</h6>
                        <div class="list-group">
                            <?php
                            foreach ($history as $h) {
                                echo "<div class=\"list-group-item\">\n";
                                echo "<b>$h[eventname]</b> on " . date($SETTINGS["datetime_format"], strtotime($h['date'])) . "<br />\n";
                                echo "<b>Technician:</b> " . htmlspecialchars((new User($h['techuid']))->getName()) . "<br />\n";
                                if (!empty($h['publicnotes'])) {
                                    echo "<div><b>Public Notes:</b><br /><div class=\"ml-3\">" . htmlspecialchars($h['publicnotes']) . "</div></div>";
                                }
                                if (!empty($h['privatenotes'])) {
                                    echo "<div><b>Private Notes:</b><br /><div class=\"ml-3\">" . htmlspecialchars($h['privatenotes']) . "</div></div>";
                                }
                                echo "\n</div>\n";
                            }
                            ?>
                        </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">
                                <div class="float-right">
                                    <a class="btn btn-primary btn-sm" href="./app.php?page=editcomponent&id=<?php echo $c->getID(); ?>"><i class="fas fa-edit"></i> <?php $Strings->get("Edit"); ?></a>
                                </div>
                                <b><?php echo $c->getTypeName(); ?></b><br />
                                <?php
                                if (!empty($c->getModel())) {
                                    ?>
                                    <b><?php $Strings->get("Model"); ?></b>: <?php echo htmlspecialchars($c->getModel()); ?><br />
                                    <?php
                                }

                                if (!empty($c->getCapacity())) {
                                    ?>
                                    <b><?php $Strings->get("Capacity"); ?></b>:  <?php echo htmlspecialchars($c->getCapacity()); ?><br />
                                    <?php
                                }

                                if (!empty($c->getSerial())) {
                                    ?>
                                    <b><?php $Strings->get("Serial"); ?></b>:  <?php echo htmlspecialchars($c->getSerial()); ?><br />
                                    <?php
                                }

                                if (!empty($c->getTestedDate())) {
                                    ?>
                                    <b><?php $Strings->get("Tested On"); ?></b>:  <?php echo date($SETTINGS["datetime_format"], strtotime($c->getTestedDate())); ?><br />
                                    <?php
                                }

                                if (!empty($c->getPrice())) {
                                    ?>
                                    <b><?php $Strings->get("Price"); ?></b>: $<?php echo number_format($c->getPrice(), 2); ?>
                                    <?php
                                }

                                if (!empty($c->getPublicNotes())) {
                                    ?>
                                    <div>
                                        <b>Public Notes:</b><br />
                                        <div class="ml-3"><?php echo htmlspecialchars($c->getPublicNotes()); ?></div>
                                    </div>
                                    <?php
                                }

                                if (!empty($c->getPrivateNotes())) {
                                    ?>
                                    <div>
                                        <b>Private Notes:</b><br />
                                        <div class="ml-3"><?php echo htmlspecialchars($c->getPrivateNotes()); ?></div>
                                    </div>
                                <?php } ?>
                            </div>
                            <?php
                        }
                        ?>
                    </div>
                </div>
                <?php
            }
            ?>
        </div>
    </div>
</div>