	<?php
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 11:48 AM
	*/

	require_once("../connection.php");
	
	$key=mysqli_real_escape_string($connect, $_GET['key']);


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


	if ($jumlah > 0){

	$query = "SELECT * FROM users WHERE master='$m[phone_number]' AND level NOT IN ('master') AND blokir ='N' ORDER BY full_name ASC";
	$hasil = mysqli_query($connect,$query);


	if(mysqli_num_rows($hasil) > 0 ){
	$response = array(
    'status' => 'true',
    'errCode' => '01',
	'msg' => 'Success'
	);
	$response["data"] = array();
	while($x = mysqli_fetch_array($hasil)){
		
	$datatoko = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$x[id_store]'");
	$t=mysqli_fetch_array($datatoko);
	
	$dataomset = mysqli_query($connect,"SELECT * FROM sales_data WHERE operator='$u[phone_number]'  AND status NOT IN ('cancel')");
	$jumlah   	= mysqli_num_rows($dataomset);
	
	$dataomset = mysqli_query($connect,"SELECT * FROM sales_data WHERE operator='$u[phone_number]'  AND status IN ('billing', 'pending')");
	$jumlah_procces   	= mysqli_num_rows($dataomset);
	
	$dataomsetf = mysqli_query($connect,"SELECT * FROM sales_data WHERE operator='$u[phone_number]'  AND status IN ('finish')");
	$jumlah_finish   	= mysqli_num_rows($dataomsetf);

	$dataomsetp = mysqli_query($connect,"SELECT * FROM sales_data WHERE operator='$u[phone_number]'  AND status IN ('paid of')");
	$jumlah_paid   	= mysqli_num_rows($dataomsetp);

    $h['full_name'] = $x["full_name"];
    $h['city'] = $x["city"];
    $h['address'] = $x["address"];
    $h['email'] = $x["email"];
    $h['level'] = $x["level"];
    $h['master'] = $x["master"];
    $h['phone_number'] = $x["phone_number"];
    $h['key'] = $x["id_session"];
    $h['salary'] = $x["salary_fixed"];
    $h['commission'] = $x["commission"];
    $h['allowance'] = $x["allowance"];
    $h['piece'] = $x["piece"];
    $h['id_store'] = $x["id_store"]; 
    $h['name_store'] = $t["name_store"]; 
    $h['total_work'] = $jumlah; 
    $h['work_procces'] = $jumlah_procces; 
    $h['work_finish'] = $jumlah_finish; 
    $h['work_paid'] = $jumlah_paid; 
    $h['img'] 			= "http://$_SERVER[HTTP_HOST]/geten/images/foto/$x[img]";
    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);	
	}
	?>
