<?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/.
 */

require_once __DIR__ . "/../lib/required_public.php";
?>
<!DOCTYPE html>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Machine Lookup</title>

<link href="../static/css/bootstrap.min.css" rel="stylesheet">
<link href="../static/css/material-color/material-color.min.css" rel="stylesheet">
<link href="../static/css/fontawesome-all.min.css" rel="stylesheet">

<?php
// Allow setting the page background to match the style of a parent frame
if (isset($_GET["backgroundcolor"]) && !empty($_GET["backgroundcolor"]) && preg_match("/^[a-z0-9#\-]+$/i", $_GET["backgroundcolor"])) {
    ?>
    <style nonce="<?php echo $SECURE_NONCE; ?>">
        html,body {
            background-color: <?php echo $_GET["backgroundcolor"]; ?>;
        }
    </style>
    <?php
}
?>

<div class="container mt-4">
    <div class="row justify-content-center">
        <div class="<?php if (!isset($_GET["embed"])) { ?>col-12 col-md-8<?php } else { ?>col-12<?php } ?>">
            <?php
            if (empty($_GET["id"]) || (!Machine::exists($_GET["id"]) && !Machine::serialExists($_GET["id"]))) {
                ?>
                <div class="card">
                    <h3 class="card-header d-flex">
                        <div>
                            <i class="fas fa-desktop"></i> <?php $Strings->get("Machine Info"); ?>
                        </div>
                    </h3>
                    <div class="card-body">
                        <?php
                        if (!empty($_GET["id"])) {
                            ?>
                            <p class="text-danger">No machine with ID <code><?php echo htmlspecialchars($_GET['id']); ?></code> could be found.</p>
                            <?php
                        }
                        ?>
                        <?php if (!isset($_GET["embed"])) { ?>
                            <p>
                                Enter a Netsyms machine ID number or serial number.
                            </p>
                            <form method="GET">
                                <input type="text" name="id" class="form-control" placeholder="Machine ID Number" required />
                                <button type="submit" class="btn btn-primary btn-block mt-2">Get Info</button>
                            </form>
                        <?php } ?>
                    </div>
                </div>
                <?php
            } else {
                if (Machine::exists($_GET["id"])) {
                    $machine = new Machine($_GET['id']);
                } else {
                    $machine = new Machine(Machine::getIDFromSerial($_GET['id']));
                }
                ?>
                <div class="card">
                    <h3 class="card-header d-flex">
                        <div>
                            <i class="fas fa-desktop"></i> <?php $Strings->get("Machine Info"); ?>
                        </div>
                    </h3>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-12 mb-3">
                                <div class="list-group">
                                    <div class="list-group-item">
                                        <b><?php $Strings->get("ID"); ?></b>: <?php echo htmlspecialchars($machine->getID()); ?>
                                    </div>
                                    <?php if (!empty($machine->getOS())) { ?>
                                        <div class="list-group-item">
                                            <b><?php $Strings->get("OS/Software"); ?></b>: <?php echo htmlspecialchars($machine->getOS()); ?>
                                        </div>
                                    <?php } ?>
                                    <?php if (!empty($machine->getSerial())) { ?>
                                        <div class="list-group-item">
                                            <b><?php $Strings->get("Serial"); ?></b>: <?php echo htmlspecialchars($machine->getSerial()); ?>
                                        </div>
                                    <?php } ?>
                                    <?php if (!empty($machine->getManufacturer())) { ?>
                                        <div class="list-group-item">
                                            <b><?php $Strings->get("Manufacturer"); ?></b>: <?php echo htmlspecialchars($machine->getManufacturer()); ?>
                                        </div>
                                    <?php } ?>
                                    <?php if (!empty($machine->getModel())) { ?>
                                        <div class="list-group-item">
                                            <b><?php $Strings->get("Model"); ?></b>: <?php echo htmlspecialchars($machine->getModel()); ?>
                                        </div>
                                    <?php } ?>
                                    <?php
                                    if ($machine->getCondition() > 0) {
                                        ?>
                                        <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>
                                        <?php
                                    }
                                    ?>
                                    <?php
                                    if ($machine->getPrice() > 0) {
                                        ?>
                                        <div class="list-group-item">
                                            <b><?php $Strings->get("Price"); ?></b>: $<?php echo number_format($machine->getPrice(), 2); ?>
                                        </div>
                                        <?php
                                    }
                                    ?>
                                    <?php
                                    if (!empty($machine->getPublicNotes())) {
                                        ?>
                                        <div class="list-group-item">
                                            <b><?php $Strings->get("Notes"); ?></b>: <?php echo htmlspecialchars($machine->getPublicNotes()); ?>
                                        </div>
                                        <?php
                                    }
                                    ?>
                                </div>
                            </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">

                                                <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>Notes:</b><br />
                                                        <div class="ml-3"><?php echo htmlspecialchars($c->getPublicNotes()); ?></div>
                                                    </div>
                                                <?php } ?>
                                            </div>
                                        <?php } ?>
                                    </div>
                                </div>
                                <?php
                            }


                            $history = $machine->getEvents();

                            if (count($history) > 0) {
                                ?>
                                <div class="col-sm-6 mb-3">
                                    <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";
                                            if (!empty($h['publicnotes'])) {
                                                echo "<div><b>Notes:</b><br /><div class=\"ml-3\">" . htmlspecialchars($h['publicnotes']) . "</div></div>";
                                            }
                                            echo "\n</div>\n";
                                        }
                                        ?>
                                    </div>
                                </div>
                                <?php
                            }
                            ?>
                        </div>
                    </div>
                    <?php /* Hide this box if we're embedded in another page */ if (!isset($_GET["embed"])) { ?>
                        <div class="card-body">
                            <p>
                                Look up another machine:
                            </p>
                            <form method="GET">
                                <input type="text" name="id" class="form-control" placeholder="Machine ID Number" required />
                                <button type="submit" class="btn btn-primary btn-block mt-2">Get Info</button>
                            </form>
                        </div>
                    <?php } ?>
                </div>
                <?php
            }
            ?>
        </div>
    </div>
</div>

<?php
// Send message to parent frame with our height so they can adjust the iframe
if (isset($_GET["embed"])) {
    ?>
    <script nonce="<?php echo $SECURE_NONCE; ?>">
        window.addEventListener("load", function () {
            parent.window.postMessage(document.documentElement.scrollHeight + "px", "*");
        });
    </script>
    <?php
}
?>