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

$page = "statsID";

$data['from'] = isset($_GET['from']) && $_GET['from'] != "" ? strtotime($_GET['from']): strtotime("14 days ago");
$data['to'] = isset($_GET['to']) && $_GET['to'] != "" ? strtotime($_GET['to']): strtotime(date('Y-m-d H:i:s'));

$data['last'] = array(
    'from' => $data['from'],
    'to' => $data['to']
);

date_default_timezone_set('Asia/Jakarta');

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

require 'autoload.php';
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['id']) && $_GET['id'] != "") {
        echo "<div class=\"container pt-3\" style='background-color: #ffffff; '>
        <div class='row'>
        <div class='col-12 col-md-6'>
        <h3>ID <strong>{$_GET['id']}</strong> Performance
        </div>
        <div class='col-12 col-md-6 float-right'>
            <form action='' method='get'>
                <div class=\"input-group input-daterange\">
                    <input name='from' type=\"text\" class=\"form-control\" autocomplete=\"off\" data-date-format=\"yyyy-mm-dd\">
                    <input name='to' type=\"text\" class=\"form-control\" autocomplete=\"off\" data-date-format=\"yyyy-mm-dd\">
                    <div class='input-group-append'>
                        <button class='btn btn-success' type='submit'>Go</button>
                    </div>
                </div>
                <input type='hidden' name='id' value='{$_GET['id']}'>
            </form>
        </div>
        </h3>
        
        
        </div>
        <div class='clearfix'></div>
        <hr /> 
        <div style=\"width: 100%; height: 200px;\">
        <canvas id=\"myChart\" role=\"img\" /></canvas>
        </div>
        <h4>Lead</h4>
        <div class='table-responsive'>
        <table id='myTable' class='table table-striped'><thead>
        <tr>
            <th>Date</th>
            <th>ID</th>
            <th>Network</th>
            <th>Traffic</th>
            <th>Country</th>
            <th>Amount</th>
        </tr>
        </thead>
        <tbody>
        ";
        $data['itemfrom'] = isset($_GET['from']) && $_GET['from'] != "" ? strtotime($_GET['from']): "";
        $data['itemto'] = isset($_GET['to']) && $_GET['to'] != "" ? strtotime($_GET['to']): "";
        $where = '';
        if ($data['itemfrom'] != "") {
            $where .= "AND `date` >= '".date('Y-m-d', $data['from'])." 00:00:00' ";
        } else {
            $where .= "AND `date` >= '".date('Y-m-d', strtotime('today'))." 00:00:00' ";
        }
        if ($data['itemto'] != "") {
            $where .= "AND `date` <= '" . date('Y-m-d', $data['to']) . " 23:59:59' ";
        } else {
            $where .= "AND `date` <= '".date('Y-m-d', strtotime('today'))." 23:59:59' ";
        }
        $sql = "SELECT * FROM `conversions` WHERE `subid` = '{$_GET['id']}' {$where} ORDER BY `id` DESC";
        $q = Db::result($sql);
        // print_r($q);
        if( !isset($q['error']) ) {
            foreach($q as $k => $v) {
                $country = strtolower($v->country);
                $ctr = strtolower($country);
                if($v->os == "web" || $v->os == "desktop" || $v->os == "Windows" ) {
                    $os = "<img src='image/icon-laptop.png' style='height: 20px;'>";
                }
                if($v->os == "WAP" || $v->os == "android" || $v->os == "Android" || $v->os == "ios" || $v->os == "iOS") {
                    $os = "<img src='image/icon-cellphone.png' style='height: 20px;'>";
                }
                if($v->os == "tablet") {
                    $os = "<img src='image/icon-tablet.png' style='height: 20px;'>";
                } 

                if($v->network == "imonetizeit") {
                    $network = "<div class='bg-success' style='font-size: .9em; color: white; padding: 2px 10px; border-radius: 4px'>{$v->network}</div>";
                } 
                if($v->network == "adverten") {
                    $network = "<div class='bg-danger' style='font-size: .9em; color: white; padding: 2px 10px; border-radius: 4px'>{$v->network}</div>";
                } 
                if($v->network == "lospollos") {
                    $network = "<div class='bg-warning' style='font-size: .9em; padding: 2px 10px; border-radius: 4px'>{$v->network}</div>";
                } 
                if($v->network == "clickdealer") {
                    $network = "<div class='bg-primary' style='font-size: .9em; color: white; padding: 2px 10px; border-radius: 4px'>{$v->network}</div>";
                } 
                if($v->network == "datify") {
                    $network = "<div class='' style='background-color: magenta; color: white; font-size: .9em; color: white; padding: 2px 10px; border-radius: 4px'>{$v->network}</div>";
                } 
                $payout = number_format($v->payout, 2);
            echo "<tr>
                <td>{$v->date}</td>
                <td>{$v->subid}</td>
                <td class='text-center'>{$network}</td>
                <td class='text-center'>{$os}</td>
                <td class='text-center'><span class=\"flag-icon flag-icon-{$ctr}\"></span> {$country}</td>
                <td class='text-center'>{$payout}</td>
                </tr>
            ";
            }
        }
        
        echo "
        <tfoot>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </tfoot>
        </tbody>
        </table>
        </div>";
        // end container
        echo "
        </div>";
    } else {
        echo "ID tidak ditemukan.";
    }
}
include('footer.php');