<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\menu;
use Schema;
use Session;
//use App\Models\events;
use App\Http\Controllers\library;

class JsonController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
   // public $obj;

   
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */


    public function test1(){

     return "test1 called";

     }
  
  
    function fetch_table_view_json(Request $request){
     
      $obj = new library;
     $sort_value= $request->sort_value;
     $sp_date_picker= $request->sp_date_picker;
     $from_date= $request->header_from_date;
     $till_date= $request->header_till_date;
     if($sp_date_picker=="1")
{
$header_from_date=date("Y-m-d", strtotime($from_date) );
$header_till_date=date("Y-m-d", strtotime($from_date) );
}
else
{
$header_from_date=date("Y-m-d");
$header_till_date=date("Y-m-d");    
}
     /*$FYEAR_ID=$_SESSION['EFYEAR_ID'];
    $TUID=$_SESSION['TUID'];*/
    $FYEAR_ID='1';
    $TUID=session()->get('CID');
    // $results = menu::where('menu_id', $sort_value)->orWhere('sp', $bloodGroup)->get();
     //$sps=menu::where('menu_id', $sort_value)->get('sp','column_conversion');
     
      $rows = DB::table('menus')->where('menu_id', $sort_value)->select('sp','column_conversion')->get()->first();
      
    $arg=1;
     $viewquery=$rows->sp;
     $formatfields=$rows->column_conversion."`";
    $formatfields=substr($formatfields, 0, -1);
    $formatpieces = explode("`", $formatfields);
    $fieldcount=sizeof($formatpieces);
    $sp_q='CALL '.$viewquery."(".$FYEAR_ID.','.$TUID.",'".$header_from_date."','".$header_till_date."','".$arg."')";
   // $sp_q='call  '.$viewquery."(?,?,?,?,?)";
  
    $qr_result=DB::select($sp_q);
    // $data=DB::select($sp_q,array($FYEAR_ID,$TUID,$header_from_date,$header_till_date,$arg));
    $data = json_decode(json_encode($qr_result),true);
    $t_count=sizeof($qr_result);
    $Mydata=array();
 //   dd($data);
   
   $i=1;
    $result=$data[0];
    //dd($result);
  
  if($t_count>0)
    {
         $queryfields=count($result);  
         if($fieldcount!=($queryfields-1))
            {
                unset($formatpieces);
                $formatpieces=array();  
    
                foreach($result as $t_name => $t_value) {    
                    array_push($formatpieces,"S");
                    
                }
    
            }
            $k=1;
             foreach($result as $t_name => $t_value) {    
        //dd($t_name );
                $Mydata[$i][$k]=$t_name;
                $k++;
                }
                    $i++;
 foreach($data as  $d_x =>  $t_row){
                    $k=1;
                        foreach($t_row as  $t_x =>  $t_value)
                         {  
                                   
                            $Mydata[$i][$k]=$obj->convert_format(@$formatpieces[($k-2)],$t_value);
                            $k++;    
                        }
                    
                
               // $t_row = $result;
                $i++;

                
                }

    } else
    {
        $Mydata[][]=array();
    }
           return  json_encode($Mydata);
    }
    public function save_table_settings(Request $request)
    {
        $id=$request->rowid;
        $length='';
    $colname='';
    $format='';
    $t_alighn='';
    foreach($request->wid as $selected)
            {
            $length.=$selected."`";
            }
            
            foreach($request->colname as $selected)
            {
            $colname.=$selected."`";
            }
            
            foreach($request->format as $selected)
            {
            $format.=$selected."`";
            }           
        
            foreach($request->td_alighn as $selected)
            {
            $t_alighn.=$selected."`";
            }
            
            
            $length=substr($length, 0, -1);
            $colname=substr($colname, 0, -1);
            $format=substr($format, 0, -1);
            $t_alighn=substr($t_alighn, 0, -1);
            menu::where('menu_id',$id)->update(array('column_heading' => $colname,'column_width'=>$length,'column_alighn'=>$t_alighn,'column_conversion'=>$format));

            $rows = DB::table('menus')->where('menu_id', $id)->first();
        return redirect()->to('/'.$rows->root.'/'.$id); 
    }
}
