<?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;
//stepLogic
use App\Http\Controllers\Action\StplCommonController;
use Validator;
use Session;

class histDataController extends Controller
{
    
   public function getHistoricalDataList(Request $request){
      $rule = [
            'resourceType' => 'required|in:Web,web',
            'requiredFor' => 'required|in:statistics,price'
          ];
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{
            $objCommon = new StplCommonController;
      $requiredFor = trim($request->requiredFor);
      
      $memid = 0;
        //update memId if getting in POST request
		if($request->memid){
          $memid = trim($request->memid);
        }
	
        if($memid<=0){
            /* $status = array(
                  'status' => 2001,
                  'message' => 'Unauthorized action'
                );
               return response()->json($status); */
        }
		$appType='SM';
		if($request->has('filters')){
			 $filter = $request->input('filters');
			 if(!empty($filter['app_type'])){
					$appType = 'CM';
			 }
		}
        
        $user = new User($memid, $appType);
        $isAccessible =  $user->isAccessible($resourceType='Web', $section='Prices_Historical');
	    
	    /* if($isAccessible!=-1){
              $status = array(
                  'status' => 2001,
                  'message' => 'Not a valid user, no access'
                );
           return response()->json($status);
        } */
	        $data = $objCommon->historicalGetData($requiredFor,$memid, $appType);      
			if($data=='false'){
				$status = array(
				  'status' => 10004,
				  'message' => 'Given Id Not found in the dataBase'
				);
				return response()->json($status);
			}
      $status = 1001;
      $message= "success";
      $getresponce = $this->innerResponceHander($data,$status,$message);
      if($request->debugMode=='true'){
        $table = array('tbl_historical_landing');
        $requreParam = array(
                    'resourceType' => 'required|in:Web,web',
                    'requiredFor' => 'required|in:statistics,price'
          );
        $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,price',
            '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=='false'){
         $status = array(
              'status' => 10004,
              'message' => 'Given Id Not found in the dataBase'
            );
           return response()->json($status);
        }
       	//$data='list Has 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,price',
			            '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' => 10003,
                '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;
		 $moreParam=[];
         $data = $objCommon->getSavedHistoricaldataList($request->all(), $moreParam);
	if($data=='false'){
         $status = array(
              'status' => 10005,
              'message' => 'No Record Found!'
            );
           return response()->json($status);
        }
         $status = 1001;
    $message= "success";
    $getresponce = $this->innerResponceHander($data,$status,$message,$moreParam);
    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,price',
            'type' => 'required|in:regionWise,commodityWise',
            'checkedIdList' => 'required|string',
            'fromDate' => 'required|string',
            'toDate'=> 'required|string',
            ///'memid'=> 'required|string',
          ];
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{
         $objCommon = new StplCommonController;
         $data = $objCommon->downloadHistoricaldataListTest($request->all());
         if($data=='Invalid User Session!'){
			$status = 10004;
			$message= "Invalid User Session!";
		 }else{
			$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,price',
                'type' => 'required|in:regionWise,commodityWise',
                'checkedIdList' => 'required|string',
                'fromDate' => 'required|string',
                'toDate'=> 'required|string',
                'memid'=> 'required|string',
          );
        $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,price',
            'type' => 'required|in:regionWise,commodityWise',
            'checkedIdList' => 'required|string',
            'fromDate' => 'required|string',
            'toDate'=> 'required|string',
            'memid'=> '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,price',
                'type' => 'required|in:regionWise,commodityWise',
                'checkedIdList' => 'required|string',
                'fromDate' => 'required|string',
                'toDate'=> 'required|string',
                'memid'=> '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());
if($data=='false'){
        $status = array(
              'status' => 10004,
              'message' => 'Given Id Not found in the dataBase'
            );
           return response()->json($status);
       }
       $data =  null;
         $status = 1001;
      $message= "record has been Removed";
      $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());
	if($data=='false'){
          $status = array(
              'status' => 10004,
              'message' => 'Given Id Not found in the dataBase'
            );
           return response()->json($status);
         }
         $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',
            '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());
	if($data=='false'){
          $status = array(
              'status' => 10004,
              'message' => 'Given Id Not found in the dataBase'
            );
           return response()->json($status);
         }

      $data=null;
	    $status = 1001;
	    
	$message= "Record Has been updated!";

      $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',
                '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,$moreParam=[]){
       $status = array(
               'status' => $status,
               'message' => $message 
        );
           $responce = array(
             'data' => $data,
             'status' => $status
        );
		if(!empty($moreParam)){
			 $responce = array_merge( $responce,$moreParam);
		}
       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;
      }
    }	
	
	public function getCommodityRegion(Request $request){
       $rule = [
            'resourceType' => 'required|in:Web,web',
            'section' => 'required',
            'memid'=> 'required'
          ]; 
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
			return response()->json($check);
       }else{
         ///$attr = $request->all();
         
		 $objCommon = new StplCommonController;
		 if($request->input('section')=='dashboard'){
            $data = $objCommon->getHistoricalDashboard($request->all());
          }else 
            $data = $objCommon->getHistoricalCommRegion($request->all());
			if($data=='false'){
			  $status = array(
				  'status' => 10004,
				  'message' => 'Given Section Not found'
				);
			   return response()->json($status);
			 }
			$status = 1001;
			$message= "success";
			
			$getresponce = $this->innerResponceHander($data,$status,$message);
			if($request->debugMode=='true'){
				
				//$getresponce['debugMode']['usedtable'] = $table;
				//$getresponce['debugMode']['argumentDetail'] = $requreParam;
			}
			
			return response()->json($getresponce);
       }
	}
	
	public function getSections(Request $request){
          $rule = [
                'resourceType' => 'required|in:Web,web',
                'requireData' => 'required|in:datatype',
                'requiredFor' => 'required|in:statistics,price'
              ];
            $check = $this->checkInputisValid($rule,$request->all());
           if($check){
            return response()->json($check);
           }else{
			
// 			$isCM = '';
// 			if($request->has('filters')){
// 				 $filter = $request->input('filters');
// 				 if(!empty($filter['app_type'])){
// 					$isCM = $filter['app_type'];
// 				 }
// 			}
			
      		$requiredFor = trim($request->requiredFor);
      		$objCommon = new StplCommonController;
      		$data = $objCommon->getDetaTypeName($requiredFor, $isCM, $request->selected_market);
          $datatypes = array();            
          if($data===null){
             $datatypes = $data;
             $status = 10005;
            $message= "No record Found!";
          }else{
             $datatypes['datatype'] = $data;
             $status = 1001;
             $message= "success";
          } 
           $getresponce = $this->innerResponceHander($datatypes,$status,$message);
         if($request->debugMode=='true'){
			$table = array('tbl_stats_datatype_header_list_bm');
			$requreParam = array(
                   'resourceType' => 'required|string|web',
                   'requireData' => 'required|string|datatype',
                   'requiredFor' => 'required|string|statistics,price',
            );
			$getresponce['debugMode']['usedtable'] = $table;
			$getresponce['debugMode']['argumentDetail'] = $requreParam;
         }       
		return response()->json($getresponce);
		}
    }
    
    
    //download price data 
    // 18jan2022
	public function downloadPriceData(Request $request){
		 $rule = [
            'resourceType' => 'required|in:Web,web',
           'checkedIdList' => 'required|string',
            'fromDate' => 'required|string',
            'toDate'=> 'required|string'
            ///'memid'=> 'required|string',
          ];
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{
         $objCommon = new StplCommonController;        
         $data = $objCommon->downloadPricedata($request->all());
         $status = 1001;
		 $message= "success";
		 $getresponce = $this->innerResponceHander($data,$status,$message);
		 return response()->json($getresponce);
       }
	}
	
	
	    //get  dashboard data url
	public function getDashboardUrlPath(Request $request){
		 $rule = [
            'resourceType' => 'required|in:Web,web',           
            'checkedIdList' => 'required|string',
            ///'memid'=> 'required|string',
          ];
        $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{
         $objCommon = new StplCommonController;
         $data = $objCommon->getDashboardUrlPath_func($request->all());
         if($data=='Invalid User Session!'){
			$status = 10004;
			$message= "Invalid User Session!";
		 }else{
			$status = 1001;
			$message= "success";
		 }
        $getresponce = $this->innerResponceHander($data,$status,$message);      
        return response()->json($getresponce);
        } 
    }
}

?>
