<?php
include('config.php');
include('countrycodes.php');
include('header.php');
$page = "statistics";

if( isset($_GET['date']) && $_GET['date'] != "" ) {
    
    if( $_GET['date'] == "yesterday") {
        $data['from'] = strtotime("yesterday");
        $data['to'] = strtotime("yesterday");
    } if( $_GET['date'] == "today") { 
        $data['from'] = strtotime("today");
        $data['to'] = strtotime(date('Y-m-d H:i:s'));
    }
} else {
    $data['from'] = isset($_GET['from']) && $_GET['from'] != "" ? strtotime($_GET['from']): strtotime("today");
    $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';


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($_POST['uploadCSV']) ) {
        if ( isset($_FILES["fileCSV"])) {

            //if there was an error uploading the file
            if ($_FILES["fileCSV"]["error"] > 0) {
                $alertDanger = "Return Code: " . $_FILES["fileCSV"]["error"] . "<br />";

            }
            else {
                $tmpName = $_FILES['fileCSV']['tmp_name'];
                $csvAsArray = array_map('str_getcsv', file($tmpName));

//                print_r($csvAsArray);
                unset($csvAsArray[0]);
                $n = 0;
                foreach ($csvAsArray as $k => $v) {

                    $date = isset($_POST['date']) && $_POST['date'] != "" ? $_POST['date']: date('Y-m-d');
                    $name = $v[1];
                    $country = $v[2];
                    $vertical = $v[3];
                    $sub1 = $v[4];
                    $sub2 = $v[5];
                    $hits = $v[6];
                    $uniques = $v[7];
                    $clicks = $v[8];
                    $conversions = $v[9];
                    $cr = $v[10];
                    $epc = $v[11];
                    $earnings = $v[12];
                    $epm = $v[13];

                    $var = array(
                        'table' => 'link_report',
                        'key' => array(
                            'date' => $date,
                            'name' => $name,
                            'country' => $country,
                            'vertical' => $vertical,
                            'sub1' => $sub1,
                            'sub2' => $sub2,
                            'hits' => $hits,
                            'uniques' => $uniques,
                            'clicks' => $clicks,
                            'conversions' => $conversions,
                            'cr' => $cr,
                            'epc' => $epc,
                            'earnings' => $earnings,
                            'epm' => $epm,
                        )
                    );

                    Db::insert($var);
                }
            }
        } else {
            echo "No file selected <br />";
        }
    }
    echo "<div class=\"container\">
    <h3>Performance - ".date("d M Y", $data['from'])." - ".date("d M Y", $data['to'])."
    <button class='btn btn-success pull-right' data-toggle=\"modal\" data-target=\"#myModal\">
            <i class='glyphicon glyphicon-plus'></i>
    </button>
    
    <div class='col-md-6 pull-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\">
                <div class=\"input-group-addon\">to</div>
                <input name='to' type=\"text\" class=\"form-control\" autocomplete=\"off\" data-date-format=\"yyyy-mm-dd\">
                <div class='input-group-btn'>
                    <button class='btn btn-success' type='submit'>Go</button>
                </div>
            </div>
            <input type='hidden' name='id' value='{$_GET['id']}'>
        </form>
        
    </div>
    </h3>
     <hr /> 
     <div class=\"row\">
        <div class=\"col-md-6\">
            <input class=\"form-control input-sm\" type=\"text\" id=\"search\" placeholder=\"Search...\">
        </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 class='table-responsive'>
    <table id='myTable'><thead>
        <tr>
            <th>ID</th>
            <th>Country</th>
            <th>Vertical</th>
            <th>Hits</th>
            <th>Uniques</th>
            <th>Clicks</th>
            <th>Conversions</th>
            <th>CR</th>
            <th>EPC</th>
            <th>Earnings</th>
            <th>EPM</th>
        </tr>
        </thead>
        <tbody>";
        $where = '';
        if ($data['from'] != "") {
            $where .= "AND `date` >= '".date('Y-m-d', $data['from']) ."' ";
        }
        if ($data['to'] != "") {
            $where .= "AND `date` <= '" . date('Y-m-d', $data['to']) . "' ";
        }
        $name = Typo::cleanX($_GET['id']);
    // select distinct ID
$q = Db::result("SELECT * FROM `link_report` WHERE `name` = '{$name}'  {$where}");

    // print_r($q);
    foreach($q as $k => $v) {
        echo "
    <tr>
        <td>{$v->name}</td>
        <td>{$v->country}</td>
        <td>{$v->vertical}</td>
        <td>{$v->hits}</td>
        <td>{$v->uniques}</td>
        <td>{$v->clicks}</td>
        <td>{$v->conversions}</td>
        <td>{$v->cr}</td>
        <td>{$v->epc}</td>
        <td>{$v->earnings}</td>
        <td>{$v->epm}</td>
    </tr>";
    }

    echo "</tbody>
        <tfoot>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </tfoot>
        </tbody>
        </table></div>";

        echo "
        </div>
        
        <!-- Modal -->
        <div class=\"modal fade\" id=\"myModal\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"myModalLabel\">
        <div class=\"modal-dialog\" role=\"document\">
            <div class=\"modal-content\">
            <div class=\"modal-header\">
                <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>
                <h4 class=\"modal-title\" id=\"myModalLabel\">Upload</h4>
            </div>
            <div class=\"modal-body\">
                <form action='' method='post' enctype='multipart/form-data'>
                <div class=\"form-group\">
                    <label>Tanggal</label>
                    <input type=\"text\" class=\"form-control date\" name='date' data-date-format=\"yyyy-mm-dd\" autocomplete=\"off\">
                </div>
                <div class=\"form-group\">
                    <label>Upload CSV</label>
                    <div class=\"input-group\" >
                    <input type=\"file\" class=\"form-control\" name='fileCSV'>
                    <div class=\"input-group-btn\">
                        <button class=\"btn btn-success\" type=\"submit\" name='uploadCSV'>
                        Upload CSV
                        </button>
                    </div>
                    </div>
                </div>
                </form>
            </div>
            <div class=\"modal-footer\">
                <button type=\"button\" class=\"btn btn-danger\" data-dismiss=\"modal\">Close</button>
            </div>
            </div>
        </div>
        </div>
        
        ";
}
include('footer.php');