<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Action\StplCommonController;
use App\Http\Controllers\Auth\authentication;
use Input;
use Validator;
use DB;

class histDataController extends Controller
{
   public function getHistoricalDataList(Request $request){
       //die('he');
      $rule = [
            'resourceType' => 'required|in:Web,web',
            'requiredFor' => 'required|in:statistics,prices'
          ];
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{
       	    $objCommon = new StplCommonController;
			$requiredFor = trim($request->requiredFor);
			$data = $objCommon->historicalGetData($requiredFor);
			$status = 1001;
			$message= "success";
			$getresponce = $this->innerResponceHander($data,$status,$message);
			if($request->debugMode=='true'){
				$table = array('tbl_historical_landing_commodity_wise','tbl_historical_landing_region_wise');
				$requreParam = array(
				    'resourceType' => 'required|in:Web,web',
                    'requiredFor' => 'required|in:statistics,prices'
				  );
				$getresponce['debugMode']['usedtable'] = $table;
				$getresponce['debugMode']['argumentDetail'] = $requreParam;
			}       
			return response()->json($getresponce);
       }
   }

   public function saveHistoricalData(Request $request){
   	   $rule = [
            'resourceType' => 'required|in:Web,web',
            'requiredFor' => 'required|in:statistics,prices',
            'type' => 'required|in:regionWise,commodityWise',
            'checkedIdList' => 'required|string',
            'fromDate' => 'required|string',
            'toDate'=> 'required|string',
            'memid' => 'required',
            'listName'=> 'required|string'
          ];
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{
           $objCommon = new StplCommonController;
       	   $checkListName = $objCommon->checkListNameExist($request->all());
       	   if($checkListName=='true'){
              $data = $objCommon->saveHistoricalDataList($request->all());
             if($data=='true'){
             	$data='list Hash been Saved';
             	$status = 1001;
				$message= "success";
				$getresponce = $this->innerResponceHander($data,$status,$message);
				if($request->debugMode=='true'){
					$table = array('tb_historical_data_saved_list');
					$requreParam = array(
					   'resourceType' => 'required|in:Web,web',
			            'requiredFor' => 'required|in:statistics,prices',
			            'type' => 'required|in:regionWise,commodityWise',
			            'checkedIdList' => 'required|string',
			            'fromDate' => 'required|string',
			            'toDate'=> 'required|string',
			            'memid' => 'required',
			            'listName'=> 'required|string'
					  );
					$getresponce['debugMode']['usedtable'] = $table;
					$getresponce['debugMode']['argumentDetail'] = $requreParam;
				}       
				return response()->json($getresponce);
             }

       	   }else{
       	   	$status = array(
	              'status' => 2001,
	              'message' => 'ListName Already Registered!'
	          	);
	           return response()->json($status);
       	   }
       }
   }


   public function saveRecentHistoricalData(Request $request){
   	  $rule = [
            'resourceType' => 'required|in:Web,web',
            'memid' => 'required'
          ];
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{
         $objCommon = new StplCommonController;
       	 $data = $objCommon->getSavedHistoricaldataList($request->all());
       	 $status = 1001;
		$message= "success";
		$getresponce = $this->innerResponceHander($data,$status,$message);
		if($request->debugMode=='true'){
			$table = array('tb_historical_data_saved_list');
			$requreParam = array(
			    'resourceType' => 'required|in:Web,web',
                'memid' => 'required'
			  );
			$getresponce['debugMode']['usedtable'] = $table;
			$getresponce['debugMode']['argumentDetail'] = $requreParam;
		}       
		return response()->json($getresponce);
       }
   }

   public function downloadHistoricalData(Request $request){
   	   $rule = [
            'resourceType' => 'required|in:Web,web',
            'requiredFor' => 'required|in:statistics,prices',
            'type' => 'required|in:regionWise,commodityWise',
            'checkedIdList' => 'required|string',
            'fromDate' => 'required|string',
            'toDate'=> 'required|string',
          ];
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{
       	 $objCommon = new StplCommonController;
       	 $data = $objCommon->downloadHistoricaldataList($request->all());
       	 $status = 1001;
			$message= "success";
			$getresponce = $this->innerResponceHander($data,$status,$message);
			if($request->debugMode=='true'){
				$table = array('tb_historical_data_saved_list');
				$requreParam = array(
				    'resourceType' => 'required|in:Web,web',
		            'requiredFor' => 'required|in:statistics,prices',
		            'type' => 'required|in:regionWise,commodityWise',
		            'checkedIdList' => 'required|string',
		            'fromDate' => 'required|string',
		            'toDate'=> 'required|string',
				  );
				$getresponce['debugMode']['usedtable'] = $table;
				$getresponce['debugMode']['argumentDetail'] = $requreParam;
			}       
			return response()->json($getresponce);
       }
   }

   public function removeFromList(Request $request){
   	  $rule = [
            'resourceType' => 'required|in:Web,web',
            'itemId' => 'required',
            'memid'=> 'required'
          ];
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{
       	 //$id = trim($request->itemId);
       	 $objCommon = new StplCommonController;
       	 $data = $objCommon->removeItemFromList($request->all());
       	 $status = 1001;
			$message= "success";
			$getresponce = $this->innerResponceHander($data,$status,$message);
			if($request->debugMode=='true'){
				$table = array('tb_historical_data_saved_list');
				$requreParam = array(
				  'resourceType' => 'required|in:Web,web',
                   'itemId' => 'required',
                   'memid'=> 'required'
				  );
				$getresponce['debugMode']['usedtable'] = $table;
				$getresponce['debugMode']['argumentDetail'] = $requreParam;
			}       
			return response()->json($getresponce);

       	}
   }

   public function getListForEdit(Request $request){
   	  $rule = [
            'resourceType' => 'required|in:Web,web',
            'itemId' => 'required',
            'memid'=> 'required'
          ];
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{
       	 $objCommon = new StplCommonController;
       	 $data = $objCommon->getItemFromList($request->all());
       	 $status = 1001;
			$message= "success";
			$getresponce = $this->innerResponceHander($data,$status,$message);
			if($request->debugMode=='true'){
				$table = array('tb_historical_data_saved_list');
				$requreParam = array(
				  'resourceType' => 'required|in:Web,web',
                   'itemId' => 'required',
                   'memid'=> 'required'
				  );
				$getresponce['debugMode']['usedtable'] = $table;
				$getresponce['debugMode']['argumentDetail'] = $requreParam;
			}       
			return response()->json($getresponce);

       }
   }

   public function ListEditSave(Request $request){
   	  $rule = [
            'resourceType' => 'required|in:Web,web',
            'itemId' => 'required',
            'memid'=> 'required',
            'parent_id' => 'required',
            'fromDate' => 'required',
            'toDate' => 'required'
          ];
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{
	       	 $objCommon = new StplCommonController;
	       	 $data = $objCommon->editSaveList($request->all());
	       	 $status = 1001;
			$message= "success";
			$getresponce = $this->innerResponceHander($data,$status,$message);
			if($request->debugMode=='true'){
				$table = array('tb_historical_data_saved_list');
				$requreParam = array(
				   'resourceType' => 'required|in:Web,web',
		            'itemId' => 'required',
		            'memid'=> 'required',
		            'parent_id' => 'required',
		            'fromDate' => 'required',
		            'toDate' => 'required'
				  );
				$getresponce['debugMode']['usedtable'] = $table;
				$getresponce['debugMode']['argumentDetail'] = $requreParam;
			}       
			return response()->json($getresponce);
       }
   }


   public function innerResponceHander($data,$status,$message){
    	 $status = array(
               'status' => $status,
               'message' => $message 
    	 	);
    	   	 $responce = array(
             'data' => $data,
             'status' => $status
    	 	);
    	 return $responce;
    }



    public function checkInputisValid($rule,$reqx){
       $trimmed = array_map('trim', $reqx);
       $messages = [
           'resourceType.required' => 'resourceType required!',
        ];  
        $validate = Validator::make($trimmed, $rule,$messages);
        if ($validate->fails()) {  
            $validationFailedArray = array();
            $validationFailedArray['status'] = array(
               'code' => 2001, 
                'messages' => 'Invalid input.'
               //'message' => $validate->errors()->first()
              );
          return $validationFailedArray;
      }
    }
}


?>