	<?php
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 11:48 AM
	* Location profil -> data akun home dan menu samping
	*/

	require_once("../connection.php");
	
		$key = mysqli_real_escape_string($connect, $_GET["key"]);
		
		$setting = mysqli_query($connect,"SELECT * FROM setting");
	$s=mysqli_fetch_array($setting);


	$getusers	= mysqli_query($connect,"SELECT * FROM users WHERE id_session='$key'");
	$u	   		= mysqli_fetch_array($getusers);
	$amount   	= mysqli_num_rows($getusers);
	
	$masterakun = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$u[master]'");
	$m=mysqli_fetch_array($masterakun);

	if ($amount > 0){

	$query = "SELECT * FROM users WHERE id_session='$u[id_session]'";
	$hasil = mysqli_query($connect,$query);


	$dataomset = mysqli_query($connect,"SELECT SUM(totalorder) as total FROM sales_data WHERE operator='$u[phone_number]' AND status NOT IN ('cancel')");
	$n=mysqli_fetch_array($dataomset);
	
	
	$dataetalase = mysqli_query($connect,"SELECT * FROM online_store WHERE user='$m[phone_number]'");
	$d=mysqli_fetch_array($dataetalase);

	if($n['total']==''){
	$omset=0;
	}else{
	$omset= $n['total'];
	}	
	
	$myDate1b = date('Y-m-01');
	$myDate2b = date('Y-m-31');
	
	$datatransaksi = mysqli_query($connect,"SELECT SUM(totalorder) as totalod FROM sales_data 
											WHERE id_store='$u[id_store]'
											AND status NOT IN ('cancel')
											AND date>='$myDate1b' 
											AND date<='$myDate2b'");
	$dt=mysqli_fetch_array($datatransaksi);
	if($dt['totalod']==''){
	$transaksi=0;
	}else{
	$transaksi= $dt['totalod'];
	}
	
	$dataorder = mysqli_query($connect,"SELECT SUM(amount) as amounts FROM sales 
											WHERE id_store='$u[id_store]'
											AND status NOT IN ('cancel')
											AND date>='$myDate1b' 
											AND date<='$myDate2b'");
	$do=mysqli_fetch_array($dataorder);
	if($do['amounts']==''){
	$order=0;
	}else{
	$order= $do['amounts'];
	}
	
		$subdomain="http://itoyu.id/store/id-$d[subdomain]";
		$signup="http://$_SERVER[HTTP_HOST]/app/sign-$d[subdomain]";
		$screen="http://$_SERVER[HTTP_HOST]/screen/id-$d[subdomain]";


	if(mysqli_num_rows($hasil) > 0 ){
	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
	);
	$response["data"] = array();
	while($x = mysqli_fetch_array($hasil)){
		if($m['packages']=='1'){
			$type="premium";
		}else{
			$type="free";
		} 
    $h['full_name'] 	= $x["full_name"];
    $h['address'] 		= $x["address"];
    $h['email'] 		= $x["email"];
    $h['phone_number'] 	= $x["phone_number"];
    $h['key'] 			= $x["id_session"];
    $h['afiliasi'] 		= $x["afiliasi"];
    $h['omset'] 		= $omset;
    $h['saldo'] 		= "";
	$h['subdomain'] 	= $subdomain;
    $h['point'] 		= "";
	$h['transaksi'] 	= $transaksi;
    $h['order'] 		= $order;
    $h['type'] 			= $type;
	$h['system_point'] 	= $s['point_system'];
    $h['img'] 			= "http://$_SERVER[HTTP_HOST]/geten/images/foto/$x[img]";
    $h['signup'] 			= $signup;
    $h['screen'] 			= $screen;
    array_push($response["data"], $h);
	}
	echo json_encode($response);
	}else {
	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '02',
	'msg' 		=> 'no data'
	);
	echo json_encode($response);
	}


	}else{
	
	$json = array(
    'status' 	=> 'true',
    'errCode' 	=> '00',
	'msg' 		=> 'User Not Found'
	);

    echo json_encode($json);	
	}
	?>