<?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;
use App\Http\Controllers\Auth\User;

use App\Traits\ReferenceData;

class common1Controller extends Controller
{ 
	use ReferenceData;
    public function __construct(Request $request) {
        if($request->has('debug') && $request->input('debug') == true) {
            $this->debug = true;			
        }
    }
	
	public function index()
    {      
	   $result = array();
	   $result = $this->referenceDataArr;
	   
	   return new JsonResponse(array("status"=>array("code" => 1001, "message" => "success"), "data"=> $result));
    }  
	
	public function alerts(Request $request)
    {      
	   $result = array();
	   
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
			$source = $request->input('source');//Tender/Prices

			$itemId = $request->input('itemId');
			$mediumList = $request->input('mediumList');//["email"  , "sms" ]
			$xDaysBefore = $request->input('xDaysBefore');
			$action = $request->input('action'); 
			$accessToken = $request->input('accessToken');
		}
		$code = 1001;
		$message = "Alert Set Successfully";
	   //, "data"=> $result;
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
    } 
	
	public function removeBookmark(Request $request)
    {      
	    $result = array();
	    $accessToken = $request->input('accessToken');
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
			$source = $request->input('source');//price/insights/tender

			$itemId = $request->input('itemId');
			$section = $request->input('section'); //bookmark/compare/portfolio/alerts
			switch($section){
				case "bookmark":
					switch($source){
						case "price":
						break;
						case "insights":
						break;
						case "tender":
						break;
					}
				break;
				case "compare":
					switch($source){
						case "price":
						break;
						case "insights":
						break;
						case "tender":
						break;
					}
				break;
				case "portfolio":
					switch($source){
						case "price":
						break;
						case "insights":
						break;
						case "tender":
						break;
					}
				break;
				case "alerts":
					switch($source){
						case "price":
						break;
						case "insights":
						break;
						case "tender":
						break;
					}
				break;
			}
			
			$code = 1001;
			$message = "Successfully Saved";
		}else{
			$code = 2100;
			$message = "Access token is invalid or has expired.";
		}
		
	   //, "data"=> $result;
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
    } 
	//Add bookmark -
	public function addBookmark(Request $request)
    {      
	    $result = array();
	    $accessToken = $request->input('accessToken');
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
			$source = $request->input('source');//price/insights/tender

			$itemIds = $request->input('itemIds');
			///$section = $request->input('section'); //bookmark/compare/portfolio/alerts
			switch($source){
				case "price":
				break;
				case "insights":
				break;
				case "tender":
				break;
			}
			
			$code = 1001;
			$message = "Successfully Bookmarked";
		}else{
			$code = 2100;
			$message = "Access token is invalid or has expired.";
		}
		
	   //, "data"=> $result;
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
    }
	//Show more/less Like this - insight
	public function showSimilar(Request $request)
    {      
	    $result = array();
	    $accessToken = $request->input('accessToken');
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
			$source = $request->input('source');//price/insights/tender

			$itemIds = $request->input('itemIds');
			$type = $request->input('type'); //more/less
			switch($source){				
				case "insights":
				break;
				case "tender":
				break;
			}
			
			$code = 1001;
			$message = "Successfully Saved";
		}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 portfolioAddItem(Request $request)
    {      
	    $result = array();
	    $accessToken = $request->input('accessToken');
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
			$source = $request->input('source');//price/insights/tender

			$itemIds = $request->input('itemIds');
			///$section = $request->input('section'); //bookmark/compare/portfolio/alerts
			switch($source){
				case "price":
				break;
				case "insights":
				break;
				case "tender":
				break;
			}
			
			$code = 1001;
			$message = "Successfully added to portfolio";
		}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 preferences(Request $request)
    {      
		$result = array();
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
		}
		$source = strtolower($request->input('source'));
		if($source=='' || empty($source)){
			$source ='all';
		}
		$result = array(
					'all'=>array(
						"commodityId" => array(1,3,5,11,21,31),
						"regionId" => array(1,3,5,11,21,31),
						"channel" => array("sms","email","wa")
					),
					'tenders'=>array(
						"commodityId" => array(1,3,5,11,21,31),
						"regionId" => array(1,3,5,11,21,31),
						"channel" => array("sms","email","wa")
					),
					'insight'=>array(
						"commodityId" => array(1,3,5,11,21,31),
						"regionId" => array(1,3,5,11,21,31),
						"channel" => array("sms","email","wa")
					),
					'intel'=>array(
						"commodityId" => array(1,3,5,11,21,31),
						"regionId" => array(1,3,5,11,21,31),
						"channel" => array("sms","email","wa")
					)
				);
	   
	   return new JsonResponse(array("status"=>array("code" => 1001, "message" => "success"), "data"=> $result));
    } 
	
	public function download(Request $request)
    {      
		$resourceType = "app";
		$user_id = 0;
		
		$result = array();	
		if($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		$section='';
		if($request->has('section'))
			$section = strtolower($request->input('section'));
		
		if($request->has('company'))
			$company = strtolower($request->input('company'));
		
		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.")));
		}
		
		switch($section){
			case "report":
				if($request->has('itemID') && $request->input('itemID')!=''){
					$itemId = $request->input('itemID');
				
					$user = new User($user_id, $this->appType);
					$isAccessible = $user->getTakenServicesByUser($item='reports', $at_place=$resourceType);
					if ($isAccessible==-1) {
						$result['path'] = "https://www.steelmint.com/reports/Iron_Ore_Insight_28_02_2019.pdf";
					}else{
						return new JsonResponse(array("status"=>array("code" => 2100, "message" => "Unauthorized action.")));
					}	
				}				
			break;
			case "tender":
				if($request->has('itemID') && $request->input('itemID')!=''){
					$itemId = $request->input('itemID');
				
					$user = new User($user_id, $this->appType);
					$isAccessible = $user->getTakenServicesByUser($item='tenders', $at_place=$resourceType);
					if ($isAccessible==-1) {
						$result['path'] = "https://www.steelmint.com/reports/Iron_Ore_Insight_28_02_2019.pdf";
					}else{
						return new JsonResponse(array("status"=>array("code" => 2100, "message" => "Unauthorized action.")));
					}	
				}
			break;
			case "invoice":
				if($request->has('invNo') && $request->input('invNo')!=''){
					$inv_no=$request->input('invNo');
				
					if($user_id){
						if ($company=='events' || $company=='event') {
							$result['path'] = "https://www.steelmint.com/invoices/multiform/preview.php?inv_no=".$inv_no;
						}else{
							$result['path'] = "https://www.steelmint.com/invoices/preview.php?inv_no=".$inv_no;
						}
					}
					
					$code = 1001;
					$message = "Success";
			
				}else{
					return new JsonResponse(array("status"=>array("code" => 2100, "message" => "Invalid invoice")));
				}
			break;
		}
	
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }
	
	public function alertsListing(Request $request)
    {      
		$result= array();
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
			$source = $request->input('source');//price/insights/tender

			switch($source){
				case "tenders":
				case "tender":
					$result['tender'] = array(
						array(
							'itemId'=>10,
							'title'=>"Iron Ore Tenders",
							'medium'=>array('sms','push','wa')
						),
						array(
							'itemId'=>1,
							'title'=>"Billet Tenders",
							'medium'=>array('sms','push','email')
						)
					);
				break;
				case "prices":
					$result['prices'] = array(
						array(
							'itemId'=>1003,
							'title'=>"Iron Ore prices",
							'medium'=>array('sms','push','wa')
						),
						array(
							'itemId'=>22,
							'title'=>"Billet prices",
							'medium'=>array('sms','push','email')
						)
					);
				break;
			}
			
			$code = 1001;
			$message = "Success";
		}else{
			$code = 2100;
			$message = "Access token is invalid or has expired.";
		}
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }
	
	public function getFAQ()
    {      
	    $result = array();
		include_once "/home/steelmin/public_html/api/storage/v2019/faqs.php";
	    
		$code = 1001;
		$message = "Successfully Saved";
		
	   //, "data"=> $result;
	    return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }
	
	public function addPreferences(Request $request)
    {      
	    $result = array();
	    $accessToken = $request->input('accessToken');
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
			$source = $request->input('source');//"Tender/Insight/Intel/all",

			$itemIds = $request->input('itemIds');
			$memId = $request->input('memId');
			$commodityId = $request->input('commodityId');
			$regionId = $request->input('regionId');
			$channel = $request->input('channel'); //["whatsapp", "fb"]
			switch($source){
				case "price":
				break;
				case "insights":
				break;
				case "tender":
				break;
			}
			
			$code = 1001;
			$message = "Successfully Saved";
		}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 notificationDetails(Request $request)
    {      
		$result = array();
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
		}
		$source = strtolower($request->input('source'));
		
		$result = array(
					'prices'=>array(
						array(
							"title" => "Prices of Ex-Mines Orissa India, falls down $100",
							"id"	=> 1001,
							'description_type'	=> ""
						),
						array(
							"title" => "Prices of NMDC, India, up $1000",
							"id"	=> 1002,
							'description_type'	=> ""
						),
					),
					'tenders'=>array(
						array(
							"title" => "tenders of Ex-Mines Orissa India, falls down $100",
							"id"	=> 101,
							'description_type'	=> ""
						),
						array(
							"title" => "tenders of NMDC, India, up $1000",
							"id"	=> 102,
							'description_type'	=> ""
						),
					),
					'insight'=>array(
						array(
							"title" => "insight : Ex-Mines Orissa India, falls down $100",
							"id"	=> 101,
							'description_type'	=> ""
						),
						array(
							"title" => "insight : NMDC, India, up $1000",
							"id"	=> 102,
							'description_type'	=> ""
						),
					),
					'intel'=>array(
						array(
							"title" => "intel 1",
							"id"	=> 101,
							'description_type'	=> ""
						),
						array(
							"title" => "tenders intel",
							"id"	=> 102,
							'description_type'	=> ""
						),
					)
				);
	   
	   return new JsonResponse(array("status"=>array("code" => 1001, "message" => "success"), "data"=> $result));
    }
	
	public function showPackageDetails(Request $request)
    {      
		$resourceType = "app";
		$user_id = 0;
		
		$result = array();	
		if($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		$section='';
		if($request->has('section'))
			$section = strtolower($request->input('section'));
		else
			return new JsonResponse(array("status"=>array("code" => 2100, "message" => "section type missing.")));
		
		
		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///
		$activePackegesIDs=array();
		if($user_id){
			$user = new User($user_id, $this->appType);
			$activePackeges = $user->accessiblePackagesToMemid($user_id);	
				
			$activePackegesIDs = $activePackeges['IDs'];
			///print_r($activePackegesIDs);	
		}
		
		$currencyName='';
		if($request->has('currencyName'))
			$currencyName = strtolower($request->input('currencyName'));
		//include_once "/home/steelmin/public_html/api/storage/v2019/package_details.php";
		$result = \ApiQuery::getPackagesRows($user_id, $section, 'Active', $resourceType, $activePackegesIDs, $this->appType, $currencyName);
		//, "data"=> $result;
		return new JsonResponse(array("status"=>array("code" => 1001, "message" => "success"), "data"=> $result));
	}
	
	public function savePersonalDetails(Request $request)
    {      
	    $result = array();
	    $accessToken = $request->input('accessToken');
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
			$memId = $request->input('memId');
			
			$code = 1001;
			$message = "Successfully Saved";
		}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 getloginSuggestedPackage(Request $request){
		$place = strtolower($request->input('place'));
		$result =array(
				
				"suggestedPackage"=>array(
					"packageId" => "2",
					"packageName" => "Standard",
					"whatYouGet" => array("Prices", "Tenders", "Newsletters"),
					"period" => array(
								array(
									"name" => "1 year",
									"isDisabled" => "Y",
									"amountPerMonth" => 4999.0,
									"billed_text" => "Billed Anually",
									"currencyConvertor"=> array(
										"USD"=>array(
											"symbol" => "$",
											"code" => 'USD',
											"amount" => 110.00
										),
										"INR"=>array(
											"symbol" => "₹",
											"code" => 'INR',
											"amount" => 6999.00
										)
									)
								)
					),
					"isActivePackage" => "Y",
					"minimumMagazineYear" => "2019"
				)
			);
		$code = 1001;
		$message = "success";	
		return new JsonResponse(array("status"=>array("code" => $code , "message" => $message), "data"=> $result));
	}
	
	public function getLoginDetails(Request $request)
    {
		$result = array();	
		$resourceType = "APP";
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		if($resourceType=='web'){
			$remeberme = strtolower($request->input('remeberme'));
			$password = $request->input('password');
			$email = $request->input('email');
			
			$code = 1001;
			$message = "success";
		}else{
			if ($request->has('email') && $request->input('email')!='') {
				$physicalId = $request->input('physicalId');
				$password = $request->input('password');
				$email = $request->input('email');
				
				include_once "/home/steelmin/public_html/api/storage/v2019/login_details.php";
				$code = 1001;
				$message = "success";
			}else{
				$code = 2100;
				$message = "invalid user.";
			}
		}
		return new JsonResponse(array("status"=>array("code" => $code , "message" => $message), "data"=> $result));
	}
	
	public function getPersonalDetails(Request $request)
    {
		$result = array();
		
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
			$memId = $request->input('memId');
			include_once "/home/steelmin/public_html/api/storage/v2019/personal_details.php";
			$code = 1001;
			$message = "success";
		}else{
			$code = 2100;
			$message = "Access token is invalid or has expired.";
		}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}
	
	//update FCMToken -
	public function updateFCMToken(Request $request)
    {      
	    $result = array();
	    $deviceId = $request->input('deviceId');
		if ($request->has('deviceId') && $request->input('deviceId')!='') {
			$fcmToken = $request->input('fcmToken');//price/insights/tender	
			
			$code = 1001;
			$message = "Token Successfully updated";
		}else{
			$code = 2100;
			$message = "Access token is invalid or has expired.";
		}
		
	   //, "data"=> $result;
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
    }
	
	//register -
	public function register(Request $request)
    {      
	    $result = array();
	    
		$resourceType = "APP";
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		if($resourceType=='web'){
			$result = array(
				"paymentType"=>"sdk/web",
				"paymentName"=>"ios",
				"packageName"=>"Sliver,Reports",
				"paymentUrl"=>"",
				"lastLogId"=>"1234567,1234568",
				"totalAmount"=>5100,
				"amountPayable"=>5200,
				"tax"=>900,
				"discount"=>100,
				"currency"=>'INR',				
			);
			$code = 1001;
			$message = "Successfully Register";
		}else{
			$deviceId = $request->input('deviceId');
			if ($request->has('deviceId') && $request->input('deviceId')!='') {
				$fcmToken = $request->input('fcmToken');//price/insights/tender	
				$userDetails = $request->input('userDetails');//price/insights/tender	
				$packageDetails = $request->input('packageDetails');//price/insights/tender	
				
				$result = array(
					"paymentType"=>"sdk/web",
					"paymentName"=>"ios",
					"packageName"=>"Sliver,Reports",
					"paymentUrl"=>"",
					"lastLogId"=>"1234567,1234568",
					"totalAmount"=>5100,
					"amountPayable"=>5200,
					"tax"=>900,
					"discount"=>100,
					"currency"=>'INR',				
				);
				$code = 1001;
				$message = "Successfully Register";
			}else{
				$code = 2100;
				$message = "Access token is invalid or has expired.";
			}
		}
		
	   //, "data"=> $result;
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }
	
	public function sendOTP(Request $request)
    {      
	    $result = array();
	    $mobileNumber = $request->input('mobileNumber');
		if ($request->has('mobileNumber') && $request->input('mobileNumber')!='') {
			$mobileNumber = $request->input('mobileNumber');//price/insights/tender	
			
			$code = 1001;
			$message = "OTP Sent";
		}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 verifyOTP(Request $request)
    {      
	    $result = array();
	    $deviceId = $request->input('deviceId');
		if ($request->has('deviceId') && $request->input('deviceId')!='') {
			$memid = $request->input('memid');//price/insights/tender	
			
			$code = 1001;
			$message = "Successfully verified";
		}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 resetPassword(Request $request)
    {      
	    $result = array();
	    $accessToken = $request->input('accessToken');
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
			$oldPassword = $request->input('oldPassword');//oldPassword
			$newPassword = $request->input('newPassword');
			$memId = $request->input('memId');//memId
			
			if($oldPassword==$newPassword){
				$code = 2001;
				$message = "Invalid  User";
			}else{
				$code = 1001;
				$message = "Successfully updated";
			}
		}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 forgotPassword(Request $request)
    {      
	    $result = array();
	    $accessToken = $request->input('accessToken');
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
			$uemail = $request->input('email');//oldPassword
			
			if($uemail==''){
				$code = 2001;
				$message = "Invalid  User";
			}else{
				$code = 1001;
				$message = "Successfully Sent, please check your register email";
			}
		}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 mapGo(Request $request){
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		$dataType = strtolower($request->input('dataType'));
		$result = array();	
		if($resourceType=='web'){
			$code = 1001;
			$message = "Success";
			if ($request->has('memid') && $request->input('memid')!=''){
				
			}else{
				
			}
		}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}

	public function mapSave(Request $request){
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));

		$dataType = strtolower($request->input('dataType'));	
		if($resourceType=='web'){
			$code = 1001;
			$message = "Success";
			if ($request->has('memid') && $request->input('memid')!=''){
				
			}else{
				
			}
		}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}
	
	

	public function mapDownload(){
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		$dataType = strtolower($request->input('dataType'));
		if($resourceType=='web'){
			$code = 1001;
			$message = "Success";
			if ($request->has('memid') && $request->input('memid')!=''){
				
			}else{
			
			}
		}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}
	
	public function mapSelected(Request $request){
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		$dataType = strtolower($request->input('dataType'));
		if($resourceType=='web'){
			$code = 1001;
			$message = "Success";
			if ($request->has('memid') && $request->input('memid')!=''){
				include_once "/home/steelmin/public_html/api/storage/v2019/web/web_map_sel_post.php";
			}else{
				include_once "/home/steelmin/public_html/api/storage/v2019/web/web_map_sel_post.php";
			}
		}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));   
	}
	
	public function search(Request $request){
		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		//if($resourceType=='web'){
			$code = 1001;
			$message = "Success";
			include_once "/home/steelmin/public_html/api/storage/v2019/web/web_search.php";
		//}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));   
	}
	
	public function getsubscriptionDetails(Request $request){
		$resourceType = "app";
		$user_id = 0;
		
		$result = array();	
		if($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		$section='';
			
		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///
		$activePackegesIDs=array();
		if($user_id){
			$user = new User($user_id, $this->appType);
			$activePackeges = $user->accessiblePackagesToMemid($user_id);	
				
			//$activePackegesIDs = $activePackeges['IDs'];
			
			$logs = DB::table('tbl_reg_logs')
					->leftJoin('tbl_packages', 'tbl_packages.id', '=', 'tbl_reg_logs.selected_products')
					->select("tbl_packages.*", 'tbl_reg_logs.id as log_id', 'tbl_reg_logs.startdate', 'tbl_reg_logs.enddate', 'tbl_reg_logs.status', 'tbl_reg_logs.autoRenewal', 'tbl_reg_logs.inv_no')
					->where('tbl_reg_logs.memid' , '=' , $user_id)
					->whereIN('tbl_reg_logs.status' , array('Active', 'Expired'))
					->orderBy('tbl_reg_logs.id' , 'DESC')
					->orderBy('tbl_reg_logs.status' , 'DESC')
					->get();
			
			
			
			foreach($logs as $r){
				///$res['IDs'][$r['id']] = $r['package_order'];
				if($r['status']=='Active'){
					$str = strtotime(date('Y-m-d',strtotime($r['enddate'])))-strtotime($GLOBALS['currDate']);				
					$daysLeft = floor($str/3600/24);
				}else{
					$daysLeft = 0;
				}
				
				$details = array(
					"isActive"    => $r['status']=='Active'?"y":"n",///show renew/upgrade
					"daysLeft"    => (int)$daysLeft,
					"outOf"        => $r['period_name']." months",
					"autoRenewal"=> $r['autoRenewal'],
					"packageId" => (int)$r['id'],
					"packageName" => $r['packageName'],
					"whatYouGet" => explode(',',$r['whatYouGet']),
					"amountPerMonth" => (double)$r['amountPerMonth'],                    
					"currency"=> $r['currency_code'],                    
					"invNo"=> $r['inv_no'],                    
					"activatedBy"=> "SteelMint"
				);
				//print_r($details);
				if($r['licenses_cnt']>1){
					
					$moreLogs = DB::table('tbl_reg_logs')
						->select("tbl_reg_logs.memid", 
								"tbl_members.name",
								DB::raw('(CASE WHEN send_email_to = "R" THEN email ELSE addtnl_email END) AS emailId'),
								DB::raw('(CASE WHEN send_sms_to = "R" THEN phoneno ELSE alternate_mob_no END) AS contacNo')
								)
						->leftJoin('tbl_members', 'tbl_members.id', '=', 'tbl_reg_logs.memid')
						->where('tbl_reg_logs.parent_id' , '=' , $r['log_id'])
						->whereIN('tbl_reg_logs.status' , array('Active', 'Expired'))
						->get();
					
					foreach($moreLogs as $mr){
						$details["licensesInformation"][] = array(
														"memId" => $mr['memid'],
														"name" => $mr['name'],
														"emailId" => $mr['emailId'],
														"contacNo" => $mr['contacNo']                     
													);
					}
					
				}
				
				$result['packageDetails'][]  = $details;
			}
			
			
			
			
			$code = 1001;
			$message = "Success";
			//include_once "/home/steelmin/public_html/api/storage/v2019/web/subcriptionDetails.php";
			
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));   
		}else{
			return new JsonResponse(array("status"=>array("code" => 2100, "message" => "Unauthorized action.")));
		}			
	}
	
	public function getAnnouncement(Request $request){
			$result = array(
				array(
						"date"=> "18-Mar-2018",
						"comodity" => 3,
						"type" => "correction",
						"title" => "text",
						"download" => "link"
					),
				array(
						"date"=> "18-Mar-2018",
						"comodity" => 3,
						"type" => "holiday",
						"title" => "15th aug",
						"download" => "link"
					)
			);

		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		//if($resourceType=='web'){
			$code = 1001;
			$message = "Success";
			
		//}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));   
	}
	
	public function getMessgeBoard(Request $request){
			$result = array(
				array(
						"type"=> "18-Mar-2018",					
						"message" => "general message 1",
						"url" => "url"
					),
				array(
						"type"=> "18-Mar-2018",					
						"message" => "general message 2",
						"url" => "url"
					)
			);

		$resourceType = "APP";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		//if($resourceType=='web'){
			$code = 1001;
			$message = "Success";
			
		//}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));   
	}
}
