<?php
namespace App\Http\Controllers\v2019;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Auth\authentication;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Auth\User;

class Reports1Controller extends Controller
{ 
    public function __construct(Request $request) {
        if($request->has('debug') && $request->input('debug') == true) {
            $this->debug = true;			
        }
    }
	
	public function getReports(Request $request)
    {      
		$resourceType = "app";
		$source = "listing";
		$result = array();	
		if($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		if($request->has('source'))
			$source = strtolower($request->input('source'));
		
		$filters = array();
		if($request->has('filters') && !empty($request->input('filters')))
		    $filters = $request->input('filters');
		
		$offset=0;
		$pageValue=1;
		
		if ($request->has('pageValue') && $request->input('pageValue')!='')
			$pageValue = $request->input('pageValue');	
		
		$offset = ($pageValue-1)* DEFAULT_REPORTS_LIMIT;		
		$user_id=0;
		if ($resourceType=='app' && $request->has('accessToken') && $request->input('accessToken')!='') {
			//$uemail = $request->input('email');//oldPassword
			$token = $request->input('accessToken');
			//$user = new User();
			$auth = new authentication();
			$user_id = $auth->authToken($token);
			if(!$user_id)
				return new JsonResponse(array("status"=>array("code" => 2100, "message" => "Unauthorized action.")));
		}elseif ($resourceType=='web' && $request->has('memid') && $request->input('memid')!=''){
								
			$memid = $request->input('memid');
			$auth = new authentication();
			$user_id = $auth->authUserId($memid);//for web user
			if(!$user_id)
				return new JsonResponse(array("status"=>array("code" => 2100, "message" => "Unauthorized action.")));
		}
		
		$accessType='pre';//for visitor///
		if($user_id){
			$accessType = 'post';//check for each report, 
			/* $user = new User($user_id, $this->appType);
			$isAccessible = $user->getTakenServicesByUser($item='Reports', $at_place=$resourceType);
			if ($isAccessible==-1) {
				$accessType = 'post';
			}else{
				$accessType = 'pre';
			} */
		}
		
		if($pageValue>1 || count($filters)>0){
			$result = \ApiQuery::getReportsRows($user_id, $source, $resourceType, $type='', $filters, $accessType,DEFAULT_REPORTS_LIMIT, $offset, $this->appType);			
		}else {/// data from json array file 		
			$result = \ApiQuery::getReportsRows($user_id, $source, $resourceType, $type='default', $filters, $accessType,DEFAULT_REPORTS_LIMIT, $offset, $this->appType);
		}
		
		$code = 1001;				
		$message = "Success";	
	    return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }  
	
	public function getReportCount(Request $request){
		$result = array();
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		if($resourceType=='web'){
			$code = 1001;
			$message = "Success";
			$result = array(
				"upcoming" => 57,
				"archived" => 365
			 );
		} elseif($resourceType=='app') {
			/* if ($request->has('accessToken') && $request->input('accessToken')!='') {
			   // $auth = new authentication();
				$token = $request->input('accessToken');
				if(empty($token)){
					include_once "/home/steelmin/public_html/api/storage/v2019/reports_pre.php";
					$code = 2013;
					$message = "User not found.";
				}else{
					include_once "/home/steelmin/public_html/api/storage/v2019/reports_post.php";
					$code = 1001;
					$message = "Success";
				}
			}else{
				include_once "/home/steelmin/public_html/api/storage/v2019/reports_pre.php";
				$code = 1001;
				$message = "Success";
			} */
		}else{
			$code = 2001;
			$message = "invalid input";
		}
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}
	
}
