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

    if( isset( $_POST['createReport'] )) {
        // print_r($_POST);
        $report['id'] = $_POST['id'];
        $report['rate'] = $_POST['rateValue'];
        $report['earning'] = $_POST['earning'];
        $report['jml'] = $_POST['jml'];
        $report['finalValue'] = $_POST['finalValue'];

        $reportJson = json_encode($report);
        // echo $reportJson;
        $ins = Db::insert(array(
            'table' => 'payment_weekly',
            'key' => array(
                'start' => date("Y-m-d", $data['from']),
                'end' => date("Y-m-d", $data['to']),
                'data' => $reportJson,
                'rate' => $_POST['rate'],
                'uang_masuk' => $_POST['uangMasuk'],
                'uang_kas' => $_POST['uangKas'],
            )
        ));

        if ($ins) {
            echo "<div class='alert alert-success'>Report Berhasil Dibuat.</div>";
        }
        
    }
    
    echo "<div class=\"container\">
    <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 class='clearfix'></div>
     
     </div>
     <hr /> 
     <div class=\"card card-default searchbox\">
     <form action='' method='post'>
     
        <div class=\"card-header\">
            <div class='row'>
                <div class='col-12 col-md-6'>
                    <input type='text' id='rate' name='rate' class='form-control' placeholder='Rate Dollar' required>
                </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);
        // echo $earning;
        $percent_90 = number_format($q2[0]->totalEarning, 2, ".", "")*0.9;
        echo "
    <tr>
        <td><strong>{$v->name}</strong>
        <input type='hidden' name='id[]' value='".$v->name."'>
        </td>
        <td>".number_format($q2[0]->totalEarning, 2)."
        <input type='hidden' name='earning[]' class='earning' value='".number_format($q2[0]->totalEarning, 2)."'>
        </td>
        <td>90%</td>
        <td>".number_format($percent_90, 2)."
        <input type='hidden' name='jml[]' class='jml' value='".$percent_90."'>
        </td>
        <td><input type='text' class='form-control rateValue' name='rateValue[]' value=''></td>
        <td><input type='text' class='form-control finalValue' name='finalValue[]' value=''></td>
    </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><input type='text' name='uangMasuk' class='form-control uangMasuk' value='' required></td>
            <td>10%</td>
            <td><input type='text' name='uangKas' class='form-control uangKas' value='' required></td>
        </tr>
        </table>
        
        </div>
        <div class='col-12 text-center'>
        <button class='btn btn-success' type='submit' name='createReport'>Create Report</button>
        </div>
        <br />
        
        ";

        echo "
        </form>
        </div>
        
        
        
        ";
}
include('footer.php');