<?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;


class compareController extends Controller
{
  public function ListCommRegion(Request $request){
      $rule = [
        'resourceType' => 'required|in:Web,web',
        'requireData' => 'required|in:regionCommodity',
        'dataType' => 'required'
      ];
      $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{               
          $datatype = trim($request->dataType);
          $objCommon = new StplCommonController;
          $data = $objCommon->comGetCommodityRegion($request->all());
          if($data=='false'){
          $status = array(
            'status' => 10005,
            'message' => 'No Record found!'
          );
          return response()->json($status);
          }
     //$data['headers'] = $objCommon->getHeaderNameAndColumns($request->all());
     $status = 1001;
     $message= "success";
     $getresponce = $this->innerResponceHander($data,$status,$message);
     if($request->debugMode=='true'){
       $table = array('tbl_stats_datatype_header_list_bm','tbl_stats_selection_compare_bm');
       $requreParam = array(
                 'resourceType' => 'required|string|web',
                 'requireData' => 'required|string|regionCommodity',
                 'dataType' => 'required|string',
          );
      $getresponce['debugMode']['usedtable'] = $table;
      $getresponce['debugMode']['argumentDetail'] = $requreParam;
     }       
     return response()->json($getresponce); 
   }  
   }

   public function getHeaderList(Request $request){
         $rule = [
          'resourceType' => 'required|in:Web,web',
          'dataType' => 'required',
          'commodity' => 'required',
          'columnNameOfCommodity' => 'required',
          'region' => 'required',
          'columnNameOfRegion' => 'required',
        ];
       $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{  
          $objCommon = new StplCommonController;
          $data = $objCommon->getHeaderList($request->all());
          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);
          if($request->debugMode=='true'){
          $table = array('tbl_stats_datatype_header_list_bm');
          $requreParam = array(
                 'resourceType' => 'required|in:Web,web',
                'dataType' => 'required',
                'commodity' => 'required',
                'columnNameOfCommodity' => 'required',
                'region' => 'required',
                'columnNameOfRegion' => 'required',
          );
          $getresponce['debugMode']['usedtable'] = $table;
          $getresponce['debugMode']['argumentDetail'] = $requreParam;
          }       
          return response()->json($getresponce); 
       } 

    } 

   public function getComparison(Request $request){
      $rule = [
          'resourceType' => 'required|in:Web,web',
          'itemNumber' => 'required|numeric',
          'dataType'=>'required|string',
          'commodity'=>'required|string',
          'region'=>'required|string',
          'columnNameOfRegion'=>'required|string',
          'columnNameOfCommodity'=>'required|string',
          //'headerColumn'=>'required|string',
          //'headerValue'=>'required|string',
          'fromDate'=>'required|string',
          'toDate'=>'required|string',
          'periodFormat'=>'required|string'
      ];
      $check = $this->checkInputisValid($rule,$request->all());
       if($check){
        return response()->json($check);
       }else{  
          $objCommon = new StplCommonController;
          $item = $request->itemNumber;
          $data['item'.$item] = $objCommon->getDataForCompare($request->all());

          if($data=='false'){
             $status = array(
              'status' => 10005,
              'message' => 'Data Not Found!'
            );
           return response()->json($status);
          }
          //$data['headers'] = $objCommon->getHeaderNameAndColumns($request->all());
          $status = 1001;
          $message= "success";
          $getresponce = $this->innerResponceHander($data,$status,$message);
          if($request->debugMode=='true'){
          $table = array('tbl_stats_datatype_header_list_bm','tbl_stats_selection_compare_bm');
          $requreParam = array(
           'resourceType' => 'required|in:Web,web',
           'itemNumber' => 'required|numeric',
          'dataType'=>'required|string',
          'commodity'=>'required|string',
          'region'=>'required|string',
          'columnNameOfRegion'=>'required|string',
          'columnNameOfCommodity'=>'required|string',
          //'headerColumn'=>'required|string',
          //'headerValue'=>'required|string',
          'fromDate'=>'required|string',
          'toDate'=>'required|string',
          'periodFormat'=>'required|string'
          );
          $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;
      }
    }  

}
