<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
    }
// Check existence of id parameter before processing further
if(isset($_GET["sysID"]) && !empty(trim($_GET["sysID"]))){
    // Include config file
    require_once "../assets/php/config.php";
    require_once "../assets/php/cuttext.php";
    $idGET = $_GET["sysID"];
    // Prepare a select statement
    $sql = "SELECT * FROM `systems` WHERE `sysID` = '$idGET'";
    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "i", $param_id);
        
        // Set parameters
        $param_id = trim($_GET["sysID"]);
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);
    
            if(mysqli_num_rows($result) == 1){
                /* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
                $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                
                // Retrieve individual field value
                $sysID = $row["sysID"];
            } else{
                // URL doesn't contain valid id parameter. Redirect to error page
                header("location: error.php?error=sdne");
                exit();
            }
            
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
    $string = $row["sysMeta"];
    $trimmedString = trim($string);
    $lines = explode("\n", $trimmedString);
    // Close connection
} else{
    // URL doesn't contain id parameter. Redirect to error page
    header("location: error.php?error=sdne");
    exit();
}
?>
<!DOCTYPE html>
<html lang="en-us">

<head>
    <title>SYSDesk - System #<?php echo $row["sysID"]; ?></title>
    <meta charset="UTF-8">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" type="image/x-icon" href="../assets/favicon.ico" />
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,100,100italic,300,300italic,400italic,500,500italic,700italic,700,900,900italic" rel="stylesheet">
    <!-- STYLESHEETS -->
    <style type="text/css">
        [fuse-cloak],
        .fuse-cloak {
            display: none !important;
        }

    </style>
    <!-- Icons.css -->
    <link type="text/css" rel="stylesheet" href="../assets/icons/fuse-icon-font/style.css">
    <!-- Animate.css -->
    <link type="text/css" rel="stylesheet" href="../assets/node_modules/animate.css/animate.min.css">




    <!-- Perfect Scrollbar -->
    <link type="text/css" rel="stylesheet" href="../assets/node_modules/perfect-scrollbar/css/perfect-scrollbar.css" />
    <!-- Fuse Html -->
    <link type="text/css" rel="stylesheet" href="../assets/fuse-html/fuse-html.min.css" />
    <!-- Main CSS -->
    <link type="text/css" rel="stylesheet" href="../assets/css/main.css">
    <!-- Custom CSS -->
    <link type="text/css" rel="stylesheet" href="../assets/css/custom.css">
    <!-- / STYLESHEETS -->

    <!-- JAVASCRIPT -->
    <!-- jQuery -->
    <script type="text/javascript" src="../assets/node_modules/jquery/dist/jquery.min.js"></script>
    <!-- Mobile Detect -->
    <script type="text/javascript" src="../assets/node_modules/mobile-detect/mobile-detect.min.js"></script>
    <!-- Perfect Scrollbar -->
    <script type="text/javascript" src="../assets/node_modules/perfect-scrollbar/dist/perfect-scrollbar.min.js">
    </script>
    <!-- Popper.js -->
    <script type="text/javascript" src="../assets/node_modules/popper.js/dist/umd/popper.min.js"></script>
    <!-- Bootstrap -->
    <script type="text/javascript" src="../assets/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>



    <!-- Data tables -->
    <script type="text/javascript" src="../assets/node_modules/datatables.net/js/jquery.dataTables.js"></script>
    <script type="text/javascript" src="../assets/node_modules/datatables-responsive/js/dataTables.responsive.js">
    </script>

    








    <!-- Fuse Html -->
    <script type="text/javascript" src="../assets/fuse-html/fuse-html.min.js"></script>
    <!-- Main JS -->
    <script type="text/javascript" src="../assets/js/main.js"></script>
    <!-- / JAVASCRIPT -->
</head>

<body class="layout layout-vertical layout-left-navigation layout-below-toolbar layout-below-footer">
    <main>
        <div id="wrapper">
            <aside id="aside" class="aside aside-left" data-fuse-bar="aside" data-fuse-bar-media-step="md" data-fuse-bar-position="left">
                <div class="aside-content bg-primary-700 text-auto">
                    <div class="aside-toolbar">
                        <div class="logo">
                            <span class="logo-icon icon-desktop-mac"></span>
                            <p class="logo-text texttoolbar">SYSDesk</p>
                        </div>
                        <button id="toggle-fold-aside-button" type="button" class="btn btn-icon d-none d-lg-block" data-fuse-aside-toggle-fold>
                            <i class="icon icon-backburger"></i>
                        </button>
                    </div>
                    <ul class="nav flex-column custom-scrollbar" id="sidenav" data-children=".nav-item">
                        <li class="nav-item">
                            <a class="nav-link ripple " href="systems.php" data-url="index.html">
                                <i class="icon s-4 icon-desktop-tower"></i>
                                <span>Systems</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link ripple" href="tickets.php">
                                <i class="icon s-4 icon-message-bulleted"></i>
                                <span>Tickets</span>
                            </a>
                        </li>

                        <li class="nav-item" role="tab" id="heading-ecommerce">

                            <a class="nav-link ripple with-arrow collapsed" data-toggle="collapse" data-target="#collapse-ecommerce" href="#" aria-expanded="false" aria-controls="collapse-ecommerce">

                                <i class="icon s-4 icon-settings"></i>

                                <span>Settings</span>
                            </a>
                            <ul id="collapse-ecommerce" class='collapse ' role="tabpanel" aria-labelledby="heading-ecommerce" data-children=".nav-item">

                                <li class="nav-item">
                                    <a class="nav-link ripple " href="users.php" data-url="index.html">

                                        <span>Users</span>
                                    </a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link ripple " href="reset-password.php">

                                        <span>Reset Password</span>
                                    </a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link ripple " href="other-settings.php" data-url="index.html">

                                        <span>Other settings</span>
                                    </a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </div>
            </aside>
            <div class="content-wrapper">
                <nav id="toolbar" class="bg-white">
                    <div class="row no-gutters align-items-center flex-nowrap">
                        <div class="col">
                            <div class="row no-gutters align-items-center flex-nowrap">
                                <button type="button" class="toggle-aside-button btn btn-icon d-block d-lg-none fuse-ripple-ready" data-fuse-bar-toggle="aside">
                                    <i class="icon icon-menu"></i>
                                </button>
                                <div class="toolbar-separator d-block d-lg-none"></div>


                            </div>

                        </div>
                        <div class="col-auto">
                            <div class="row no-gutters align-items-center justify-content-end">
							<a href="logout.php" class="logintoolbar ripple icon row align-items-center no-gutters px-2 px-sm-4" role="button" id="logintoolbar" aria-haspopup="true" aria-expanded="false">Logout</a>
                            </div>
                        </div>
                    </div>
                </nav>
                    <div class="content custom-scrollbar">
                        <div class="page-layout simple full-width">
                            <div class="page-content-wrapper custom-scrollbar">


                                <!-- CONTENT -->
                                <div class="clearfix" style="background:white; padding:20px;">
                                    <div class="toolbar crud pull-left">
                                        <button class="headbnt btn btn-secondary btn-fab icon icon-arrow-left" onclick="window.history.back();"></button>
                                        <?php

       echo "<a class=' btn btn-warning btn-fab'  href='edit-system.php?id=". $row['id'] ."'><span class='icons icon-pencil'></span></a>";
              echo "<span> </span>"; 
       echo "<a class='btn btn-danger btn-fab' href='delete-system.php?id=". $row['id'] ."'><span class='icons icon-trash'></span></a>";
       ?>
                                    </div>
                                    <h2 class="pull-right">System ID: <?php echo $row["sysID"]; ?></h2>
                                </div>
                                <div class="form-wrapper md-elevation-8 p-4">
                                    <div class="pagebackend">
                                        <div class="container-fluid">
                                            <div class="row">
                                                <div class="col-md-12">
                                                    <nav>
                                                        <div class="nav nav-tabs" id="nav-tab" role="tablist">
                                                            <a class="backend nav-item nav-link active" id="nav-sysinfo-tab" data-toggle="tab" href="#nav-sysinfo" role="tab" aria-controls="nav-sysinfo" aria-selected="true">System
                                                                information</a>
                                                            <a class="backend nav-item nav-link" id="nav-tickets-tab" data-toggle="tab" href="#nav-tickets" role="tab" aria-controls="nav-tickets" aria-selected="false">Tickets</a>
                                                        </div>
                                                    </nav>
                                                    <div class="tabbackend tab-content" id="nav-tabContent">
                                                        <div class="tab-pane fade show active" id="nav-sysinfo" role="tabpanel" aria-labelledby="nav-sysinfo-tab">
                                                            <div class="viewer form-group">
                                                                <label class="crud">System Type</label>
                                                                <p class="form-control-static">
                                                                    <?php echo $row["sysType"]; ?></p>
                                                            </div>
                                                            <div class="viewer form-group">
                                                                <label class="crud">Service tag / Serial Number</label>
                                                                <p class="form-control-static">
                                                                    <?php echo $row["ServiceTag"]; ?></p>
                                                            </div>
                                                            <div class="viewer form-group">
                                                                <label class="crud">Location</label>
                                                                <p class="form-control-static">
                                                                    <?php echo $row["location"]; ?></p>
                                                            </div>
                                                            <div class="viewer form-group">
                                                                <label class="crud">Mac addres</label>
                                                                <p class="form-control-static">
                                                                    <?php echo $row["EthMac"]; ?></p>
                                                            </div>
                                                            <div class="viewer form-group">
                                                                <label class="crud">Windows License</label>
                                                                <p class="form-control-static">
                                                                    <?php echo $row["WinKey"]; ?></p>
                                                            </div>
                                                        <div class="meta col-md-4 viewer form-group">    
                                                        <table class="table">
                                                          <tr>
                                                            <thead>
                                                            <th class="meta">Meta Tag</th>
                                                            <th class="meta">Value</th>
                                                          </tr>
                                                          </thead>
                                                          <?php
                                                          foreach($lines as $line) {
                                                            echo "<tr>";
                                                            $elements = explode("*", $line);
                                                            foreach($elements as $element) {
                                                              echo "<td>" . $element . "</td>";
                                                            }
                                                            echo "</tr>";
                                                          }
                                                          
                                                          ?>
                                                          </table>
                                                          </div>
                                                        </div>
                                                        <div class="tab-pane fade" id="nav-tickets" role="tabpanel" aria-labelledby="nav-tickets-tab">

                                                            <div id="accordion" role="tablist">
                                                                <div class="card">
                                                                    <div class="card-header" role="tab" id="headingOne">
                                                                        <center>
                                                                            <h5 class="mb-0">
                                                                                <a data-toggle="collapse" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                                                                                    Open tickets
                                                                                </a>
                                                                            </h5>
                                                                        </center>
                                                                    </div>

                                                                    <div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingOne" data-parent="#accordion">
                                                                        <div class="card-block custom-scrollbar">
                                                                            <?php
                    // Attempt select query execution
                    $sql1 = "SELECT * FROM tickets WHERE `sysID` = '$idGET'";
                    if($result1 = mysqli_query($link, $sql1)){
                        if(mysqli_num_rows($result1) > 0){
                            echo "<table class='table table-bordered table-striped'>";
                                echo "<thead>";
                                    echo "<tr>";
                                        echo "<th>Ticket ID</th>";
                                        echo "<th>Location</th>";
                                        echo "<th>Request Type</th>";
                                        echo "<th>Request by</th>";
                                        echo "<th>Reason</th>";
                                        echo "<th>Date/Time</th>";
                                    echo "</tr>";
                                echo "</thead>";
                                echo "<tbody>";
                                while($row1 = mysqli_fetch_array($result1)){
                                    echo "<tr>";
                                        echo "<td><a class='sysIDticket' href='view-ticket.php?id=". $row1['id'] ."' title='View ticket' data-toggle='tooltip'>" . $row1['id'] . "</a></td>";
                                        echo "<td>" . $row1['location'] . "</td>";
                                        echo "<td>" . $row1['requestype'] . "</td>";
                                        echo "<td>" . $row1['rusername'] . "</td>";
                                        echo "<td>" . cutText($row1['reason'], 50)."</td>";
                                        echo "<td>" . $row1['time'] . "</td>";
                                        echo "<td>";
                                            echo "<a class='crud' href='view-ticket.php?id=". $row1['id'] ."' title='View ticket' data-toggle='tooltip'><span class='icons icon-eye'></span></a>";
                                            echo "<a class='crud' href='archive-ticket.php?id=". $row1['id'] ."' title='Archive ticket' data-toggle='tooltip'><span class='icons icon-archive'></span></a>";
                                        echo "</td>";
                                       echo "</tr>";
                                }
                                echo "</tbody>";                            
                                echo "</table>";
                            // Free result set
                            mysqli_free_result($result1);
                        } else{
                            echo "<p class='lead'><em>No tickets were found.</em></p>";
                        }
                    } else{
                        echo "ERROR: Could not able to execute $sql1. " . mysqli_error($link);
                    }
 
                    ?>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                                <div class="card">
                                                                    <div class="card-header" role="tab" id="headingTwo">
                                                                        <center>
                                                                            <h5 class="mb-0">
                                                                                <a class="collapsed" data-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                                                                                    Archived tickets
                                                                                </a>
                                                                            </h5>
                                                                        </center>
                                                                    </div>
                                                                    <div id="collapseTwo" class="collapse" role="tabpanel" aria-labelledby="headingTwo" data-parent="#accordion">
                                                                        <div class="card-block custom-scrollbar">
                                                                            <?php
                    // Attempt select query execution
                    $sql2 = "SELECT * FROM tickets_arch WHERE `sysID` = '$idGET'";
                    if($result2 = mysqli_query($link, $sql2)){
                        if(mysqli_num_rows($result2) > 0){
                            echo "<table class='table table-bordered table-striped'>";
                                echo "<thead>";
                                    echo "<tr>";                                       
                                        echo "<th>Ticket ID</th>";
                                        echo "<th>Location</th>";
                                        echo "<th>Request Type</th>";
                                        echo "<th>Request by</th>";
                                        echo "<th>Reason</th>";
                                        echo "<th>Date/Time</th>";
                                    echo "</tr>";
                                echo "</thead>";
                                echo "<tbody>";
                                while($row2 = mysqli_fetch_array($result2)){
                                    echo "<tr>";
                                        echo "<td><a class='sysIDticket' href='view-achivedticket.php?id=". $row2['id'] ."' title='View ticket' data-toggle='tooltip'>" . $row2['id'] . "</a></td>";
                                        echo "<td>" . $row2['location'] . "</td>";
                                        echo "<td>" . $row2['requestype'] . "</td>";
                                        echo "<td>" . $row2['rusername'] . "</td>";
                                        echo "<td>" . cutText($row2['reason'], 50)."</td>";
                                        echo "<td>" . $row2['time'] . "</td>";
                                        echo "<td>";
                                            echo "<a class='crud' href='view-achivedticket.php?id=". $row2['id'] ."' title='View ticket' data-toggle='tooltip'><span class='icons icon-eye'></span></a>";
                                        echo "</td>";
                                       echo "</tr>";
                                }
                                echo "</tbody>";                            
                                echo "</table>";
                            // Free result set
                            mysqli_free_result($result2);
                        } else{
                            echo "<p class='lead'><em>No tickets were found.</em></p>";
                        }
                    } else{
                        echo "ERROR: Could not able to execute $sql2. " . mysqli_error($link);
                    }
 
                    // Close connection
                    mysqli_close($link);
                    ?>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </div>

                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- / CONTENT -->
                                </div>
                            </div>
                        </div>
                    </div>
            </div>
    </main>
</body>
</html>