<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Http\Requests;
use Charts;
use Auth;
use Khill\Lavacharts\Lavacharts;
use Khill\Lavacharts\Lavacharts as Lava;
use App;
//use Khill\Lavacharts\Laravel\LavachartsFacade as Lava;



class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {   
           $tnam=$request->session()->get('session_tname');
           $fnam= $request->session()->get('session_fname');
             if(($tnam==="") &&($fnam==="") )
               {
                  $request->session()->put('session_tname',"Tapal");
                  $request->session()->put('session_fname',"File");

               }
             else
               {
               $gcon=\App\Gcon::orderBy('updated_at','desc')->first();
               $tn=$gcon->tname;
               $fn=$gcon->fname;
               $request->session()->put('session_tname',"$tn");
               $request->session()->put('session_fname',"$fn");
            }


        ///////////////////////////////////////////////////////////////////////////////////////////////////////////
        $userid = Auth::user()->id;

        $seat=\App\User_Seat::where('user_id',$userid)->with('seat')->get();
        
        $currentseat=\App\User_Seat::where('user_id',$userid)->first();
        $currentseatid=$currentseat->seat_id;

        if(!$request->session()->has('session_seat'))
        {
          
           $request->session()->put('session_seat',$currentseatid);
        }
        else
        {
            

        }

       /////// ///GLOBAL CONFIGURATIONS FOR TAPAL AND FILE
        $gcon=\App\Gcon::orderBy('updated_at', 'desc')->first();
              $tnam=$gcon->tname;
              $fnam=$gcon->fname;
//checking for session_tnameand fname
              if (($request->session()->has('session_tname')) &&($request->session()->has('session_fname')))
                    {
                        $request->session()->put('session_tname',$tnam);
                        $request->session()->put('session_fname',$fnam);
                    }
             else {
                        $request->session()->put('session_tname',"Tapal");
                        $request->session()->put('session_fname',"File");

               }




       // return $request->session()->get('session_seat');
           
        $seatcount=\App\User_Seat::where('user_id',$userid)->with('seat')->count();

           // Bar chart for FILES//////////

        
       
        //dd($us_arr);
        $filecount=0;
        $readfilecount=0;
        $currentseatid=$request->session()->get('session_seat');
           $seatid_new = $currentseatid;
           $filecount=$filecount + \App\File_forward::where('to_seat',$seatid_new)->where('active_flag',1)->count();
           $readfilecount=$readfilecount + \App\File_forward::where('to_seat',$seatid_new)->where('active_flag',1)->where('read_flag',1)->count();
         
         
        //$lava = new Lavacharts;

        $reasons = \Lava::DataTable();
         
      
        
        $reasons->addStringColumn('Status')
                ->addNumberColumn('Number')
                ->addRow(['Read Files', $readfilecount])
                ->addRow(['Total Files', $filecount]);
                
               
                
        
        \Lava::BarChart('starr', $reasons, [
            'title'  => 'Files',
            'is3D'   => true,
            'slices' => [
                ['offset' => 0.2],
                ['offset' => 0.25],
                ['offset' => 0.3]
            ]
        ]);


          // Bar chart for TAPALS/////////////


      
        //dd($us_arr);
        $tapalcount=0;
        $readtapalcount=0;
         $currentseatid=$request->session()->get('session_seat');
           $seatid_new = $currentseatid;
           $tapalcount =$tapalcount + \App\Tapal_forward::whereHas('tapal', function ($query) {
            $query->where('status','=','0');
            })->where('to_seat', $seatid_new)->where('active_flag',1)->with('tapal')->count();

           $readtapalcount =$readtapalcount + \App\Tapal_forward::whereHas('tapal', function ($query) {
            $query->where('status','=','0');
            })->where('to_seat', $seatid_new)->where('active_flag',1)->where('read_flag',1)->with('tapal')->count();
           //$tapalcount=$tapalcount + \App\Tapal_forward::where('to_seat',$seatid_new)->where('active_flag',1)->count();
           //$readtapalcount=$readtapalcount + \App\Tapal_forward::where('to_seat',$seatid_new)->where('active_flag',1)->where('read_flag',1)->count();
         
         
        //$lava = new Lavacharts;

        $reasons1 = \Lava::DataTable();
         
      
        
        $reasons1->addStringColumn('Status')
                ->addNumberColumn('Number')
                ->addRow(['Read Tapals', $readtapalcount])
                ->addRow(['Total Tapals', $tapalcount]);
                
               
                
        
        \Lava::BarChart('starr1', $reasons1, [
            'title'  => 'Tapals',
            'is3D'   => true,
            'slices' => [
                ['offset' => 0.2],
                ['offset' => 0.25],
                ['offset' => 0.3]
            ]
        ]);
        ////////////////////////////////////////////Bar chart for Tapalcount and subjects
      
 $userid = Auth::user()->id;
 $seatid=$request->session()->get('session_seat');
 $lavatable=\Lava::Datatable();
 $inboxtapalsubarr=\App\Subject::get();
 $lavatable->addStringColumn('Tapals And Subjects')
            ->addNumberColumn('Tapal Count');
               foreach($inboxtapalsubarr as $subjectarr)   
               {
                $subjectid=$subjectarr->id;
                $subjectname=$subjectarr->subject;
                $inboxtapalcnt=0;
                $inboxtapalcnt = \App\Tapal_forward::whereHas('tapal', function ($query)use($subjectid) {
        $query->where('status','=','0')->with('user')->where('subject_id',$subjectid)->with('tapal');
    })->where('to_seat', $seatid)->where('active_flag',1)->with('tapal')->orderBy('created_at','desc')->count(); 
                  //dd($inboxtapalcnt);

        $lavatable->addRow([$subjectname,$inboxtapalcnt]);
}  
\Lava::ColumnChart('subjectchart',$lavatable,[
            'title' => 'Subjects and Tapal Count',
            'titleTextStyle' => [
            'color'    => '#eb6b2c',
            'fontSize' => 10
              ],
              'slices' => [
                ['offset' => 0.2],
                ['offset' => 0.25],
                ['offset' => 0.3]
            ]
]);            
                     
    ///////////////////////////////////////////////////////////////////////////////////////////
         ///////column chart for File subject and counts
      $userid=Auth::user()->id;
      $seatid=$request->session()->get('session_seat');
      $filegroupsarr=\App\File_Group::get();
      $lavatable2=\Lava::Datatable();
      $lavatable2->addStringColumn('Files and File_group')
                 ->addNumberColumn('File count');
      foreach($filegroupsarr as $filegroup)
      {
              $filegroupid=$filegroup->id;
              $filegroupname=$filegroup->file_group;
              $inboxfilegroupcnt=0;
              $inboxfilegroupcnt = \App\File_forward::whereHas('file', function ($query)use($filegroupid) {
              $query->where('file_group_id',$filegroupid)->with('user');
          })->where('to_seat', $seatid)->where('active_flag',1)->with('file')->orderBy('created_at','desc')->count();
               $lavatable2->addRow([$filegroupname,$inboxfilegroupcnt]) ;
          } 

          \Lava::ColumnChart('File_group chart',$lavatable2,[
            'title' => 'File_group and File Count',
            'titleTextStyle' => [
            'color'    => '#eb6b2c',
            'fontSize' => 10
              ]
              
]);               
///////////////////////////////////////////////////////////
////////////////////////column chart for seats and inbox tapals
          $datatable1=\Lava::Datatable();
          $seats=\App\seat::get();
          $datatable1->addStringColumn('Seats and Tapals')
                    ->addNumberColumn('Tapalcount')
                    ->addNumberColumn('Filecount');
          foreach($seats as $seat1)
          {
            $seatid=$seat1->id;
            $seatcode=$seat1->seat_code;
            $seatname=$seat1->seat_name;
            $seatni=$seatcode."::".$seatname;
            $inboxtapalcount= \App\Tapal_forward::whereHas('tapal', function ($query) {
        $query->where('status','=','0')->with('user');
    })->where('to_seat', $seatid)->where('active_flag',1)->with('tapal')->orderBy('created_at','desc')->count();
            $inboxfilecount = \App\File_forward::whereHas('file', function ($query) {
              $query->with('user');
          })->where('to_seat', $seatid)->where('active_flag',1)->with('file')->orderBy('created_at','desc')->count();
            
                    $datatable1->addRow([$seatni,$inboxtapalcount,$inboxfilecount]);
                  }
 \Lava::ColumnChart('pendingtapalchart',$datatable1,[
            'title' => 'Tapal and File Count For Seats',
            'titleTextStyle' => [
            'color'    => '#eb6b2c',
            'fontSize' => 10
              ]
              
]);               
/////////////////column charts for inboxfiles and C




        








       



             
















    //////////////////////////////////////////////////////////////////////////////////////////////               
        return view('home', compact('reasons','reasons1','seat','currentseatid','seatcount'));
    }

    public function sessionpost(Request $request)
    {
            $this->validate($request,[
            
                'seatname' => 'required'   
            
            ]);

            $seatname=$request->seatname;

            $request->session()->put('session_seat',$seatname);

            //return $request->session()->get('session_seat');

            return redirect('home');

    }


}
