<?php
header('Content-Type: application/json');

// $con = mysqli_connect("localhost", "", "", "jibran84_ach");

$con=mysqli_connect("localhost", "jibran84_itc70kk", "Smartest1", "jibran84_cms");


if(isset($_GET['country'])){
   $sqlQuery = "SELECT country, COUNT(user_status.status) AS NUM FROM user_status
join new_invoice on new_invoice.id = user_status.user_id
where user_status.status = 'Decline'
GROUP BY country"; 
}elseif(isset($_GET['bankDec'])){
    $sqlQuery = "SELECT bank, COUNT(user_status.status) AS NUM FROM user_status
WHERE status = 'Decline'
GROUP BY bank";
}elseif(isset($_GET['Search'])){
    $sqlQuery = "select DAYNAME(user_status.created_at) as Day,
    count(user_status.status) total,
    sum(status='Success') Success,
    sum(status='Decline') Decline
from user_status
WHERE DATE(user_status.created_at ) > DATE_SUB( CURDATE( ) , INTERVAL 1 WEEK ) 
 GROUP BY created_at";
}
elseif(isset($_GET['from']) && isset($_GET['to'])){
    
    $from = $_GET['from'];
    $to = $_GET['to'];
    $webname = $_GET['webname'];
    
    $timestamp = strtotime($from);
$from= date('Y-m-d', $timestamp); 

 
$timestamp2 = strtotime($to);
$to = date('Y-m-d', $timestamp2); 
    
    if( $webname == ""){
     $sqlQuery ="SELECT new_invoice.shortname, COUNT(user_status.`status`) as total FROM user_status 
                join new_invoice on new_invoice.id = user_status.user_id
                WHERE user_status.created_at >= '$from' AND user_status.created_at <= '$to' and user_status.status = 'Decline'
                GROUP BY new_invoice.shortname";
    }elseif( $webname != ""){
        $date1=date_create($from);
        $date2=date_create($to);
        $diff=date_diff($date1,$date2);
        $months = $diff->format("%m months");
        $years = $diff->format("%y years");
        $days = $diff->format("%d days");
        $days = str_replace("+","",str_replace(" days","",$days));
        if($days < 30){
            
        }
    }
    

}
else{
$sqlQuery = "SELECT websitename,country, COUNT(*) AS NUM FROM new_invoice GROUP BY country";
}
$result = mysqli_query($con,$sqlQuery);

$data = array();
foreach ($result as $row) {
	$data[] = $row;
}

mysqli_close($con);

  echo json_encode($data);
?>
