<?php
namespace App\Http\Controllers\v2019;
use Illuminate\Http\Request;
use App\Http\Controllers\Auth\authentication;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\JsonResponse;

class Intel1Controller extends Controller
{   
    public function __construct(Request $request) {
        if($request->has('debug') && $request->input('debug') == true) {
            $this->debug = true;			
		}
    }
	
    public function getIntelByFilter(Request $request){
	   $sql = DB::table("intel")->where("1","=","1");
       if($request->input("filter")){
		   if($request->has("id") && $request->input("id") != null)
				$sql = $sql->where("id","=","1");
		   if($request->has("title") && $request->input("title") != null)
				$sql = $sql->where('title','LIKE',"%{$request->input("title")}%");		
	  }
	  return $sql;  
	}

	public function getIntel(Request $request)
    {      
		$result = array();
		$resourceType = "APP";
			
		if($request->has('resourceType'))
			echo $resourceType = strtolower($request->input('resourceType'));
		
		if($request->has('filter'))
		    $type = $request->input('type'); 
     
			if($resourceType=='web'){
				$code = 1001;				
				$message = "Success";$offset=0;
				if ($request->has('memid') && $request->input('memid')!=''){
					$pageValue = $request->input('pageValue');					
					$offset = ($pageValue-1)* DEFAULT_LIMIT;
					
					$memid = $request->input('memid');
					$auth = new authentication();
					$user_id = $auth->authUserId($memid);//for web user
					if ($user_id) {
						if($pageValue>1){//run query here
	                        $result = \apiQuery::getIntelRowSet($resourceType, $type, $filter=array(),'post',DEFAULT_LIMIT, $offset);
							/* $sql = DB::table('intel')->offset($offset)->limit(DEFAULT_LIMIT); 
						    $result = $sql->get(); */
						}else {/// data from json array file 
							$result = \apiQuery::getIntelRowSet($resourceType, $type, $filter=array(),'post',DEFAULT_LIMIT,0);
							////$sql = DB::table('intel')->limit(DEFAULT_LIMIT); 
							///include_once "/home/steelmin/public_html/api/storage/v2019/json/web/intel/intel_web_post.php";
						}
					}else{
						$code = 2013;
						$message = "Unauthorized action.";
					}
					
				}else{/// pre data from json array file 
                    ////$sql = DB::table('intel')->limit(5);
					$result = \apiQuery::getIntelRowSet($resourceType, $type, $filter=array(),'pre',DEFAULT_LIMIT, 0);
					//include_once "/home/steelmin/public_html/api/storage/v2019/json/web/intel/intel_web_pre.php";
				}								
		    }else {    
				if ($request->has('accessToken') && $request->input('accessToken')!='') {				
					$auth = new authentication();	
					$token = $request->input('accessToken');
					$user_id = $auth->authToken($token);	
					
					$pageValue = $request->input('pageValue');					
					$offset = ($pageValue-1)* DEFAULT_LIMIT;
					
					if ($user_id) {
						if($pageValue>1){//run query here
							echo "sf";
							$result = \apiQuery::getIntelRowSet($resourceType, $type, $filter=array(),'post',DEFAULT_LIMIT, $offset);						
							//$sql = DB::table('intel')->offset($offset)->limit(DEFAULT_LIMIT); 
							//$result = $sql->get();
						}else {/// data from json array file 
							echo "fs";
							$result = \apiQuery::getIntelRowSet($resourceType, $type, $filter=array(),'post',DEFAULT_LIMIT,0);
							////$sql = DB::table('intel')->limit(DEFAULT_LIMIT); 
							//include_once "/home/steelmin/public_html/api/storage/v2019/json/app/intel/intel_app_post.php";
						}
					}else{
						$code = 2013;
						$message = "Unauthorized action.";
					}
				}else{
					echo "gs";
					$result = \apiQuery::getIntelRowSet($resourceType, $type, $filter=array(),'pre',DEFAULT_LIMIT, 0);
					//include_once "/home/steelmin/public_html/api/storage/v2019/json/app/intel/intel_app_pre.php";
					$code = 1001;
					$message = "Success";
				}
			}	
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }  
	
	public function getIntelTrending(Request $request)
    {      
		$result = array();
		include_once "/home/steelmin/public_html/api/storage/v2019/intel_trending_post.php";
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
           // $auth = new authentication();
           $token = $request->input('accessToken');
           if(empty($token)){
			   $code = 2013;
				$message = "User not found.";
			}else{
				$code = 1001;
				$message = "Success";
			}
        }else{
			
			$code = 1001;
			$message = "Success";
		}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }
	
}
