<?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 TabularController extends Controller

{

  /*it used to show tabuler filter according selection of datatype */
	public function getTabularFilter(Request $request){

        $trimmed = array_map('trim', $request->all());

        $validate = Validator::make($trimmed, [

              'resourceType' => 'required|in:Web,web',

              'requireData' => 'required|in:tabularFilter',

              'requiredFor' => 'required|in:statistics,price',

              'dataType' => 'required'

          ]);

        if ($validate->fails()) {    

			return response()->json($validate->messages(), 200);

		}

		$datatype = trim($request->dataType);
		$appType = 'SM';
		if($request->has('filters')){
			 $filter = $request->input('filters');
			 
			 if('' != $filter['app_type']){
					$appType = 'CM';
					$datatype = $datatype.'_not_reqd';
			 }
			 //if( isset($filter['selected_market']) && $filter['selected_market'] != '' )
			  //  $market = $filter['selected_market'];
		}

        $requiredFor = trim($request->requiredFor);
    
        if($request->resourceType == 'Web'){        
    
            $objCommon = new StplCommonController;
            //echo $datatype . " -- " . $requiredFor;
            $checkRecordExist = $objCommon->checkDatatypeExistTabular($datatype,$requiredFor);
    
            if($checkRecordExist!='true'){
              $status = array(
                  'status' => 10004,
                  'message' => 'dataType not Found!'
                );
    
               return response()->json($status);
               exit;
            }        
    
    		$memid = 0;
    		$table = array('tbl_stats_datatype_header_list_bm','tbl_stats_selection_compare_bm');   
    
            $objCommon = new StplCommonController;
           
            $dataArray = $objCommon->getTabularFilter($datatype, $appType,$memid, $request->selected_market);
            $status = 1001;
            
            $message= "success";
            $getresponce = $this->innerResponceHander($dataArray,$status,$message);     

    		return response()->json($getresponce);
    	}else if($request->resourceType == 'APP') { }
    }
	
    public function getTabularFilterOld(Request $request){

        $trimmed = array_map('trim', $request->all());

        $validate = Validator::make($trimmed, [

              'resourceType' => 'required|in:Web,web',

              'requireData' => 'required|in:tabularFilter',

              'requiredFor' => 'required|in:statistics,price',

              'dataType' => 'required'

          ]);

        if ($validate->fails()) {    

        return response()->json($validate->messages(), 200);

    }

		$datatype = trim($request->dataType);
		$appType = 'SM';
		if($request->has('filters')){
			 $filter = $request->input('filters');
			 
			 if('' != $filter['app_type']){
					$appType = 'CM';
					$datatype = $datatype.'_not_reqd';
			 }
			 
		}


    

    $requiredFor = trim($request->requiredFor);

    if($request->resourceType == 'Web'){

        

        $objCommon = new StplCommonController;

        $checkRecordExist = $objCommon->checkDatatypeExistTabular($datatype,$requiredFor);



        if($checkRecordExist!='true'){

          $status = array(

              'status' => 10004,

              'message' => 'dataType not Found!'

            );

           return response()->json($status);

           exit;

        }

        

        $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);

        }

        

        $user = new User($memid, $appType);

        $isAccessible =  $user->isAccessible($resourceType='Web', $section='Statistics_Tabular');

	    

	    if($isAccessible!=-1){

              $status = array(

                  'status' => 2001,

                  'message' => 'Not a valid user, no access'

                );

           return response()->json($status);

           exit;

        }



        $table = array('tbl_stats_datatype_header_list_bm','tbl_stats_selection_compare_bm');

       

          

          $objCommon = new StplCommonController;

          $dataArray = $objCommon->getTabularFilter($datatype, $appType,$memid);



          $status = 1001;

          $message= "success";

          $getresponce = $this->innerResponceHander($dataArray,$status,$message);



       

     

        if($request->debugMode=='enable'){

            $getresponce['debugMode']['usedtable'] = $table;

        }       

        

       return response()->json($getresponce);

    } 

      else if($request->resourceType == 'APP') {



      }

       

    }





       /*it used to show tabuler data after click go button*/

  public function getTabular(Request $request){



        $objCommon = new StplCommonController;

        $trimmed = array_map('trim', $request->all());



        $valid = [];

        $valid['resourceType'] = 'required|in:Web,web';

        $valid['requireData'] = 'required|in:getTabularData';

        $valid['requiredFor'] = 'required|in:statistics,price';

        $valid['dataType'] = 'required';

        $valid['commodity'] = 'required';

        $valid['region'] = 'required';

        $valid['memid'] = 'required';

        

        if(trim($request->region) != "ALL"){

          $valid['columnNameOfRegion'] = 'required';

        }

        

        if(trim($request->commodity) != "ALL"){

          $valid['columnNameOfCommodity'] = 'required';

        } 


         if(trim($request->region) == "ALL" && trim($request->commodity) == "ALL"){

          $valid['commodity'] = 'required|not_in:All,ALL,all';

        }



        $check = $this->checkInputisValid($valid,$request->all());



        if($check){

          return response()->json($check);

       }else{

        

        $datatype = trim($request->dataType);
		if($request->has('filters')){
			 $filter = $request->input('filters');			 
			 if('' != $filter['app_type']){
					$appType = 'CM';
					$datatype = $datatype.'_not_reqd';
			 }			 
		}
		
        $commodity = trim($request->commodity);

        $commodityColumn = trim($request->columnNameOfCommodity);

        $region = trim($request->region);

        $columnNameOfRegion = trim($request->columnNameOfRegion);

        $requiredFor = trim($request->requiredFor);

        $memid = trim($request->memid);

        //added for maincommodity check in case of subcommodity
        $mainCommodityName ='';
        if($request->has('mainCommodityName'))
            $mainCommodityName = trim($request->mainCommodityName);
		//if called from coalmint + commodity = ALL then show data for only coal
		/* if(trim($request->commodity) == "ALL") {
			if($request->has('filters') && !empty($request->input('filters'))){
				$filters = $request->input('filters');
				
				if( isset($filters['app_type']) && $filters['app_type'] == 'CM'){
				   
					//$commodity = 'coal';
					$datatype = $datatype.'_not_reqd';
				}			
			}	
		}elseif('Others'==trim($request->commodity))
			$datatype = $datatype.'_not_reqd' */;

		
        $objCommon = new StplCommonController;

        $checkRecordExist = $objCommon->checkDatatypeExistTabular($datatype,$requiredFor);

        if($checkRecordExist!='true'){

          $status = array(

              'status' => 10004,

              'message' => 'dataType not Found!'

            );

           return response()->json($status);

           exit;

        }

        
 if($request->debugMode=='true'){
	 echo "dsdss inside".$memid;
	 //dd();
 }
        if($request->resourceType == 'Web'){

            $data = $objCommon->getTabularData($requiredFor,$datatype,$commodity,$commodityColumn,$region,$columnNameOfRegion,$memid,$appType,$mainCommodityName);

        } else if($request->resourceType == 'APP') {



        }

        if($data){

              $status = 1001;

              $message = 'success';

              $getresponce = $this->innerResponceHander($data,$status,$message);

          } else {

            $status = 10004;

            $message = 'Data not Found';

          }



          $getresponce = $this->innerResponceHander($data,$status,$message);

          if($request->debugMode=='true'){

                $table = array('tbl_stats_datatype_header_list_bm,tbl_stats_datatype_freq','tbl_stats_selection_compare_bm');

                $getresponce['debugMode']['usedtable'] = $table;

                $getresponce['debugMode']['argumentDetail'] = $valid;

          }  



          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;

      }

    }

}





?>
