<?php session_start(); error_reporting(0); 
//if session
if(isset($_SESSION['j_admin'])){ }else{ 
    die(
    "
    <div class='alert alert-danger'> Error 100. Try again.</div>
    "
    );
}
require_once("j_conn/url.php");
require_once("j_conn/dbc.php");
require_once("j_incs/functions.php");
if(isset($_POST['period'])){ }else{ 
die(
"
<div class='alert alert-danger'> Error 101. Try again.</div>
"
);
}
$period = RMC(trim($_POST['period'])); 
$currency = RMC(trim($_POST['currency'])); 
//
$cd = get_cd($currency);
$currency_in_use = $cd[0];
$currency_code_in_use = $cd[1];
$currency_sort_in_use = $cd[2];
//
if($period=="Today"){
    $start_date = date("Y-m-d 00:00:00");
    $end_date = $CurrentDate;
}
if($period=="Yesterday"){
    $date = date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $CurrentDate) ) ));
    $start_date = "$date 00:00:00";
    $end_date = "$date 24:59:59";
}
if($period=="This Week"){
    $d = strtotime("today");
    $start_week = strtotime("last sunday midnight",$d);
    $end_week = strtotime("next saturday",$d);
    $start = date("Y-m-d",$start_week); 
    $end = date("Y-m-d",$end_week);  
    //
    $start_date = "$start 00:00:00";
    $end_date = "$end 24:59:59";
}
if($period=="Last Week"){
    //$d = strtotime("+1 week -1 day"); //next week
    $d = strtotime("-1 week +1 day"); //last week
    $start_week = strtotime("last sunday midnight",$d);
    $end_week = strtotime("next saturday",$d);
    $start = date("Y-m-d",$start_week); 
    $end = date("Y-m-d",$end_week);  
    //
    $start_date = "$start 00:00:00";
    $end_date = "$end 24:59:59";
}
if($period=="This Month"){
    $start =  date('01-m-Y');
    $end = date('t-m-Y');
    $start_date = "$start 00:00:00";
    $end_date = "$end 24:59:59";
}
if($period=="Last Month"){
    $start =  date("Y-n-01", strtotime("first day of previous month"));
    $end = date("Y-n-j", strtotime("last day of previous month"));
    $start_date = "$start 00:00:00";
    $end_date = "$end 24:59:59";
}
if($period=="This Year"){
    $start =  date("Y-01-01");
    $end =  date("Y-12-31");
    $start_date = "$start 00:00:00";
    $end_date = "$end 24:59:59";
}
if($period=="Last Year"){
    $yearx = date("Y"); $yearx = $yearx-1;
    
    $start =  date("$yearx-01-01");
    $end =  date("$yearx-12-31");
    $start_date = "$start 00:00:00";
    $end_date = "$end 24:59:59";
}
if($period=="All Time"){
    $start =  date("2019-01-01");
    $start_date = "2019-01-01 00:00:00";
    $end_date = $CurrentDate;
}
//echo "$start_date ++++ $end_date";
/*
$sql4="SELECT sum(j_amount) as total from j_pays WHERE 
j_paid > 0 AND j_type='credit' AND j_deleted < 1 AND j_currency='".$currency."' 
AND j_date >= '".date('Y-m-d H:i:s', strtotime($start_date))."' AND j_date <= '".date('Y-m-d H:i:s', strtotime($end_date))."'
"; //check 
$result4 = runSQL($sql4);
$data = mysqli_fetch_array($result4);
$credit = $data['total'];
//
$sql4="SELECT sum(j_amount) as total from j_pays WHERE 
j_paid > 0 AND j_type='debit' AND j_deleted < 1 AND j_currency='".$currency."' 
AND j_date >= '".date('Y-m-d H:i:s', strtotime($start_date))."' AND j_date <= '".date('Y-m-d H:i:s', strtotime($end_date))."'
"; //check 
$result4 = runSQL($sql4);
$data = mysqli_fetch_array($result4);
$debit = $data['total'];
$users_bal = $credit-$debit;
*/
//
$sql4="SELECT * from j_users WHERE j_deleted < 1 AND j_status < 1
AND j_date >= '".date('Y-m-d H:i:s', strtotime($start_date))."' AND j_date <= '".date('Y-m-d H:i:s', strtotime($end_date))."'
"; //check 
$result4 = runSQL($sql4);
$students = mysqli_num_rows($result4);
//
$sql4="SELECT sum(j_amount) as total from j_pays WHERE 
j_paid > 0 AND j_type='debit' AND j_deleted < 1 AND j_kind ='ACTIVATION' AND j_currency='".$currency."' 
AND j_date >= '".date('Y-m-d H:i:s', strtotime($start_date))."' AND j_date <= '".date('Y-m-d H:i:s', strtotime($end_date))."'
||
j_paid > 0 AND j_type='debit' AND j_deleted < 1 AND j_kind ='ACTIVATION-A' AND j_currency='".$currency."' 
AND j_date >= '".date('Y-m-d H:i:s', strtotime($start_date))."' AND j_date <= '".date('Y-m-d H:i:s', strtotime($end_date))."'
||
j_paid > 0 AND j_type='debit' AND j_deleted < 1 AND j_kind ='ACTIVATION-M' AND j_currency='".$currency."' 
AND j_date >= '".date('Y-m-d H:i:s', strtotime($start_date))."' AND j_date <= '".date('Y-m-d H:i:s', strtotime($end_date))."'
"; //check 
$result4 = runSQL($sql4);
$data = mysqli_fetch_array($result4);
$income = $data['total'];
//
$sql4="SELECT sum(j_amount) as total from j_pays WHERE 
j_paid > 0 AND j_type='credit' AND j_deleted < 1 
AND j_date >= '".date('Y-m-d H:i:s', strtotime($start_date))."' AND j_date <= '".date('Y-m-d H:i:s', strtotime($end_date))."'"; //check 
$result4 = runSQL($sql4);
$data = mysqli_fetch_array($result4);
$credit = $data['total'];
//
$sql4="SELECT sum(j_amount) as total from j_pays WHERE 
j_paid > 0 AND j_type='debit' AND j_deleted < 1
AND j_date >= '".date('Y-m-d H:i:s', strtotime($start_date))."' AND j_date <= '".date('Y-m-d H:i:s', strtotime($end_date))."'"; //check 
$result4 = runSQL($sql4);
$data = mysqli_fetch_array($result4);
$debit = $data['total'];
//
$balance = $credit-$debit;
//
$sql4="SELECT sum(j_bonus) as total from j_pays WHERE 
j_paid > 0 AND j_referrer_id !='' AND j_deleted < 1 AND j_currency='".$currency."' 
AND j_date >= '".date('Y-m-d H:i:s', strtotime($start_date))."' AND j_date <= '".date('Y-m-d H:i:s', strtotime($end_date))."'
"; //check 
$result4 = runSQL($sql4);
$data = mysqli_fetch_array($result4);
$bonus = $data['total']; 
//
$profit = $income-$bonus;
//
?>
<h4>Reporting Date</h4>
<p>
    <span class="text-success"><?php echo FULL_DATE($start_date); ?></span>
    till
    <span class="text-danger"><?php echo FULL_DATE($end_date); ?></span>
</p>

<div class="row">
                        
    <div class="col-md-6 col-lg-4">
        <div class="card">
            <div class="card-body">
                <div class="media align-items-center">
                    <div class="avatar avatar-icon avatar-lg avatar-gold">
                        <i class="fas fa-users"></i>
                    </div>
                    <div class="m-l-15">
                        <h2 class="m-b-0" style="font-size:14px;"><?php echo number_format($students); ?></h2>
                        <p class="m-b-0 text-muted">Users </p>
                    </div>
                </div>
            </div>
        </div>
    </div>


     <div class="col-md-6 col-lg-4">
        <div class="card">
            <div class="card-body">
                <div class="media align-items-center">
                    <div class="avatar avatar-icon avatar-lg avatar-cyan">
                        <i class="fas fa-wallet"></i>
                    </div>
                    <div class="m-l-15">
                        <h2 class="m-b-0" style="font-size:14px;"><?php echo amount_w_c($credit,$currency_in_use); ?></h2>
                        <p class="m-b-0 text-muted">Credit</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    

     <div class="col-md-6 col-lg-4">
        <div class="card">
            <div class="card-body">
                <div class="media align-items-center">
                    <div class="avatar avatar-icon avatar-lg avatar-red">
                        <i class="fas fa-wallet"></i>
                    </div>
                    <div class="m-l-15">
                        <h2 class="m-b-0" style="font-size:14px;"><?php echo amount_w_c($debit,$currency_in_use); ?></h2>
                        <p class="m-b-0 text-muted">Debit</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
  
    
</div>