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

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

$con=mysqli_connect("localhost", "jibran84_itc70kk", "Smartest1", "jibran84_cms");
if(isset($_GET['dateGraph']) &&  isset($_GET['webname'])){
	$webname = $_GET['webname'];
	if ($_GET['dateGraph'] == "7" && $_GET['webname'] !="") {
		$sqlQuery = "SELECT DAYNAME(dates) as Day, COUNT(*) as Visits
						FROM new_invoice 
						WHERE DATE(dates ) > DATE_SUB( CURDATE( ) , INTERVAL 1 WEEK )
						AND  shortname = '$webname'
						GROUP BY DAYNAME(dates)
						ORDER BY dates";
	}elseif( $_GET['dateGraph'] == "30" && $_GET['webname'] !=""){
		$sqlQuery = "SELECT DAYNAME(dates) as Day, COUNT(*) as Visits
						FROM new_invoice 
						WHERE DATE(dates ) > DATE_SUB( CURDATE( ) , INTERVAL 1 MONTH ) 
						AND  shortname = '$webname'
						GROUP BY DAYNAME(dates)
						ORDER BY dates";
	}elseif( $_GET['dateGraph'] == "90" && $_GET['webname'] !=""){
		$sqlQuery = "SELECT MONTHNAME(dates) as Day, COUNT(*) as Visits
						FROM new_invoice 
						WHERE DATE(dates ) > DATE_SUB( CURDATE( ) , INTERVAL 3 MONTH ) 
						AND  shortname = '$webname'
						GROUP BY  MONTHNAME(dates)
						ORDER BY dates";
	}elseif( $_GET['dateGraph'] == "365" && $_GET['webname'] !=""){
		$sqlQuery = "SELECT MONTHNAME(dates) as Day, COUNT(*) as Visits
						FROM new_invoice 
						WHERE DATE(dates ) > DATE_SUB( CURDATE( ) , INTERVAL 1 YEAR ) 
						AND  shortname = '$webname'
						GROUP BY  MONTHNAME(dates)
						ORDER BY dates";
	}elseif ($_GET['dateGraph'] == "7" && $_GET['webname'] =="") {
		$sqlQuery = "SELECT DAYNAME(dates) as Day, COUNT(*) as Visits
						FROM new_invoice 
						WHERE DATE(dates ) > DATE_SUB( CURDATE( ) , INTERVAL 1 WEEK ) 
						GROUP BY DAYNAME(dates)
						ORDER BY dates";
	}elseif( $_GET['dateGraph'] == "30" && $_GET['webname'] ==""){
		$sqlQuery = "SELECT DAYNAME(dates) as Day, COUNT(*) as Visits
						FROM new_invoice 
						WHERE DATE(dates ) > DATE_SUB( CURDATE( ) , INTERVAL 1 MONTH ) 
						GROUP BY DAYNAME(dates)
						ORDER BY dates";
	}elseif( $_GET['dateGraph'] == "90" && $_GET['webname'] ==""){
		$sqlQuery = "SELECT MONTHNAME(dates) as Day, COUNT(*) as Visits
						FROM new_invoice 
						WHERE DATE(dates ) > DATE_SUB( CURDATE( ) , INTERVAL 3 MONTH ) 
						GROUP BY  MONTHNAME(dates)
						ORDER BY dates";
	}elseif( $_GET['dateGraph'] == "365" && $_GET['webname'] ==""){
		$sqlQuery = "SELECT MONTHNAME(dates) as Day, COUNT(*) as Visits
						FROM new_invoice 
						WHERE DATE(dates ) > DATE_SUB( CURDATE( ) , INTERVAL 1 YEAR ) 
						GROUP BY  MONTHNAME(dates)
						ORDER BY dates";
	}

	


// $sqlQuery = "SELECT DAYNAME(created_at) as Day, COUNT(*) as Visits
// FROM new_invoice 
// WHERE DATE(created_at ) > DATE_SUB( CURDATE( ) , INTERVAL 1 WEEK ) 
// GROUP BY DAYNAME(created_at)
// ORDER BY created_at";

$result = mysqli_query($con,$sqlQuery);

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

mysqli_close($con);

  echo json_encode($data);

}elseif(isset($_GET['dailySell'])){
    $sqlQuery = "SELECT sum(new_invoice.amount) as total ,DAYNAME(user_status.created_at) as Day, COUNT(*) as Sell
            FROM user_status
            join new_invoice on new_invoice.id = user_status.user_id
            WHERE DATE(user_status.created_at) > DATE_SUB( CURDATE( ) , INTERVAL 1 WEEK ) 
            and user_status.status = 'Success'
            GROUP BY DAYNAME(user_status.created_at)
            ORDER BY user_status.created_at";

$result = mysqli_query($con,$sqlQuery);

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

mysqli_close($con);

 echo json_encode($data);

    
}elseif(isset($_GET['monthSell'])){
	$lastMonth = date('Y-m-d', strtotime("-1 month"));
    $sqlQuery = "SELECT MONTHNAME(user_status.created_at) as month, sum(new_invoice.amount) as total
FROM user_status
join new_invoice on new_invoice.id = user_status.user_id
WHERE DATE(new_invoice.dates ) > DATE_SUB( '$lastMonth' , INTERVAL 1 YEAR )
and user_status.status = 'Success'
GROUP BY YEAR( user_status.created_at ) , MONTH( user_status.created_at )
  ORDER BY user_status.created_at";

$result = mysqli_query($con,$sqlQuery);

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

mysqli_close($con);

 echo json_encode($data);

    
}else{



$sqlQuery = "SELECT DAYNAME(dates) as Day, COUNT(*) as Visits
            FROM new_invoice 
            WHERE DATE(dates ) > DATE_SUB( CURDATE( ) , INTERVAL 1 WEEK ) 
            GROUP BY DAYNAME(dates)
            ORDER BY dates";

$result = mysqli_query($con,$sqlQuery);

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

mysqli_close($con);

 echo json_encode($data);
}
?>
