<?php
include('config.php');
include('function.php');
include('countrycodes.php');



date_default_timezone_set('Asia/Jakarta');

define('GX_PATH', realpath(__DIR__.'/'));
define('GX_LIB', GX_PATH.'/lib/');

require 'autoload.php';
$page = 'index';
include('header.php');
if( isset($_POST['loginpass']) && $_POST['password'] != "" ) {
    if($_POST['password'] == $passWd) {
        $_SESSION['loggedIn'] = true;
    } else {
        echo "<div class='container'><div class='alert alert-danger'>Password Salah!!</div></div>";
    }
    
} 
if( !isset($_SESSION['loggedIn']) ) {
    echo "
    <div class='container'>
        <form action='' method='post'>
        <div class='form-group'>
            <div class='input-group'>
                <input type='password' name='password' class='form-control' placeholder='masukkan password untuk melihat report' >
                <div class='input-group-btn'>
                <button type='submit' name='loginpass' class='btn btn-success'>
                    Login
                </button>
                </div>
            </div>
        </div>
        </form>
    </div>
    ";
} else {
new Db();

if( isset($_GET['date'])) {
    if( $_GET['date'] == 'today') {
        $date = date('Y-m-d');
    } elseif( $_GET['date'] == 'yesterday' ) {
        $date = date('Y-m-d', strtotime("yesterday"));
    }
} else {
    $date = date('Y-m-d');
}
$topEarning = ""; //topEarning($date);
$r = Db::result("SELECT * FROM `hitstat` WHERE `date` LIKE '%{$date}%' ORDER BY `id` DESC ");
// print_r($r);
echo "<div class=\"container\"><div class=\"card card-default searchbox\">
<div class=\"card-header\">
    <div class=\"row\">
        <div class=\"col-md-6\">
            <input class=\"form-control input-sm\" type=\"text\" id=\"search\" placeholder=\"Search...\" autocomplete=\"off\">
        </div>
        <div class=\"col-md-6\">
            <select class=\"form-control input-sm\" name=\"select_date\" id=\"select_date\">
            <option selected=\"selected\" disabled value=\"0\">Select date</option>
            <option value=\"today\">Today</option>
            <option value=\"yesterday\">Yesterday</option>
            </select>
        </div>
        
    </div>
</div>

<div class='table-responsive'>
<table id='myTable' class='table table-sm table-striped'><thead>
<tr>
    <th>No</th>
    <th>Date</th>
    <th>IP</th>
    <th>Country</th>
    <th class='text-center'>Device</th>
</tr>
</thead>
<tbody>
";
$lastipq = Db::result("SELECT `id` FROM `hitstat` ORDER BY `id` DESC LIMIT 1  ");
$lastip_id = $lastipq[0]->id;

if (!isset($r['error'])) {
    $n = 0;
    
    foreach($r as $k => $v) {
        $n = $n+1;
        $country = strtolower($v->country); //strtolower(array_search($v->country, $countrycodes));
        if($v->device == "web" || $v->device == "desktop") {
            $os = "<img src='image/icon-laptop.png' style='height: 20px;'>";
        }
        if($v->device == "WAP" || $v->device == "android" || $v->device == "ios") {
            $device = "<img src='image/icon-cellphone.png' style='height: 20px;'>";
        }
        if($v->device == "tablet") {
            $device = "<img src='image/icon-tablet.png' style='height: 20px;'>";
        } 
        
        
        echo "<tr>
            <td width='40'>{$n}</td>
            <td>{$v->date}</td>
            <td>{$v->ip}</td>
            <td class='text-center'><span class=\"flag-icon flag-icon-{$country}\"></span> {$v->country}</td>
            <td class='text-center'>{$device}</td>
            </tr>
        ";
        // $last_id = $v->id;
    }
}

echo "

</tbody>
</table>


</div>
</div>
<div class='row'>
    <div class='traffic'>
    <ul id='listhits'>
    </ul>
    <input type='hidden' id='lastip_id' value='{$lastip_id}'>
    </div>
</div>
</div>
<div class='clearfix mb-5'></div>
";
}

include('footer.php');