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

date_default_timezone_set('Asia/Jakarta');

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

require 'autoload.php';

$page = "payment";

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("yesterday");
    $data['to'] = isset($_GET['to']) && $_GET['to'] != "" ? strtotime($_GET['to']): strtotime("yesterday");
}

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




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();

    
    echo "<div class=\"container\">
    <div class='row'>
    
    
    <div class='clearfix'></div>
     
     </div>
     <hr /> 
     <div class=\"card card-default searchbox\">
     
     
        <div class=\"card-header\">
            <div class='row'>
            <div class='col-12 col-md-6'>
            <h5>Payment Report - ".date("d M Y", $data['from'])." - ".date("d M Y", $data['to'])."</h5>
            </div>
            <div class='col-12 col-md-6'>
                <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>
                    
                </form>
                
            </div>
            </div>
        </div>

    
    <div class='table-responsive'>
    <table id='myTable' class='table table-striped'><thead>
        <tr>
            <th>ID</th>
            <th>Earnings</th>
            <th>%</th>
            <th>Jumlah</th>
            <th>Rate Dollar</th>
            <th>Rupiah</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']) . "' ";
        }
    // select distinct ID
    $q = Db::result("SELECT DISTINCT `name` FROM `payment`");
    $totalEarning = 0;
if (!isset($q['error'])) {
    // print_r($q);
    foreach($q as $k => $v) {
        $q2 = Db::result("SELECT SUM(`conversions`) as `conversions`,
        SUM(`earning`) as  totalEarning
        FROM `payment` WHERE `name` = '{$v->name}' {$where} ");
        // print_r($q2);
        $earningFinal = (number_format($q2[0]->totalEarning, 2)*0.9)*($rate);
        $totalEarning = $earningFinal + $totalEarning;
        // echo $earning;
        echo "
    <tr>
        <td><strong>{$v->name}</strong></td>
        <td>".number_format($q2[0]->totalEarning, 2)."</td>
        <th>90%</th>
        <th>".number_format($q2[0]->totalEarning, 2)*0.9."</th>
        <th><span id='rate'>".number_format($rate,0)."</span></th>
        <th class='text-right'>".number_format($earningFinal)."</th>
    </tr>";
    }
} else {
    echo "<tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
</tr>";
}

    echo "</tbody>

        </tbody>
        </table>
        <table class='table'>
        <thead>
            <th>Uang Masuk</th>
            <th>%</th>
            <th>Uang Kas</th>
        </thead>
        <tr>
            <td>".number_format($totalEarning)."</td>
            <td>10%</td>
            <td>".number_format($totalEarning*0.1)."</td>
        </tr>
        </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');