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

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

class Statistics1Controller extends Controller
{ 
    public $appType = 'SM' ;
	public function __construct(Request $request) {
        if($request->has('debug') && $request->input('debug') == true) {
            $this->debug = true;			
        }
		if($request->has('appType') && $request->input('appType') == true) {
            $this->appType = $request->input('appType');		
        }
    }
	
	public function getFilterLeftMain(Request $request)
    {      
		$result = array();
		//include_once "/home/steelmin/public_html/api/storage/v2019/reports_pre.php";
		$source = strtolower($request->input('source'));
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		include_once "/home/steelmin/public_html/api/storage/v2019/web/web_stat_filter_main.php";
		$code = 1001;
		$message = "Success";	
		
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));	
	 
	}
	
	public function getFilterleftDate(Request $request)
    {      
		$result = array();
		//include_once "/home/steelmin/public_html/api/storage/v2019/reports_pre.php";
		$source = strtolower($request->input('source'));
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		include_once "/home/steelmin/public_html/api/storage/v2019/web/web_stat_filter_date.php";
		$code = 1001;
		$message = "Success";	
		
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));	
	 
	}
	
	public function getGraphs(Request $request)
    {      
		$result = array();
		//include_once "/home/steelmin/public_html/api/storage/v2019/reports_pre.php";
		$source = strtolower($request->input('source'));
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		include_once "/home/steelmin/public_html/api/storage/v2019/web/statistic_post.php";
		$code = 1001;
		$message = "Success";	
		
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));	
	 
	}  
	
	public function getGraphs2ndLevel(Request $request)
    {      
		$result = array();
		//include_once "/home/steelmin/public_html/api/storage/v2019/reports_pre.php";
		$source = strtolower($request->input('source'));
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		include_once "/home/steelmin/public_html/api/storage/v2019/web/statistic_2ndlevel_post.php";
		$code = 1001;
		$message = "Success";	
		
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));	
	 
	}
	
	public function getStatisticsPortfolio(Request $request)
    {      
		$result = array();
		if($request->has('memid') && !empty($request->input('memid'))){
			$memid = $request->input('memid');
			$auth = new authentication();
			$memid = $auth->authUserId($memid);//for web user
			
			if($memid){
				$common = new Common();
				$result = $common->getUserPortfolioV2019($memid, $type='statistics', $appType='SM');
				//include_once "/home/steelmin/public_html/api/storage/v2019/web/statistic_2ndlevel_post.php";
				$code = 1001;
				$message = "Success";	
			}else{
				$code = 2013;
				$message = "User not found.";
			}
		}else{
			$code = 2013;
			$message = "User not found.";
		}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));	
	 
	}
	
	public function getStatistics(){
	   $result = array();
	   $code = 1001;
	   $message = "Success";	
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}

	public function getInfograph(){
		$result = array();
		//include_once "/home/steelmin/public_html/api/storage/v2019/reports_pre.php";
		$source = strtolower($request->input('source'));
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		include_once "/home/steelmin/public_html/api/storage/v2019/web/web_infograph.php";
		$code = 1001;
		$message = "Success";	
		
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}

	public function getRecentView(){
		$result = array();
		//include_once "/home/steelmin/public_html/api/storage/v2019/reports_pre.php";
		$source = strtolower($request->input('source'));
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		include_once "/home/steelmin/public_html/api/storage/v2019/web/web_recent_view.php";
		$code = 1001;
		$message = "Success";	
		
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}

	public function getTabular(){
		$result = array();
		//include_once "/home/steelmin/public_html/api/storage/v2019/reports_pre.php";
		$source = strtolower($request->input('source'));
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		include_once "/home/steelmin/public_html/api/storage/v2019/web/web_tabular_view.php";
		$code = 1001;
		$message = "Success";	
		
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}

	public function getTabularGraph(){
		$result = array();
		//include_once "/home/steelmin/public_html/api/storage/v2019/reports_pre.php";
		$source = strtolower($request->input('source'));
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		include_once "/home/steelmin/public_html/api/storage/v2019/web/web_tabular_graph.php";
		$code = 1001;
		$message = "Success";	
		
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}

	public function getTabularFilter(){
		$result = array(
			"country" => array(
			  array(
				"countryID" => 2 ,
				"countryName" => "India",
			  )
			),            "commodity" => array(
			  array(
				"commodityID" => 2 ,
				"commodityName" => "Fines",
			  )
			)
		);
		//include_once "/home/steelmin/public_html/api/storage/v2019/reports_pre.php";
		$source = strtolower($request->input('source'));
		$resourceType = "APP";
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));       
		$code = 1001;
		$message = "Success";       
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}
	
	public function addInSavedList(Request $request)
    {      
	   $result = array();
	   
		$resourceType = "APP";
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
			$source = $request->input('source');//Tender/Prices
			$memid = $request->input('memid');
		if($memid){
			/* "memid" : 83757,
			  "resourceType" : "web",        
			  "regionID"    : 45,
			  "commodityId" :2,
			  "dataType"    : "export",
			  "assessmentType" "yearly"
			  "periodFrom"  : 2014,
			  "periodTo"    : 2019,
			  "headerType"  : "" */
		
			$code = 1001;
			$message = "Success";
		}else{
			$code = 2100;
			$message = "Access token is invalid or has expired.";
		}
	   //, "data"=> $result;
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
    } 
	
	public function downloadSavedList(Request $request)
    {      
	   $result = array();
	   
		$resourceType = "APP";
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
			$source = $request->input('source');//Tender/Prices

			/* "memid" : 83757,
			  "resourceType" : "web",        
			  "regionID"    : 45,
			  "commodityId" :2,
			  "dataType"    : "export",
			  "assessmentType" "yearly"
			  "periodFrom"  : 2014,
			  "periodTo"    : 2019,
			  "headerType"  : "" */
		
		$code = 1001;
		$message = "Saved Successfully";
	   //, "data"=> $result;
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
    } 
}
