<!DOCTYPE html>
<html>
<head>
    <title>Admin interface</title>
    <link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<div class="container">
    <div class="centerH">
        <nav>
            <a href="./">Back</a>
        </nav>
        <div>
            <?php
            require_once '../include/DbConnect.php';

            // opening db connection
            $db = new DbConnect();
            $connection = $db->connect();
            $prefix = "";
            $prefixFile = fopen("../databasePrefix.txt", "r") or die("Cannot find databasePrefix.txt in root directory!");
            if ($prefixFile == null)
                return false;
            if (!filesize("../databasePrefix.txt") == 0)
                $prefix = fread($prefixFile, filesize("../databasePrefix.txt"));
            fclose($prefixFile);

            if(isset($_GET["id"]))
            {
                $id = $_GET["id"];

                $data = mysqli_query($connection, 'SELECT Location, DateAdded FROM '.$prefix.'locations WHERE UserId = '. $id);

                echo '<table cellpadding="0" cellspacing="10px" class="db-table">';
                echo '<tr>';

                for($i = 0; $i < mysqli_num_fields($data); $i++) {
                    $field_info = mysqli_fetch_field($data);
                    echo "<th>{$field_info->name}</th>";
                }

                echo '</tr>';
                if(mysqli_num_rows($data))
                    while($row2 = mysqli_fetch_row($data)) {
                        echo '<tr>';
                        foreach($row2 as $key=>$value) {
                            if($key == 0)
                            {
                                $lat = substr($value, 17, 9);
                                $lat = str_replace(",",".",$lat);
                                $long = substr($value, 27, 9);
                                $long = str_replace(",",".",$long);
                                echo '<td><a href="https://www.google.com/maps/@'.$lat.','.$long.',15z">',$value,'</a></td>';
                            }
                            else
                                echo '<td>',$value,'</td>';
                        }
                        echo '</tr>';
                    }
                echo '</table><br/>';

                $data = mysqli_query($connection, 'SELECT FromWho, Message, DateAdded FROM '.$prefix.'messages WHERE UserId = '. $id);
                writeTable($data);

                $dir = "../logCalls/";
                $callFiles = array();
                // Open a directory, and read its contents
                if (is_dir($dir)){
                    if ($dh = opendir($dir)){
                        while (($file = readdir($dh)) !== false){
                            if($file != "." && $file != ".." && $file != "logCall.php")
                                array_push($callFiles, $file);
                        }
                        closedir($dh);
                    }
                }

                $data = mysqli_query($connection, 'SELECT Username FROM '.$prefix.'users WHERE Id = '. $id);
                $username = mysqli_fetch_row($data)[0];

                foreach($callFiles as $callFile)
                    if(strpos($callFile, $username) === 0)
                    {
                        echo '<table cellpadding="0" cellspacing="10px" class="db-table">';
                        echo '<tr>';
                        echo '<th>Call logged</th>';
                        echo '</tr>';
                        echo '<td><a href="../logCalls/'.$callFile.'">',$callFile,'</a></td>';
                        echo '</table><br/>';
                    }
            }

            function writeTable($data)
            {
                echo '<table cellpadding="0" cellspacing="10px" class="db-table">';
                echo '<tr>';

                for($i = 0; $i < mysqli_num_fields($data); $i++) {
                    $field_info = mysqli_fetch_field($data);
                    echo "<th>{$field_info->name}</th>";
                }

                echo '</tr>';
                if(mysqli_num_rows($data))
                    while($row2 = mysqli_fetch_row($data)) {
                        echo '<tr>';
                        foreach($row2 as $key=>$value) {
                            echo '<td>',$value,'</td>';
                        }
                        echo '</tr>';
                    }
                echo '</table><br/>';
            }
            ?>
        </div>
    </div>
</div>
</body>
</html>