<?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($_GET['kelola']) && $_GET['del'] != '' ) {
        Db::delete(array(
            'table' => 'payment_weekly',
            'where' => array(
                'id' => $_GET['del']
            )
        ));
    }
    if( !isset($_GET['detail']) ) {
        $list = Db::result("SELECT * FROM `payment_weekly` ORDER BY `id` DESC LIMIT 15");
        // print_r($list);
        $new = 0;
        echo "<div class='container'><table class='table'>
            <thead>
                <th>ID</th>
                <th>Laporan Mingguan</th>
            </thead>
            <tbody>
            ";
        foreach($list as $k => $v) {
            echo "
            <tr>
                <td>{$v->id}</td>
                <td>";
            if( isset($_GET['kelola']) ) {
                echo "<a href='laporanmingguan.php?kelola&del={$v->id}' class='btn btn-danger btn-sm'>
                <i class='fa fa-trash'></i></a> ";
            }
                
            echo "<a href='laporanmingguan.php?detail={$v->id}&from={$v->start}&to={$v->end}'>{$v->start} - {$v->end}</a>";
            if ($new == 0) {
                echo " <span class='badge badge-success'>new</span>";
            }
            echo "</td>
            </tr>";
            $new++;
        }
        echo "</tbody></table></div>";
    } else {
        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'>
                        
                        
                    </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 `start` >= '".date('Y-m-d', $data['from']) ."' ";
            }
            if ($data['to'] != "") {
                $where .= "AND `end` <= '" . date('Y-m-d', $data['to']) . "' ";
            }
        // select distinct ID
        $q = Db::result("SELECT *  FROM `payment_weekly` WHERE `id` = '{$_GET['detail']}' ");
        $totalEarning = 0;
        if (!isset($q['error'])) {
            // print_r($q);
            foreach($q as $k => $v) {
                $rp = json_decode($v->data, true);
                // print_r($rp);
                for( $i=0; $i < count($rp['id']); $i++ ) {
                    echo "
                    <tr>
                        <td><strong>{$rp['id'][$i]}</strong></td>
                        <td>{$rp['earning'][$i]}</td>
                        <td>90%</td>
                        <td>".$rp['jml'][$i]."</td>
                        <td><span id='rate'>".$rp['rate'][$i]."</span></td>
                        <td class='text-right'>Rp. ".number_format($rp['finalValue'][$i])."</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>Rp. ".number_format($v->uang_masuk)."</td>
                <td>10%</td>
                <td>Rp. ".number_format($v->uang_masuk*0.1)."</td>
            </tr>
            </table>
            </div>";

        echo "
        </div>
        
        
        
        ";

    }
}
include('footer.php');