<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Controllers\stdClass;

use Auth;
use App;


use Yajra\Datatables\Datatables;

class InboxController extends Controller
{

    //
  protected $childarray=[];
    public function __construct()
    {
        $this->middleware('auth');
    }

//Tapal inbox

public function inbox(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");
            }










      /////////////////////////////////////////////////////////////////////////////////////////////////

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

      //$creation_of_tapal=\App\Tapal::where('id',$id)->get();

     $currentseat=\App\User_Seat::where('user_id',$id)->first();
        $currentseatid=$currentseat->seat_id;

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

    $seatid=$request->session()->get('session_seat');
$office=\App\Office::all();
$department=\App\Department::all();
$section =\App\Section::all();
       //inter office permission
      /*$permissionoffice = \App\Permission::where('permission','Inter office workflow')->first();
      $permission_id=$permissionoffice->id;

      $seatpermcount =\App\Seat_Permission::where('seat_id',$seatid)->where('permission_id',$permission_id)->count();
      if($seatpermcount)
      {
        $office=\App\Office::all();
      }

      //intra office permission
      $permissionoffice1 = \App\Permission::where('permission','Intra office workflow')->first();
      $permissionid=$permissionoffice1->id;

      $seatpermcount1 =\App\Seat_Permission::where('seat_id',$seatid)->where('permission_id',$permissionid)->count();
      if($seatpermcount1)
      {
        $officeseat=\App\Seat::where('id',$seatid)->with('office')->first();
        $officeid=$officeseat->office->id;
        $office=\App\Office::where('id',$officeid)->get();*/
        /*foreach ($currentseatoffice as $currentoffice) 
        {
          $office=$currentoffice->office->office_name;
        }*/
      //}
      /////////////
      //inter dept permission
      /*$permissiondepartment = \App\Permission::where('permission','Inter department workflow')->first();
      $permission_id=$permissiondepartment->id;

      $seatpermcountdept =\App\Seat_Permission::where('seat_id',$seatid)->where('permission_id',$permission_id)->count();
      if($seatpermcountdept)
      {
        $department=\App\Department::all();
      }

      //intra dept permission
      $permissiondepartment1 = \App\Permission::where('permission','Intra department workflow')->first();
      $permissionid=$permissionoffice1->id;

      $seatpermcountdept1 =\App\Seat_Permission::where('seat_id',$seatid)->where('permission_id',$permissionid)->count();
      if($seatpermcountdept1)
      {
        $deptseat=\App\Seat::where('id',$seatid)->with('department')->first();
        $deptid=$deptseat->department->id;
        $department=\App\Department::where('id',$deptid)->get();*/
        /*foreach ($currentseatoffice as $currentoffice) 
        {
          $office=$currentoffice->office->office_name;
        }*/
      //}
      //////////////////
      //inter section permission
     /* $permissionsection = \App\Permission::where('permission','Inter section workflow')->first();
      $permission_id=$permissionsection->id;

      $seatpermcountsection =\App\Seat_Permission::where('seat_id',$seatid)->where('permission_id',$permission_id)->count();
      if($seatpermcountsection)
      {
        $section =\App\Section::all();
      }

      //intra section permission
      $permissionsection1 = \App\Permission::where('permission','Intra section workflow')->first();
      $permissionid=$permissionsection1->id;

      $seatpermcountsect1 =\App\Seat_Permission::where('seat_id',$seatid)->where('permission_id',$permissionid)->count();
      if($seatpermcountsect1)
      {
        $sectseat=\App\Seat::where('id',$seatid)->with('section')->first();
        $sectid=$sectseat->section->id;
        $section=\App\Section::where('id',$sectid)->get();
        /*foreach ($currentseatoffice as $currentoffice) 
        {
          $office=$currentoffice->office->office_name;
        }
      }
      */

      $priority =\App\Priority::all();

      $tapalinboxcount = \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();

      $fileinboxcount = \App\File_forward::where('to_seat', $seatid)->where('active_flag',1)->with('file')->orderBy('created_at','DESC')->count();

      $tapaloutboxcount = \App\Tapal_forward::where('from_seat', $seatid)->with('tapal')->orderBy('created_at','DESC')->count();

      $fileoutboxcount = \App\File_forward::where('from_seat', $seatid)->with('file')->orderBy('created_at','DESC')->count();

      //$tapalinboxcount = \App\Tapal_forward::where('to_seat', $seatid)->where('active_flag',1)->count();
      $tapal_arr = \App\Tapal_forward::where('to_seat', $seatid)->where('active_flag',1)->get();
      foreach ($tapal_arr as $tapal_val) 
      {
          $tapalid=$tapal_val->id;
      }

      //$values = \App\Comments::where('tapal_id',$tapal_arr)->with('seat')->orderBy('created_at','desc')->get();

      $actions = \App\Action::all();

      $comments = \App\Comments::all();

     // $file_forward =\App\File_forward::with('file_group')->get();
     
    
     // $subject =\App\Subject::where('id',$creation_of_tapal->subject_id)->first();

///////////////TAPALROUTE DETAILS:
    $troutarr=[];
   $tapals=\App\Tapal_forward::where('to_seat',$seatid)->where('active_flag',1)->get();
   //dd($tapals);
      foreach($tapals as $tapal)
       { 
          $tapalidforward = $tapal->tapal_id;
          $tapalsubid=$tapal->tapal->subject_id;
          //dd($tapalsub->subject);
        
            $trout=\App\Troute::where('subject_id',$tapalsubid)->get();
            
            $i=0;
               foreach($trout as $trouts)
                   { 
                      $troutid=$trouts->id;
                      $troutename=$trouts->routename; 
                      $troutarr[$i]['id']=$troutid;
                      $troutarr[$i]['name']=$troutename;
                        $i++;


                   } 
          }
          
        //dd($tapalidforward);
      return view('inbox', compact('department','section','tapal_arr','actions','priority','comments','office','tapalinboxcount','fileinboxcount','tapaloutboxcount','fileoutboxcount','troutarr'));

     }

    // modal for old file function
 public function modaloldfile()

   {
      $oldfilearr =[];

      $priorityfile=App\Priority::all();
      $oldfilearr['priority']=$priorityfile;
      //dd($oldfilearr);
      $yr=date("Y");
        $filesearch='%'.$yr;
        $fileobj=\App\File::select('file_no')->where("file_no",'like',$filesearch)->orderBy('id','desc')->first();
        if($fileobj)
        {
            $lastfileno=$fileobj->file_no;

            $filearr=explode("/",$lastfileno);
            $lastfileint=$filearr[1];
            $fileno=$lastfileint+1;
        }
        else
            $fileno=1;
        $fileprint="FILE/".$fileno."/".$yr;

        $oldfilearr['fileno']=$fileprint;

        $file_groups =\App\File_Group::all();
        $oldfilearr['filegroup']=$file_groups;
        //dd($oldfilearr);

        return $oldfilearr;

   }

 // modal for attach new file
 
 public function modalnewfile()

   {
      $newfilearr =[];

      $priorityfile=App\Priority::all();
      $newfilearr['priority']=$priorityfile;
      //dd($newfilearr);
      $yr=date("Y");
        $filesearch='%'.$yr;
        $fileobj=\App\File::select('file_no')->where("file_no",'like',$filesearch)->orderBy('id','desc')->first();
        if($fileobj)
        {
            $lastfileno=$fileobj->file_no;

            $filearr=explode("/",$lastfileno);
            $lastfileint=$filearr[1];
            $fileno=$lastfileint+1;
        }
        else
            $fileno=1;
        $fileprint="FILE/".$fileno."/".$yr;

        $newfilearr['fileno']=$fileprint;

        $file_groups =\App\File_Group::all();
        $newfilearr['file_group']=$file_groups;
        //dd($oldfilearr);

        return $newfilearr;

   }  

//new modal tapal view

  public function modaltapalview($id,Request $request)

   {
        
      $tapal_id= $id;
      $creation_of_tapal=\App\Tapal::where('id',$id)->with('user')->first();
      $tapal_no=$creation_of_tapal->tapal_no;
      $description=$creation_of_tapal->tapal_description;
      $confidential=$creation_of_tapal->confidential;
      $status=$creation_of_tapal->status;
      $tapalcreateduser=$creation_of_tapal->user->name;
      //dd($tapal_no);
      $tapaluserseat=\App\User_Seat::where('user_id',$creation_of_tapal->created_by)->with('seat')->first();
      $tapalseat=$tapaluserseat->seat->seat_name;

      $tapaldetailsarr =[];
      $i=0;
      $tapaldetailsarr['id']=$tapal_id;
       $tapaldetailsarr['tapal_no']=' Tapal No:'." ".$tapal_no."   " .$tapalseat." : " .$tapalcreateduser;
       if($confidential==1)
       {
          $conf=', Confidential';
          $tapaldetailsarr['tapal_no']=' Tapal No:'." ".$tapal_no."   " .$tapalseat." : " .$tapalcreateduser .$conf;
       }
       $tapaldetailsarr['tapal_description']=$description;
       if($creation_of_tapal->internal_external == 1)
       {
          $internal_external='Internal Tapal';
       }
       else
       {
          $internal_external='External Tapal';
       }
       $tapaldetailsarr['internal_external']=$internal_external;
       $tapaldetailsarr['title']=$creation_of_tapal->title;
       $tapaldetailsarr['status']=$status;

       $tapalfrwrd=\App\Tapal_forward::where('tapal_id',$tapal_id)->where('active_flag',1)->with('tapal')->first();
       $createdby=$tapalfrwrd->tapal->user->name;
       $createdat=$tapalfrwrd->tapal->created_at->format('d-M-Y h-m-s');
       $forwardedon=$tapalfrwrd->created_at->format('d-M-Y h-m-s');
       $forwardedby=$tapalfrwrd->from_seat;
         if(!$forwardedby)
          $forwardedby="Self Generated";
        else
        {
          $forwardseatobj=\App\Seat::where('id',$forwardedby)->first();
          $forwardedby=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
        
        }

        $tapaldetailsarr['from_seat']=$forwardedby;
        $tapaldetailsarr['created_at']=$forwardedon;
        $tapaldetailsarr['name']=$createdby;
        $tapaldetailsarr['created_at']=$createdat;

        $subject =\App\Subject::where('id',$creation_of_tapal->subject_id)->first();
        $sub=$subject->subject;
        $tapaldetailsarr['subject']=$sub;

        if($creation_of_tapal->mode_of_receipt==0)
            $mode='NA';
        else if($creation_of_tapal->mode_of_receipt==1)
            $mode='Fax';
        else if($creation_of_tapal->mode_of_receipt==2)
            $mode='Email';
        else if($creation_of_tapal->mode_of_receipt==3)
            $mode='By hand';
        else if($creation_of_tapal->mode_of_receipt==4)
            $mode='Post';    

        $tapaldetailsarr['mode_of_receipt']=$mode;

        $prio=App\Priority::where('id',$creation_of_tapal->priority)->first();
        $priority=$prio->priority;
        $tapaldetailsarr['priority']=$priority;

      $attachmentcount =\App\Documents::where('tapal_id',$tapal_id)->count();

      
      $i=0;
      $tapal_attchment =\App\Documents::where('tapal_id', $tapal_id)->get();
      $tapaldocstr='';
      foreach ($tapal_attchment as $tattachment) 
      {
         $tapal_documentname=$tattachment->document_name;
         $path=$tattachment->document_path;
         $tapal_attach_createdat=$tattachment->created_at->format('d-M-Y h:i:s');

         $size='';
         $filepath=storage_path('app/'.$path);
         if(file_exists($filepath)){
          $size=round(filesize($filepath)/1024,1);
         }

         if($tapal_documentname)
         {
            
            $tapaldocstr.= "<li><div class='mailbox-attachment-info'><a href='#' class='mailbox-attachment-name'><i class='fa fa-paperclip'></i> $tapal_documentname</a><span class='mailbox-attachment-size'>$size KB<a href='../../storage/app/$path' class='btn btn-default btn-xs pull-right'><i class='fa fa-cloud-download'></i></a></span></div></li>";

            $i++;
         }
      }
      $tapaldetailsarr['document_path']=$tapaldocstr;

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

      $comments_arr = \App\Comments::where('tapal_id', $tapal_id)->with('tapal','seat')->orderBy('created_at','DESC')->get();
  
       $commentstr ='';
       $comments_count = \App\Comments::where('tapal_id', $tapal_id)->with('tapal','seat')->count();
       if($comments_count==0)
       {
            $commentstr .= "<div class='box-comment'><div class='comment-text'><span class='username' id='nocomment'>&nbsp;<span class='text-muted pull-right'></span></span>No Comments added yet !!!</div></div>";
       }

      
      foreach ($comments_arr as $comments_list)
      {
          $created_at=$comments_list->created_at->format('d-M-Y ');
          $tapalid=$comments_list->tapal_id;
          $comment_type=$comments_list->comment_type;
          $comment_description=$comments_list->comment_description;
          $seatname=$comments_list->seat->seat_name;
          $commentseatid=$comments_list->seat_id;
          //dd($tapalid);
          $checkforseatmatch=\App\User_Seat::where('user_id',$userid)->where('seat_id',$commentseatid)->count();
          $currentseat=\App\User_Seat::where('user_id',$userid)->with('seat')->get();
        foreach ($currentseat as $userseat)
          {
            $currentuserseat=$userseat->seat_id;
            //dd($currentuserseat);
            $currenttapalseat=\App\Tapal_forward::where('tapal_id',$tapalid)->where('active_flag',1)->first();
            //foreach ($currenttapalseat as $tapalseat)
            //{
            $seat=$currenttapalseat->to_seat;
            // dd($seat);
            //}
            $seatflag=0;
            if($currentuserseat==$seat)
              {
                 $seatflag=1;
              }

          }


          if(($comment_type==1)&&($checkforseatmatch>0)) 
          {    
            $commentstr .= "<div class='box-comment'><input type='hidden' name='creation_of_tapal' id='creation_of_tapal' value='$id'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname<span class='text-muted pull-right'>$created_at</span></span><font color='red'>$comment_description</font></div></div>";
          }

            if($comment_type==0) 
            {
              
              $commentstr .= "<div class='box-comment'><input type='hidden' name='creation_of_tapal' id='creation_of_tapal' value='$id'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname<span class='text-muted pull-right'>$created_at</span></span><font color='green'>$comment_description</font></div></div>";

            }

            if(($comment_type==2)  && ($comments_list->disgard_flag==0))
            {
              
              $commentstr .= "<div class='box-comment'><form class=form-horizontal action='".url('postit_comment')."'  method='post' > <input type='hidden' name='_token' value=".csrf_token()."><input type='hidden' name='tapal' id='tapal' value='$id'><input type='hidden' name='comment_id' id='comment_id' value='$comments_list->id'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname<span class='text-muted pull-right'>$created_at</span></span><font color='#ad890a'>$comment_description</font></div>";

              if($seatflag==1)
              {
                $commentstr .= "<button type='submit' class='btn btn-default btn-xm'> <span class='glyphicon glyphicon-trash'></span> Trash </button></form></div> "; 
              }

            }
            
           
        }

        $tapaldetailsarr['comment_description']=$commentstr;

        //dd($tapaldetailsarr);






            $actions_arr = \App\Tapal_action::where('tapal_id', $id)->with('tapal','seat','action')->orderBy('created_at','DESC')->get();
 

            $actionstr ='';

              $action_count = \App\Tapal_action::where('tapal_id', $id)->with('tapal','seat','action')->count();
             if($action_count==0)
             {
                  $actionstr .= "<div class='box-comment'><div class='comment-text'><span class='username'>&nbsp;<span class='text-muted pull-right'></span></span>No Actions added yet !!!</div></div>";
             }

            foreach ($actions_arr as $actions_list)
            {
                $created_at=$actions_list->created_at->format('d-M-Y');
                $tapalid=$actions_list->id;
                //$action_type=$actions_list->action->actions;
                $action_description=$actions_list->action_description;
                $seatname=$actions_list->seat->seat_name;
                //$actionseatid=$actions_list->seat_id;
                
               
                if($action_description) 
                {    
                  $actionstr .= "<div class='box-comment'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname<span class='text-muted pull-right'>$created_at</span></span>$action_description</div></div>";
                  }


                 
              }  


              $tapaldetailsarr['action_description']=$actionstr;


            $tapalattachedtofile_arr = \App\File_reference::where('tapal_id', $tapal_id)->with('file')->first();

              $tapaldetailsarr['file_id']=$tapalattachedtofile_arr;
              $seatid=$request->session()->get('session_seat');
              $act_flg_status = 0;
              $match=[['to_seat',$seatid],['tapal_id',$id]];
              //print_r($match);
              $current_tapaluser=\App\Tapal_forward::where($match)->get();
              foreach($current_tapaluser as $tapvallist)
              {
                $active_flag = $tapvallist->active_flag;
                if($active_flag == 1)
                {
                  $act_flg_status = 1;
                }

              }

              $tapaldetailsarr['active_flag']=$act_flg_status;

              $readflag="-1";
              $fromseat = \App\Tapal_forward::where('from_seat', $seatid)->where('tapal_id',$tapal_id)->get();
              foreach ($fromseat as $togetseat) 
              {
                $toseat=$togetseat->to_seat;
                //dd($toseat);
                $readflag=$togetseat->read_flag;
                
                //dd($flagread);
              }
              $tapaldetailsarr['read_flag']=$readflag;

              $user_comment_flag='';
              $tapal_comment_last = \App\Comments::where('tapal_id',$tapal_id)->orderBy('created_at','DESC')->first();
              if(isset($tapal_comment_last)!='')
              {
                  $userid_last_tapalcomment = $tapal_comment_last->updated_by;
                  if($userid_last_tapalcomment == $userid)
                  {
                      $user_comment_flag = 1;
                  }
              }
              else
              {
                  $user_comment_flag=0;
              }
              //$note="Forward button will be enabled,if alteast one note is entered by the current seat.";

              $tapaldetailsarr['user_comment_flag']=$user_comment_flag;
              //$tapaldetailsarr['note']=$note;
              //dd($user_comment_flag);
              $charray=$this->getchildseats1($seatid);
               $tapalseat = \App\Tapal_forward::where('tapal_id',$tapal_id)->where('active_flag',1)->first();
         $tapaltoseat=$tapalseat->to_seat;
              $enablecapture="";
              if(in_array($tapaltoseat, $charray))
              {
                $enablecapture=1;
              }
              else
              {
                $enablecapture=0;
              }
              $tapaldetailsarr['enablecapture']=$enablecapture;
              //////////////
            $tapalconfflag=0;
             $newflag="";
            //echo "$tapaltoseat....$seatid_new";
            if($tapaltoseat!=$seatid)
            {
              //check if loggedin user is in fileforward fromseat for this fileid
              $tapalckecking = \App\Tapal_forward::where('tapal_id',$tapal_id)->where('from_seat',$seatid)->count();
              if($tapalckecking==0)
              {
               $tapalconf = \App\Tapal::where('id',$tapal_id)->where('confidential',1)->count();
               if($tapalconf>0)         
                  $tapalconfflag=1;
              }
               
            }
      //return 4;
             if($tapalconfflag==1)
                return view('tapaldenied', compact('tapalconfflag','tapaltoseat','seatid'));
              //if the loggedin user doesnt comes in the history of tapal
            $tapalcondition = \App\Tapal_forward::where('tapal_id',$tapal_id)->where('to_seat',$seatid)->count();
            //dd($tapalcondition);
           // return 5;
            if($tapalcondition==0)
            {
              $tapalconfflag=1;
            }
            
              $charray=$this->getchildseats1($seatid);
              
              if(in_array($tapaltoseat, $charray)||($tapalconfflag==0))
              {
                $newflag=1;
              }
              else
              {
                $newflag=0;
              }
              if($tapaltoseat==$seatid)
              {
                $newflag=1;
              }
              if($newflag==0)
                return view('tapaldenied', compact('newflag','tapaltoseat','seatid'));
            

              $priorityfile=App\Priority::all();





     return $tapaldetailsarr;

     }

public function move_to_inbox(Request $request)

        {

            $id=$request->creation_of_tapal;
           // dd($id);
            $userid = Auth::user()->id;
                     
            $seat = \App\Tapal_forward::where('tapal_id',$id)->get();
            foreach ($seat as $togetseat) 
            {
              $toseat=$togetseat->to_seat;
              //dd($toseat);
              $fromseat=$togetseat->from_seat;  
              //dd($fromseat);
              $delete=\App\Tapal_forward::where('tapal_id',$id)->where('from_seat',$fromseat)->where('to_seat',$toseat)->where('read_flag',0)->delete();
            }

             $update = \App\Tapal_forward::where('tapal_id',$id)->where('to_seat',$fromseat)->where('read_flag',0)->update(['read_flag' => 1]);
             $updateflag = \App\Tapal_forward::where('tapal_id',$id)->where('to_seat',$fromseat)->where('active_flag',0)->update(['active_flag' => 1]);
        
                return redirect()->route('tapal_view',$id);
        }


    ///////////////////
     //capture file/////////

   public function capturetapal(Request $request)
     {

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

      //$creation_of_tapal=\App\Tapal::where('id',$id)->get();

      $allseats=\App\Seat::all();

      
      $seatid=$request->session()->get('session_seat');
     

      $charray=$this->getchildseats1($seatid);
     // print_r($charray);
      
      return view('capturetapal', compact('seat','charray','allseats'));

     }

     public function getchildseats1($parent)
      {

        
        $childs=\App\Seat::where('reporting_seat',$parent)->get();
        foreach ($childs as $child) 
        {
        
          array_push($this->childarray,$child->id);

          $this->getchildseats1($child->id);
        }
        return $this->childarray;
      }

      public function gettapallist($id,Request $request)
          {
            
           //dd($id);
            $seatid=$request->session()->get('session_seat');
              $data  = [];
            $i=0;

              $tapaldetails=\App\Tapal_forward::where('to_seat',$id)->where('active_flag',1)->with('tapal')->whereHas('tapal', function($q) {
                $q->where('confidential', 1)->where('status',0);
            })->get();
           
              //dd($tapaldetails);
            foreach ($tapaldetails as $tapal)
            {   
                $tapalid=$tapal->tapal_id;
                $tapalckecking = \App\Tapal_forward::where('tapal_id',$tapalid)->where('from_seat',$seatid)->count();
                if($tapalckecking==0)
                  continue;

                $tapalnum=$tapal->tapal->tapal_no;//db name
                $tapaltitle=$tapal->tapal->title;
                $subjectegrp=\App\Tapal::where('id',$tapalid)->with('subject_groupobj')->first();
            //   dd( $subjectegrp);

                $subject_group=$subjectegrp->subject_groupobj->subject_group;
                $date=$tapal->created_at->format('d-M-Y');
             
                $updated_at=$tapal->updated_at->format('d-M-Y');
       
               

                $obj = new \stdClass;
                $obj->incid = $i + 1;
                $obj->id = $tapalid;
                $obj->tapal_no = $tapalnum;
                $obj->title = $tapaltitle;
                $obj->subject_group = $subject_group;
                $obj->created_at = $date;          
                $obj->updated_at = $updated_at;

                $data[$i] = $obj;
                $i++;

            }

            
            $tapaldetails=\App\Tapal_forward::where('to_seat',$id)->where('active_flag',1)->with('tapal')->whereHas('tapal', function($q) {
                $q->where('confidential', NULL)->where('status',0);
            })->get();
           

            foreach ($tapaldetails as $tapal)
            {   
                $tapalid=$tapal->tapal_id;

                $tapalnum=$tapal->tapal->tapal_no;//db name
                $tapaltitle=$tapal->tapal->title;
                $subjectegrp=\App\Tapal::where('id',$tapalid)->with('subject_groupobj')->first();
            //   dd( $subjectegrp);

                $subject_group=$subjectegrp->subject_groupobj->subject_group;
                $date=$tapal->created_at->format('d-M-Y');
             
                $updated_at=$tapal->updated_at->format('d-M-Y');
       
               

                $obj = new \stdClass;
                $obj->incid = $i + 1;
                $obj->id = $tapalid;
                $obj->tapal_no = $tapalnum;
                $obj->title = $tapaltitle;
                $obj->subject_group = $subject_group;
                $obj->created_at = $date;          
                $obj->updated_at = $updated_at;

                $data[$i] = $obj;
                $i++;

            }

           $tapaldata = collect($data);
          
            return Datatables::of($tapaldata)
            ->addColumn('action', function ($tapallist) {
                return '<a href="' . url('tapal_view') . '/' . $tapallist->id . '" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Show</a>';
            })
            ->editColumn('id', 'ID: {{$incid}}')
            ->make(true);
           
            }


  
      public function capture1(Request $request)

        {

            $id=$request->creation_of_tapal;
           // dd($id);
          
            $userid = Auth::user()->id;
          
            $seatid=$request->session()->get('session_seat');
             

            $tapalseat = \App\Tapal_forward::where('tapal_id',$id)->where('active_flag',1)->first();
            $tapaltoseat=$tapalseat->to_seat;

            $update = \App\Tapal_forward::where('tapal_id',$id)->where('from_seat',$seatid)->where('to_seat',$tapaltoseat)->where('active_flag',1)->update(['active_flag' => 0]);


             $insert_id = \App\Tapal_forward::create([//model name database field name

                          'tapal_id' => $id,
                          'to_seat' => $seatid,
                          'from_seat' => $tapaltoseat,
                          'active_flag' => 1,
                          'read_flag' => 1,
                          'capture_flag' => 1,
                          'updated_by'=>$userid
                          ]);
        
                return redirect()->route('tapal_view',$id);
        }







  public function tapal_view($id,Request $request)

       {
        
        $tapal_id= $id;
      $creation_of_tapal=\App\Tapal::where('id',$id)->first();

      //dd($creation_of_tapal);
        $priority=App\Priority::where('id',$creation_of_tapal->priority)->first();

        $priorityfile=App\Priority::all();

      $subject_group =\App\Subject_Group::where('id',$creation_of_tapal->subject_group_id)->first();

      $subject =\App\Subject::where('id',$creation_of_tapal->subject_id)->first();

       $office=\App\Office::all();

      $department=\App\Department::all();

      $section =\App\Section::all();

      $seat =\App\Seat::all();

      $actions =\App\Action::all();

      $file_groups =\App\File_Group::all();

      $username = Auth::user()->name;

      $values = \App\Comments::where('tapal_id',$tapal_id)->with('seat')->orderBy('created_at','desc')->get();
      
      $tapal_action =\App\Tapal_action::all();

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

      $seatid=$request->session()->get('session_seat');
        $seatid_new=$seatid;
      
     
      $seatobj=\App\Seat::where('id',$seatid)->first();
        $seatname= $seatobj->seat_name;

      $act_flg_status = "";
      $match=[['to_seat',$seatid_new],['tapal_id',$id]];
      $current_tapaluser=\App\Tapal_forward::where($match)->get();
      foreach($current_tapaluser as $tapvallist)
      {
        $active_flag = $tapvallist->active_flag;
        if($active_flag == 1)
        {
          $act_flg_status = 1;
        }
        else
        {
          $act_flg_status = 0;
        }
      }

      $yr=date("Y");
        $filesearch='%'.$yr;
        $fileobj=\App\File::select('file_no')->where("file_no",'like',$filesearch)->orderBy('id','desc')->first();
        if($fileobj)
        {
            $lastfileno=$fileobj->file_no;

            $filearr=explode("/",$lastfileno);
            $lastfileint=$filearr[1];
            $fileno=$lastfileint+1;
        }
        else
            $fileno=1;
        $fileprint="FILE/".$fileno."/".$yr;

      //$new_tapaluser=\App\Tapal_forward::where('from_seat',$seatid)->get();

      //dd($act_flg_status);
   // dd($current_tapaluser);
     return view('tapal_view', compact('values','seatid_new','username','subject_group','subject','creation_of_tapal','department','section','seat','act_flg_status','actions','tapal_action','file_groups','fileprint','tapal_id','priority','priorityfile','office'));

     }

//modal comment     

     public function comment_view($tapalid)

       {
        
        $tapal_id= $tapalid;
      $creation_of_tapal=\App\Tapal::where('id',$tapalid)->first();

      //dd($creation_of_tapal);

      //$subject_group =\App\Subject_Group::where('id',$creation_of_tapal->subject_group_id)->first();

      //$subject =\App\Subject::where('id',$creation_of_tapal->subject_id)->first();

      //$department=\App\Department::all();

      //$section =\App\Section::all();

      $seat =\App\Seat::all();

      $actions =\App\Action::all();

      //$file_groups =\App\File_Group::all();

      $username = Auth::user()->name;

      $values = \App\Comments::where('tapal_id',$tapal_id)->with('seat')->orderBy('created_at','desc')->get();
      
      //$tapal_action =\App\Tapal_action::all();

      $userid = Auth::user()->id;
      
     
      $seatid=$request->session()->get('session_seat');
        $seatid_new=$seatid;
      
     
      $seatobj=\App\Seat::where('id',$seatid)->first();
        $seatname= $seatobj->seat_name;

     

      //$new_tapaluser=\App\Tapal_forward::where('from_seat',$seatid)->get();

      //dd($act_flg_status);
   // dd($current_tapaluser);
     return view('tapal_view', compact('values','seatid_new','username','creation_of_tapal','seat'));

     }

    public function inboxjson(Request $request)
    {

      $id = Auth::user()->id;
      $seatid=$request->session()->get('session_seat');

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



      $inboxs = \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')->get();
      //dd($inboxs);

      $data  = [];
            $i=0;

      foreach ($inboxs as $inbox)
      {
        $id=$inbox->id;
        $tapalno=$inbox->tapal->tapal_no;
        $tapalid=$inbox->tapal->id;
        $priority=$inbox->tapal->priority;
        $createdby=$inbox->tapal->user->name;

        $prioritylabelobj=\App\Priority::where('id',$priority)->first();

        $priority=$prioritylabelobj->priority;

        $date=$inbox->tapal->created_at->format('d-M-Y h-m-s');;
        $title=$inbox->tapal->title;
         $confidential=$inbox->tapal->confidential;
         $valuables=$inbox->tapal->has_valuables;

         $forwardedby=$inbox->from_seat;
         if(!$forwardedby)
          $forwardedby="Self Generated";
        else
        {
          $forwardseatobj=\App\Seat::where('id',$forwardedby)->first();
          $forwardedby=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
        
        }

        $forwardedon=$inbox->created_at->format('d-M-Y');
        $fdate=new \DateTime($forwardedon);
        $cdate=new \DateTime();

        $diff='';
       $diff=date_diff($fdate,$cdate);
       $noofdays= $diff->format("%a days");

       $tapal_forward_toseat=$inbox->to_seat;
        /////count of comments -- tapal/////////////////////
        $match_comments = [['tapal_id','=',$tapalid],['comment_type','=',0]];
        $tapalcommentcount=\App\Comments::where($match_comments)->count();
        /////count of actions -- tapal/////////////////////
        $match_actions = [['tapal_id','=',$tapalid]];
        $tapalactioncount=\App\Tapal_action::where($match_actions)->count();
        /////count of attachments -- tapal/////////////////////
        $match_attachments = [['tapal_id','=',$tapalid]];
        $tapalattachmentcount=\App\Documents::where($match_attachments)->count();

         $subjectgroup=$inbox->tapal->subject_groupobj->subject_group;


         $obj = new \stdClass;
                $obj->incid = $i + 1;
                $obj->id = $id;
                $obj->tapalid = $tapalid;
                $obj->tapal_no = $tapalno;
                $obj->title = $title;
                $obj->created_at = $date;
                $obj->createdby = $createdby;
               
                $obj->from_seat = $forwardedby;
                $obj->forwardedon = $forwardedon."(".$noofdays.")" ;
                $obj->subject_group = $subjectgroup;
                $obj->tapalcommentcount = $tapalcommentcount;
                $obj->tapalactioncount = $tapalactioncount;
                $obj->tapalattachmentcount = $tapalattachmentcount;
                 $obj->priority = $priority;
                 $obj->confidential = $confidential;
                 $obj->valuables = $valuables;
               


             
               
                $data[$i] = $obj;
                $i++;

      }

             $inboxjson = collect($data);
        
            return Datatables::of($inboxjson)
                    ->make(true);


    
  // return Datatables::of($inboxs)->make(true);
      } 



public function getcomment($id)
{
  $userid = Auth::user()->id;
  $comments_arr = \App\Comments::where('tapal_id', $id)->with('tapal','seat')->orderBy('created_at','DESC')->get();
  
   $commentstr ='';
   $comments_count = \App\Comments::where('tapal_id', $id)->with('tapal','seat')->count();
   if($comments_count==0)
   {
        $commentstr .= "<div class='col-md-12'><span class='primary'>No Comments added yet !!!</span</div>";
   }

  
  foreach ($comments_arr as $comments_list)
  {
      $created_at=$comments_list->created_at->format('d-M-Y');
      $tapalid=$comments_list->tapal_id;
      $comment_type=$comments_list->comment_type;
      $comment_description=$comments_list->comment_description;
      $seatname=$comments_list->seat->seat_name;
      $commentseatid=$comments_list->seat_id;
      //dd($tapalid);
      $checkforseatmatch=\App\User_Seat::where('user_id',$userid)->where('seat_id',$commentseatid)->count();
      $currentseat=\App\User_Seat::where('user_id',$userid)->with('seat')->get();
    foreach ($currentseat as $userseat)
      {
        $currentuserseat=$userseat->seat_id;
        //dd($currentuserseat);
        $currenttapalseat=\App\Tapal_forward::where('tapal_id',$tapalid)->where('active_flag',1)->first();
        //foreach ($currenttapalseat as $tapalseat)
        //{
        $seat=$currenttapalseat->to_seat;
        // dd($seat);
        //}
        $seatflag=0;
        if($currentuserseat==$seat)
          {
             $seatflag=1;
          }

      }


      if(($comment_type==1)&&($checkforseatmatch>0)) 
      {    
        $commentstr .= "<div class='col-md-12'><input type='hidden' name='creation_of_tapal' id='creation_of_tapal' value='$id'><div class='panel panel-danger'><div class='panel-heading'>$seatname noted at $created_at</div><div class='panel-body'><div class='form-group'><div class='col-md-6'>$comment_description</div></div></div></div></div>";
      }

        if($comment_type==0) 
        {
          
          $commentstr .= "<div class='col-md-12'><input type='hidden' name='creation_of_tapal' id='creation_of_tapal' value='$id'><div class='panel panel-success'><div class='panel-heading'>$seatname noted at $created_at</div><div class='panel-body'><div class='form-group'><div class='col-md-6'>$comment_description</div></div></div></div></div>";

        }

        if(($comment_type==2)  && ($comments_list->disgard_flag==0))
        {
          
          $commentstr .= "<div class='col-md-12'><form class=form-horizontal action='".url('postit_comment')."'  method='post' > <input type='hidden' name='_token' value=".csrf_token()."><input type='hidden' name='tapal' id='tapal' value='$id'><input type='hidden' name='comment_id' id='comment_id' value='$comments_list->id'><div class='panel panel-warning'><div class='panel-heading'>$seatname noted at $created_at</div><div class='panel-body'><div class='form-group'><div class='col-md-6'>$comment_description</div></div></div></div> ";
          if($seatflag==1)
          {
            $commentstr .= "<button type='submit' class='btn btn-default btn-xm'> <span class='glyphicon glyphicon-trash'></span> Trash </button></form> </div>"; 
          }

        }
        
       
    }
    return $commentstr;
        
    
  }

  

  //action
  public function getaction($id)
  {
  $userid = Auth::user()->id;
  $actions_arr = \App\Tapal_action::where('tapal_id', $id)->with('tapal','seat','action')->orderBy('created_at','DESC')->get();
 

  $actionstr ='';

    $action_count = \App\Tapal_action::where('tapal_id', $id)->with('tapal','seat','action')->count();
   if($action_count==0)
   {
        $actionstr .= "<div class='col-md-12'><span class='primary'>No Actions added yet !!!</span</div>";
   }

  foreach ($actions_arr as $actions_list)
  {
      $created_at=$actions_list->created_at->format('d-M-Y');
      $tapalid=$actions_list->id;
      $action_type=$actions_list->action->actions;
      $action_description=$actions_list->action_description;
      $seatname=$actions_list->seat->seat_name;
      //$actionseatid=$actions_list->seat_id;
      
     
      if($action_type) 
      {    
        $actionstr .= "<div class='col-md-12'><input type='hidden' name='creation_of_tapal' id='creation_of_tapal' value='$id'><div class='panel panel-info'><div class='panel-heading'><h3 class='panel-title'>$seatname created a $action_type on $created_at</h3></div><div class='panel-body'><div class='form-group'><div class='col-md-12'>$action_description</div></div></div></div>
          </div>";
        }


       
    }
    return $actionstr;
  }
//////get history////////////////////////////////
public function gethistory($id)
{
      $userid = Auth::user()->id;
      
      $tapal = \App\Tapal::where('id', $id)->first();
      
      $tapalno=$tapal->tapal_no;
      $tapaltitle=$tapal->title;

      $created_at=$tapal->created_at->format('d-M-Y h:i:s');

      $i=0;
      $commentdescription="";
      /////////////tapal comments//////////////////////////////
      $comments =\App\Comments::where('tapal_id', $id)->where('comment_type',0)->with('seat')->get();
      foreach ($comments as $comment) 
      {
         $commentdescription=$comment->comment_description;
         $seat=$comment->seat->seat_name;
         $createdat=$comment->created_at->format('d-M-Y h:i:s');

         if($commentdescription)
         {
            $tapalhistorystr[$i]['date'] = $createdat;
            $tapalhistorystr[$i]['tapalstr'] = "<div class='col-md-12'><h3 class='panel-title'><i class='fa fa-fw fa-caret-right'></i>Comments was written by $seat on $createdat</h3></div><br>";
            $i++;
         }
      }
      ////////tapal actions///////////////////////////////////
      $actions =\App\Tapal_action::where('tapal_id', $id)->with('seat','action')->get();
      foreach ($actions as $action) 
      {
         $actiondescription=$action->action_description;
         $seat=$action->seat->seat_name;
         $createdat=$action->created_at->format('d-M-Y h:i:s');

         if($commentdescription)
         {
            $tapalhistorystr[$i]['date'] = $createdat;
            $tapalhistorystr[$i]['tapalstr'] = "<div class='col-md-12'><h3 class='panel-title'><i class='fa fa-fw fa-caret-right'></i>Actions was written by $seat on $createdat</h3></div><br>";
            $i++;
         }
      }
      ///////////tapal attachments /////////////////////////////////////////////////////
      $tapal_attchment =\App\Documents::where('tapal_id', $id)->get();
      foreach ($tapal_attchment as $tattachment) 
      {
         $tapal_documentname=$tattachment->document_name;
         $tapal_attach_createdat=$tattachment->created_at->format('d-M-Y h:i:s');

         if($tapal_documentname)
         {
            $tapalhistorystr[$i]['date'] = $tapal_attach_createdat;
            $tapalhistorystr[$i]['tapalstr'] = "<div class='col-md-12'><h3 class='panel-title'><i class='fa fa-fw fa-caret-right'></i>Attachment $tapal_documentname was attached on $tapal_attach_createdat</h3></div><br>";
            $i++;
         }
      }
      //////////////tapal forward///////////////////////////////
      $tapalforwards =\App\Tapal_forward::where('tapal_id', $id)->get();
      foreach ($tapalforwards as $tapalforward) 
      {
         
          $fromseat=$tapalforward->from_seat;               
          $toseat=$tapalforward->to_seat;
          $capture_flag=$tapalforward->capture_flag;
          
          $createdat=$tapalforward->created_at->format('d-M-Y h:i:s');
         
          if(!$fromseat)
          {

              $forwardtoseatobj=\App\Seat::where('id',$toseat)->first();
              $toseat_new=$forwardtoseatobj->seat_code."::".$forwardtoseatobj->seat_name;
              $seat_str="Self Generated by ".$toseat_new;
          }
          else if ($capture_flag==1)
          {
              $forwardseatobj=\App\Seat::where('id',$fromseat)->first();
              $fromseat=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
              $forwardtoseatobj=\App\Seat::where('id',$toseat)->first();
              $toseat_new=$forwardtoseatobj->seat_code."::".$forwardtoseatobj->seat_name;
              $seat_str="Captured From ".$fromseat;   
          }
          else
          {
              $forwardseatobj=\App\Seat::where('id',$fromseat)->first();
              $fromseat=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
              $forwardtoseatobj=\App\Seat::where('id',$toseat)->first();
              $toseat_new=$forwardtoseatobj->seat_code."::".$forwardtoseatobj->seat_name;
              $seat_str="Forwarded to ".$toseat_new." From ".$fromseat;
          }

          $forwardedon=$tapalforward->created_at->format('d-M-Y');
          $fdate=new \DateTime($forwardedon);
          $cdate=new \DateTime();

          $diff='';
          $diff=date_diff($fdate,$cdate);
          $noofdays= $diff->format("%a days");

          if($seat_str)
          {
            $tapalhistorystr[$i]['date'] = $createdat;
            $tapalhistorystr[$i]['tapalstr'] = "<div class='col-md-12'><h3 class='panel-title'><i class='fa fa-fw fa-caret-right'></i>$tapaltitle :: $tapalno was $seat_str on $createdat ($noofdays).</h3></div><br>";
            $i++;
          }
      }

      // Sort the tapalhistorystr arraywith date///////////////////////////////////
      foreach ($tapalhistorystr as $key => $row) 
      {
          $tapalhistorystr_date[$key]  = $row['date'];
      }
      // Sort the data with date ascending
      // Add $data as the last parameter, to sort by the common key
      $tapalhistorystr_final = array_multisort($tapalhistorystr_date, SORT_ASC, $tapalhistorystr);
      ///////////////////////end sort////////////////////////////////////////////////
      //dd($tapalhistorystr);
      $tapal_history_view='';
      foreach($tapalhistorystr as $tapalhistorystr_list)
      {
          $tapal_history_view .= $tapalhistorystr_list['tapalstr'];
      }
      //dd($tapal_history_view);
      return $tapal_history_view;
}
/////end history modal/////////////////////
/////attachment insertion in inbox

    public function tapal_attachmentpost1(Request $request)

    {

     $this->validate($request,[
             
            'upload1' => 'required'
            
            
       ]);
    // $id = Auth::user()->id;

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

        $seatid_new = $request->session()->get('session_seat');
       
        $uploadcount=$request->uploadcount;
        $tapal_id=$request->creation_of_tapal;
        for($i=1;$i<=$uploadcount;$i++)
        {

          $filename="upload".$i;
          $filedescrem="filedesc".$i;
          $filedesc=$request->$filedescrem;
          if ($request->hasFile($filename)) {
  

           

            $fileobj=$request->file($filename)->getClientOriginalName();

        $destpath="uploads";

        $custom_file_name = time().'-'.$userid.'-'.$fileobj;

        $path = $request->file($filename)->storeAs($destpath,$custom_file_name);

       
        $document_id = \App\Documents::create([//model name database field name
          'tapal_id' => $tapal_id,
          'document_path' => $path,
          'document_name' => $filedesc,
          'updated_by'=>$userid
           ]);


      }

        }


      if($document_id)
        //$sendmessage="Attachment Added!";
        notify()->success('Attachment Added!');


      else
       // $sendmessage="Error Occurred Pls recheck!";
       smilify('Error', 'Error Occurred Pls recheck!');
      
      return redirect()->back();


    }


  //////////////////////////

  //attachments
    public function getattachment($id)
    {
      $userid = Auth::user()->id;
      $attachments_arr = \App\Documents::where('tapal_id', $id)->with('tapal')->orderBy('created_at','DESC')->get();
   

      $attachmentstr ='';
      $data  = [];
      $attachment_count = \App\Documents::where('tapal_id', $id)->with('tapal')->count();

     if($attachment_count==0)
       {
            $attachmentstr .= "<div class='col-md-12'><span class='primary'>No Attachments uploaded yet !!!</span</div>";
            $data[0] = $attachmentstr;
       }
  
            $i=0;

      foreach ($attachments_arr as $attachments_list)
      {
          $created_at=$attachments_list->created_at->format('d-M-Y');
          $tapalid=$attachments_list->id;
          $document_path=$attachments_list->document_path;
          $document_name=$attachments_list->document_name;
      
      //$seatname=$attachments_list->seat->seat_name;
      
     
      if($document_path) 
      {    
        $attachmentstr = "<div class='col-md-12'><input type='hidden' name='creation_of_tapal' id='creation_of_tapal' value='$id'><div class='panel panel-info'><div class='panel-heading'><h3 class='panel-title'>Attachments</h3></div><div class='panel-body'><div class='form-group'><div class='col-md-12'>   <div ><a href='#' onclick=showdocument('$document_path');>$document_name::View File</a></div><div class='col-md-10' align='center'></div> </div></div></div></div></div>";
        }

                $data[$i] = $attachmentstr;
                $i++;
        
      }
     return $data;
         
     }


 //////////////////////////////////////////////File inbox/////////////////////////////////////////////////////////////
      
      public function fileinbox(Request $request)
     {

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

      //$creation_of_tapal=\App\Tapal::where('id',$id)->get();

      $file_id=$request->creation_of_file;
     // dd($file_id);

      $seatid=$request->session()->get('session_seat');

       $office=\App\Office::all();
      $department=\App\Department::all();

      $section =\App\Section::all();

        $actions = \App\Action::all();

        $tapalinboxcount = \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();


      $fileinboxcount = \App\File_forward::where('to_seat', $seatid)->where('active_flag',1)->with('file')->orderBy('created_at','DESC')->count();

      $tapaloutboxcount = \App\Tapal_forward::where('from_seat', $seatid)->with('tapal')->orderBy('created_at','DESC')->count();

      $fileoutboxcount = \App\File_forward::where('from_seat', $seatid)->with('file')->orderBy('created_at','DESC')->count();



      $approveduser='';
      $template =\App\Template::all();

      $ftemplate =\App\Fileview_template::where('file_id',$file_id)->where('cancel_flag',0)->with('template','user')->get();
      foreach ($ftemplate as $filetemplate)
        {
            $fileviewtemplateid=$filetemplate->id;
            $fileviewtemplateapprovedby=$filetemplate->approved_by;
            if($fileviewtemplateapprovedby!=0)
            {
                $approveuser=\App\User::where('id',$fileviewtemplateapprovedby)->first();
                $approveduser=$approveuser->name;
            }
            $fileviewtemplatearr[$fileviewtemplateid]=$approveduser;
        } 
//////////////////// file route details

      $file_arr = \App\File_forward::where('to_seat', $seatid)->where('active_flag',1)->get();
      $troutfilearr=[];
       //dd($file_arr);
            foreach($file_arr as $filearr)
            { 
              $filegroupid=$filearr->file->file_group_id;
              //dd($filegroupid);
              $troutarr=\App\Troute::where('file_group_id',$filegroupid)->get();
              
              $i=0;
              foreach($troutarr as $troutarrs)
                {    $troutfilegroup=$troutarrs->id;
                     $troutfilegrpname=$troutarrs->froutename;
                     $troutfilearr[$i]['id']=$troutfilegroup;
                     $troutfilearr[$i]['name']=$troutfilegrpname;
                     $i++;

            }
            }
            //dd($troutfilearr);
            

      //$values = \App\Comments::where('file_id',$file_arr)->with('seat')->orderBy('created_at','desc')->get();

     // $file_forward =\App\File_forward::with('file_group')->get();
     
    
     // $subject =\App\Subject::where('id',$creation_of_tapal->subject_id)->first();

      
      return view('fileinbox', compact('department','section','file_arr','actions','office','template','ftemplate','approveduser','fileinboxcount','tapalinboxcount','tapaloutboxcount','fileoutboxcount','troutfilearr'));

     }


//////////////////////////////////////////////////////////////////////////////////////////////////////

   //new modal file view

  public function modalfileview($id,Request $request)

   {
        
      $file_id= $id;
      $creation_of_file=\App\File::where('id',$id)->with('user')->first();
      $file_no=$creation_of_file->file_no;
      $description=$creation_of_file->file_description;
      $confidential=$creation_of_file->confidential;
      $filecreateduser=$creation_of_file->user->name;
      
      $fileuserseat=\App\User_Seat::where('user_id',$creation_of_file->created_by)->with('seat')->first();
      $fileseat=$fileuserseat->seat->seat_name;

      $filedetailsarr =[];
      $i=0;
      $filedetailsarr['id']=$file_id;
       $filedetailsarr['file_no']=' File No:'." ".$file_no."   " .$fileseat." : " .$filecreateduser;
       if($confidential==1)
       {
          $conf=', Confidential';
          $filedetailsarr['file_no']=' File No:'." ".$file_no."   " .$fileseat." : " .$filecreateduser .$conf;
       }
       $filedetailsarr['file_description']=$description;
       
       $filedetailsarr['title']=$creation_of_file->title;
//dd($filedetailsarr);
       $filefrwrd=\App\File_forward::where('file_id',$file_id)->where('active_flag',1)->with('file')->first();
       $createdby=$filefrwrd->file->user->name;
       $createdat=$filefrwrd->file->created_at->format('d-M-Y h-m-s');
       $forwardedon=$filefrwrd->created_at->format('d-M-Y h-m-s');
       $forwardedby=$filefrwrd->from_seat;
         if(!$forwardedby)
          $forwardedby="Self Generated";
        else
        {
          $forwardseatobj=\App\Seat::where('id',$forwardedby)->first();
          $forwardedby=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
        
        }

        $filedetailsarr['from_seat']=$forwardedby;
        $filedetailsarr['created_at']=$forwardedon;
        $filedetailsarr['name']=$createdby;
        $filedetailsarr['created_at']=$createdat;

        $filegroup =\App\File_Group::where('id',$creation_of_file->file_group_id)->first();
        $filegrp=$filegroup->file_group;
        $filedetailsarr['file_group']=$filegrp;

        

        $prio=App\Priority::where('id',$creation_of_file->priority)->first();
        $priority=$prio->priority;
        $filedetailsarr['priority']=$priority;

        $seatid=$request->session()->get('session_seat');
              $act_flg_status = 0;
              $match=[['to_seat',$seatid],['file_id',$id]];
              //print_r($match);
              $current_fileuser=\App\File_forward::where($match)->get();
              foreach($current_fileuser as $tapvallist)
              {
                $active_flag = $tapvallist->active_flag;
                if($active_flag == 1)
                {
                  $act_flg_status = 1;
                }

              }

              $filedetailsarr['active_flag']=$act_flg_status;

              $userid = Auth::user()->id;
              $user_comment_flag='';
              $file_comment_last = \App\Comments::where('file_id',$file_id)->orderBy('created_at','DESC')->first();
              if(isset($file_comment_last)!='')
              {
                  $userid_last_filecomment = $file_comment_last->updated_by;
                  if($userid_last_filecomment == $userid)
                  {
                      $user_comment_flag = 1;
                  }
              }
              else
              {
                  $user_comment_flag=0;
              }
              //$note="Forward button will be enabled,if alteast one note is entered by the current seat.";

              $filedetailsarr['user_comment_flag']=$user_comment_flag;
            

      $attachmentcount =\App\Documents::where('file_id',$file_id)->count();

      
      $i=0;
      $file_attchment =\App\Documents::where('file_id', $file_id)->get();
      $filedocstr='';
      foreach ($file_attchment as $tattachment) 
      {
         $file_documentname=$tattachment->document_name;
         $path=$tattachment->document_path;
         $file_attach_createdat=$tattachment->created_at->format('d-M-Y h:i:s');

         $filepath=storage_path('app/'.$path);
         $size='';
         if(file_exists($filepath)){
          $size=round(filesize($filepath)/1024,1);
         }
         
         if($file_documentname)
         {
            
            $filedocstr.= "<li><div class='mailbox-attachment-info'><a href='#' class='mailbox-attachment-name'><i class='fa fa-paperclip'></i> $file_documentname</a><span class='mailbox-attachment-size'>$size KB<a href='../../storage/app/$path' class='btn btn-default btn-xs pull-right'><i class='fa fa-cloud-download'></i></a></span></div></li>";

            $i++;
         }
      }
      
      //tapal attachments
        $tapalattachmentscount=0;

        $tapalattachments_arr = \App\File_reference::where('file_id', $file_id)->with('tapal')->get();
        foreach ($tapalattachments_arr as $tapalattachment_arr)
        {

            $tapalid=$tapalattachment_arr->tapal_id;
            $tapalnumber = \App\Tapal::where('id', $tapalid)->first();
            $tapalno=$tapalnumber->tapal_no;

            $tapalattachmentscount = \App\Documents::where('tapal_id', $tapalid)->count();
            if($tapalattachmentscount > 0)
            {
              $tapalattachments[$tapalid] = \App\Documents::where('tapal_id', $tapalid)->with('tapal','user')->orderBy('created_at','DESC')->get();
              foreach ($tapalattachments[$tapalid] as $tplattachment) 
              {
                 $file_documentname=$tplattachment->document_name;
                 $path=$tplattachment->document_path;
                 $file_attach_createdat=$tplattachment->created_at->format('d-M-Y h:i:s');

                 $filepath=storage_path('app/'.$path);
                 if(file_exists($filepath)){
                  $size=round(filesize($filepath)/1024,1);
                 }
                 
                 if($file_documentname)
                 {
                    
                    $filedocstr.= "<li><div class='mailbox-attachment-info'><a href='#' class='mailbox-attachment-name'><i class='fa fa-paperclip'></i> $file_documentname&nbsp;&nbsp;$tapalno</a><span class='mailbox-attachment-size'>$size KB<a href='../../storage/app/$path' class='btn btn-default btn-xs pull-right'><i class='fa fa-cloud-download'></i></a></span></div></li>";          
                 }
              }
            }
            
        }

          $filedetailsarr['document_path']=$filedocstr;


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

      $comments_arr = \App\Comments::where('file_id', $file_id)->with('file','seat')->orderBy('created_at','DESC')->get();
  
       $commentstr ='';
       $comments_count = \App\Comments::where('file_id', $file_id)->with('file','seat')->count();
       if($comments_count==0)
       {
            $commentstr .= "<div class='box-comment'><div class='comment-text'><span class='username' id='nocomment'>&nbsp;<span class='text-muted pull-right'></span></span>No Comments added yet !!!</div></div>";
       }

      
      foreach ($comments_arr as $comments_list)
      {
          $created_at=$comments_list->created_at->format('d-M-Y');
          $fileid=$comments_list->file_id;
          $comment_type=$comments_list->comment_type;
          $comment_description=$comments_list->comment_description;
          $seatname=$comments_list->seat->seat_name;
          $commentseatid=$comments_list->seat_id;
          //dd($tapalid);
          $checkforseatmatch=\App\User_Seat::where('user_id',$userid)->where('seat_id',$commentseatid)->count();
          $currentseat=\App\User_Seat::where('user_id',$userid)->with('seat')->get();
        foreach ($currentseat as $userseat)
          {
            $currentuserseat=$userseat->seat_id;
            //dd($currentuserseat);
            $currentfileseat=\App\File_forward::where('file_id',$fileid)->where('active_flag',1)->first();
            //foreach ($currenttapalseat as $tapalseat)
            //{
            $seat=$currentfileseat->to_seat;
            // dd($seat);
            //}
            $seatflag=0;
            if($currentuserseat==$seat)
              {
                 $seatflag=1;
              }

          }


          if(($comment_type==1)&&($checkforseatmatch>0)) 
          {    
            $commentstr .= "<div class='box-comment'><input type='hidden' name='creation_of_file' id='creation_of_file' value='$fileid'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname<span class='text-muted pull-right'>$created_at</span></span><font color='red'>$comment_description</font></div></div>";
          }

            if($comment_type==0) 
            {
              
              $commentstr .= "<div class='box-comment'><input type='hidden' name='creation_of_file' id='creation_of_file' value='$fileid'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname<span class='text-muted pull-right'>$created_at</span></span><font color='green'>$comment_description</font></div></div>";

            }

            if(($comment_type==2)  && ($comments_list->disgard_flag==0))
            {
              
              $commentstr .= "<div class='box-comment'><form class=form-horizontal action='".url('postit_comment')."'  method='post' > <input type='hidden' name='_token' value=".csrf_token()."><input type='hidden' name='file' id='file' value='$fileid'><input type='hidden' name='comment_id' id='comment_id' value='$comments_list->id'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname<span class='text-muted pull-right'>$created_at</span></span><font color='#ad890a'>$comment_description</font></div>";

              if($seatflag==1)
              {
                $commentstr .= "<button type='submit' class='btn btn-default btn-xm'> <span class='glyphicon glyphicon-trash'></span> Trash </button></form></div> "; 
              }

            }
            
           
        }

        

        //dd($tapaldetailsarr);

        //tapal comments 
              $commentscount=0;
     
              $tapalcomments_arr = \App\File_reference::where('file_id', $file_id)->with('tapal')->get();
              foreach ($tapalcomments_arr as $tapalcomment_list)
               {

                  $tapalid=$tapalcomment_list->tapal_id;
                  $tapalno=$tapalcomment_list->tapal->tapal_no;

                  $commentscount += \App\Comments::where('tapal_id', $tapalid)->count();
                  if($commentscount > 0)
                   {
                      $comments[$tapalid] = \App\Comments::where('tapal_id', $tapalid)->with('tapal','seat')->orderBy('created_at','DESC')->get();
                      foreach ($comments[$tapalid] as $tapalcomment) 
                      {
                         $seatname=$tapalcomment->seat->seat_name;
                         $comment_description=$tapalcomment->comment_description;
                         $created_at=$tapalcomment->created_at->format('d-M-Y');
                         if(($tapalcomment->comment_type == 1) && ($tapalcomment->seat_id == $fileseat))
                         {
                            $commentstr .= "<div class='box-comment'><input type='hidden' name='creation_of_file' id='creation_of_file' value='$file_id'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname&nbsp;&nbsp;$tapalno<span class='text-muted pull-right'>$created_at</span></span><font color='red'>$comment_description</font></div></div>";
                         }

                         if($tapalcomment->comment_type == 0)
                         {
                            $commentstr .= "<div class='box-comment'><input type='hidden' name='creation_of_file' id='creation_of_file' value='$file_id'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname&nbsp;&nbsp;$tapalno<span class='text-muted pull-right'>$created_at</span></span><font color='green'>$comment_description</font></div></div>";
                         }

                         if(($tapalcomment->comment_type == 2) && ($tapalcomment->disgard_flag==0))
                         {
                            $commentstr .= "<div class='box-comment'><form class=form-horizontal action='".url('postit_comment')."'  method='post' > <input type='hidden' name='_token' value=".csrf_token()."><input type='hidden' name='file' id='file' value='$file_id'><input type='hidden' name='comment_id' id='comment_id' value='$comments_list->id'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname&nbsp;&nbsp;$tapalno<span class='text-muted pull-right'>$created_at</span></span><font color='#ad890a'>$comment_description</font></div>";

                            if($act_flg_status==1)
                                {
                                  $commentstr .= "<button type='submit' class='btn btn-default btn-xm'> <span class='glyphicon glyphicon-trash'></span> Trash </button></form></div> "; 
                                }
                         }
                      }
                   }
               }

            $filedetailsarr['comment_description']=$commentstr;

            $actions_arr = \App\File_action::where('file_id', $id)->with('file','seat','action')->orderBy('created_at','DESC')->get();
 

            $actionstr ='';

              $action_count = \App\File_action::where('file_id', $id)->with('file','seat','action')->count();
             if($action_count==0)
             {
                  $actionstr .= "<div class='box-comment'><div class='comment-text'><span class='username'>&nbsp;<span class='text-muted pull-right'></span></span>No Actions added yet !!!</div></div>";
             }

            foreach ($actions_arr as $actions_list)
            {
                $created_at=$actions_list->created_at->format('d-M-Y');
                $fileid=$actions_list->id;
                $action_type=$actions_list->action->actions;
                $action_description=$actions_list->action_description;
                $seatname=$actions_list->seat->seat_name;
                //$actionseatid=$actions_list->seat_id;
                
               
                if($action_type) 
                {    
                  $actionstr .= "<div class='box-comment'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname<span class='text-muted pull-right'>$created_at</span></span>$action_description</div></div>";
                  }


                 
              }  


              
              //tapal actions
              $tapalactionscount=0;
              // $checkforseatmatch=\App\User_Seat::where('user_id',$userid)->where('seat_id',$commentseatid)->count();
              $tapalactions_arr = \App\File_reference::where('file_id', $id)->with('tapal')->get();
              //dd($tapalactions_arr);
              foreach ($tapalactions_arr as $tapalactions_arrobj)
              {

                  $tapalid=$tapalactions_arrobj->tapal_id;
                  $tapalno=$tapalactions_arrobj->tapal->tapal_no;
                  $tapalactionscount += \App\Tapal_action::where('tapal_id', $tapalid)->count();
                  if($tapalactionscount > 0)
                  {
                      $tapalactions[$tapalid] = \App\Tapal_action::where('tapal_id', $tapalid)->with('tapal','seat','action')->orderBy('created_at','DESC')->get();
                      foreach ($tapalactions[$tapalid] as $tapalaction)
                      {
                         
                         $seatname=$tapalaction->seat->seat_name;
                         //$comment_description=$tapalaction->comment_description;
                         $action_description=$tapalaction->action_description;
                         $action_type=$tapalaction->action->actions;
                         $created_at=$tapalaction->created_at->format('d-M-Y');

                         if($action_type) 
                          {    
                            $actionstr .= "<div class='box-comment'><img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'><div class='comment-text'><span class='username'>$seatname&nbsp;&nbsp;$tapalno<span class='text-muted pull-right'>$created_at</span></span>$action_description</div></div>";
                          }
                      }
                  }
              }
              
              $filedetailsarr['action_description']=$actionstr;





              $priorityfile=App\Priority::all();

              // tapal attached to this file///////////////
              

             

              
              //list of tapal attached to this file////////////////
              $linkstr ='';
              $tapallinkscount = '';
              $tapallinkscount = \App\File_reference::where('file_id', $id)->with('tapal','user')->count();
              if($tapallinkscount=='')
              {
                 $linkstr .= "<div class='box-comment'><div class='comment-text'><span class='username'>&nbsp;<span class='text-muted pull-right'></span></span>No Tapals attached yet !!!</div></div>";
              }
              $tapallinks = \App\File_reference::where('file_id', $id)->with('tapal','user')->get();
              foreach ($tapallinks as $tapallink) 
              {
                     $tapallinkuser=$tapallink->user->id;
                     $tapalno=$tapallink->tapal->tapal_no;
                     $createdat=$tapallink->created_at->format('d-m-Y H:i:s');
                     //$seat=$tapallink->tapal->seat->seat_name;
                     $tapallinkseat =\App\User_Seat::where('user_id',$tapallinkuser)->with('seat')->first();
                     $seat=$tapallinkseat->seat->seat_name;
                     $linkstr .= "<div class='box-comment'><div class='comment-text'><span class='username'>$tapalno&nbsp;attached to this file on&nbsp;$createdat&nbsp;by $seat</span></div></div>";
              }

              $filedetailsarr['link']=$linkstr;

              $tempstr='';
              $approveduser='';
              $ftemplate =\App\Fileview_template::where('file_id',$id)->where('cancel_flag',0)->with('template','user')->get();
              foreach ($ftemplate as $titledisp)
                {
                    $tempstr .="<tr ><td width='75%' >";

                    $fileviewtemplateid=$titledisp->id;
                    $title=$titledisp->template->title;
                    $name=$titledisp->user->name;
                    $created_at=$titledisp->created_at->format("d-m-Y H:i:s");

                    $tempstr .="<b>$title</b> last modified by $name on $created_at";


                    $fileviewtemplateapprovedby=$titledisp->approved_by;
                    if($fileviewtemplateapprovedby!=0)
                    {
                        $approveuser=\App\User::where('id',$fileviewtemplateapprovedby)->first();
                        $approveduser=$approveuser->name;
                    }
                    $fileviewtemplatearr[$fileviewtemplateid]=$approveduser;
                    if($titledisp->approval_flag==1)
                    {
                      $tempstr .="approved by $approveduser";
                    }
                    $tempstr .="<br></td><td >";

                    if($titledisp->approval_flag==0)
                    {
                       $tempstr .="<button data-toggle='modal' data-target='#TemplateModal' class='btn btn-success pull-right btn-sm' type='button' onclick=getModaltemp('$fileviewtemplateid');>Edit this Document</button>";
                    }


                    if($titledisp->approval_flag==1)
                    {
                      $tempstr .="<button data-toggle='modal' data-target='#ApproveModal' class='btn btn-warning pull-right btn-sm' type='button' onclick=getApprovetemp('$fileviewtemplateid');>View this Document</button>";
                    }
                    
                    $tempstr .="</td></tr>";
                    
                } 

                $filedetailsarr['temp']=$tempstr;

                //dd($filedetailsarr);
               return $filedetailsarr;

      }



    public function file_view($id,Request $request)

       {
        
        $file_id= $id;
      $creation_of_file=\App\File::where('id',$id)->first();

      //dd($creation_of_tapal);
       $priority=App\Priority::where('id',$creation_of_file->priority)->first();

        $priorityfile=App\Priority::all();

      $file_group =\App\File_Group::where('id',$creation_of_file->file_group_id)->first();

     
       $actions =\App\Action::all();

        $office=\App\Office::all();

      $department=\App\Department::all();

      $section =\App\Section::all();

      $seat =\App\Seat::all();


      $username = Auth::user()->name;

      $values = \App\Comments::where('file_id',$file_id)->with('seat')->orderBy('created_at','desc')->get();
      
      $attachmentcount =\App\Documents::where('file_id',$file_id)->count();

      $attachments=\App\Documents::where('file_id',$file_id)->get();

      $values1 = \App\File_action::where('file_id',$file_id)->with('seat')->orderBy('created_at','desc')->get();

      $userid = Auth::user()->id;
      
     $act_flg_status = "";
     $seatid=$request->session()->get('session_seat');
     $seatid_new=$seatid;
    
      
     $seatobj=\App\Seat::where('id',$seatid)->first();
        $seatname= $seatobj->seat_name;

      
      $match=[['to_seat',$seatid_new],['file_id',$id]];
      $current_fileuser=\App\File_forward::where($match)->get();
      foreach($current_fileuser as $filevallist)
      {
        $active_flag = $filevallist->active_flag;
        if($active_flag == 1)
        {
          $act_flg_status = 1;
        }
        
      }
    


      $yr=date("Y");
        $filesearch='%'.$yr;
        $fileobj=\App\File::select('file_no')->where("file_no",'like',$filesearch)->orderBy('id','desc')->first();
        if($fileobj)
        {
            $lastfileno=$fileobj->file_no;

            $filearr=explode("/",$lastfileno);
            $lastfileint=$filearr[1];
            $fileno=$lastfileint+1;
        }
        else
            $fileno=1;
        $fileprint="FILE/".$fileno."/".$yr;
      //$new_tapaluser=\App\Tapal_forward::where('from_seat',$seatid)->get();

      //dd($act_flg_status);
   // dd($current_tapaluser);
     return view('file_view', compact('values','values1','seatid_new','username','file_group','creation_of_file','department','section','seat','act_flg_status','fileprint','us_arr','seatname','attachmentcount','attachments','actions','priority','priorityfile','office'));

     }

     //modal comment     

     public function comment_view1($fileid)

       {
        
        $file_id= $fileid;
      $creation_of_file=\App\File::where('id',$fileid)->first();

      //dd($creation_of_tapal);

      //$subject_group =\App\Subject_Group::where('id',$creation_of_tapal->subject_group_id)->first();

      //$subject =\App\Subject::where('id',$creation_of_tapal->subject_id)->first();

      //$department=\App\Department::all();

      //$section =\App\Section::all();

      $seat =\App\Seat::all();

      $actions =\App\Action::all();

      //$file_groups =\App\File_Group::all();

      $username = Auth::user()->name;

      $values = \App\Comments::where('file_id',$file_id)->with('seat')->orderBy('created_at','desc')->get();
      
      //$tapal_action =\App\Tapal_action::all();

      $userid = Auth::user()->id;
      
     
       $seatid=$request->session()->get('session_seat');
     $seatid_new=$seatid;
    
      
     $seatobj=\App\Seat::where('id',$seatid)->first();
        $seatname= $seatobj->seat_name;

     

      //$new_tapaluser=\App\Tapal_forward::where('from_seat',$seatid)->get();

      //dd($act_flg_status);
   // dd($current_tapaluser);
     return view('file_view', compact('values','seatid_new','username','creation_of_file','seat'));

     }

     public function fileinboxjson(Request $request)
    {

      $id = Auth::user()->id;
       
        $seatid=$request->session()->get('session_seat');

      
       $inboxs = \App\File_forward::whereHas('file', function ($query) {
        $query->with('user');
    })->where('to_seat', $seatid)->where('active_flag',1)->with('file')->orderBy('created_at','desc')->get();
      //dd($inboxs);

      $data  = [];
            $i=0;

      foreach ($inboxs as $inbox)
      {
        $id=$inbox->id;
        $fileno=$inbox->file->file_no;
        $fileid=$inbox->file->id;
        $priority=$inbox->file->priority;
        $createdby=$inbox->file->user->name;
        $confidential=$inbox->file->confidential;
        $prioritylabelobj=\App\Priority::where('id',$priority)->first();        
        $priority=$prioritylabelobj->priority;


        $date=$inbox->file->created_at->format('d-M-Y h-m-s');
        $title=$inbox->file->title;
        $refencenumber=$inbox->file->reference_number;

         $forwardedby=$inbox->from_seat;
         if(!$forwardedby)
          $forwardedby="Self Generated";
        else
        {
          $forwardseatobj=\App\Seat::where('id',$forwardedby)->first();
          $forwardedby=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
        
        }

        $file_forward_toseat=$inbox->to_seat;
        /////count of comments -- tapal/////////////////////
        $match_comments = [['file_id','=',$fileid],['comment_type','=',0]];
        $filecommentcount=\App\Comments::where($match_comments)->count();
        //////attached tapal comments in a file////////////
        $tapallinkcomment=0;
        $tapallinks = \App\File_reference::where('file_id', $fileid)->get();

        foreach ($tapallinks as $tapallink) 
        {
           $tapal_id_ref=$tapallink->tapal_id;
           $match_comments_tapal = [['tapal_id',$tapal_id_ref],['comment_type','=',0]];
           $tapal_comment =\App\Comments::where($match_comments_tapal)->count();
          $tapallinkcomment += $tapal_comment;

        }
        $total_comment_count = $tapallinkcomment + $filecommentcount;


        /////count of actions -- tapal/////////////////////
        $match_actions = [['file_id','=',$fileid]];
        $fileactioncount=\App\File_action::where($match_actions)->count();
        //////attached tapal actions in a file////////////
        $tapallinkactions=0;
        $tapallinks_actions = \App\File_reference::where('file_id', $fileid)->get();

        foreach ($tapallinks_actions as $tap_action_list) 
        {
           $tapal_id_ref_act=$tap_action_list->tapal_id;
           $tapal_action =\App\Tapal_action::where('tapal_id',$tapal_id_ref_act)->count();
          $tapallinkactions += $tapal_action;

        }
        $total_fileaction_count = ($tapallinkactions*1) + ($fileactioncount*1);



        /////count of attachments -- tapal/////////////////////
        $match_attachments = [['file_id','=',$fileid]];
        $fileattachmentcount=\App\Documents::where($match_attachments)->count();
        //////attached tapal actions in a file////////////
        $tapallinkattachments=0;
        $tapallinks_attachments = \App\File_reference::where('file_id', $fileid)->get();

        foreach ($tapallinks_attachments as $tap_attach_list) 
        {
           $tapal_id_ref_attach=$tap_attach_list->tapal_id;
           $tapal_attachment_count =\App\Documents::where('tapal_id',$tapal_id_ref_attach)->count();
           $tapallinkattachments += $tapal_attachment_count;

        }
        $total_fileattachment_count = ($tapallinkattachments*1) + ($fileattachmentcount*1);

        
        //////////////////////////////////////////
        $forwardedon=$inbox->created_at->format('d-M-Y');
        $fdate=new \DateTime($forwardedon);
        $cdate=new \DateTime();

        $diff='';
       $diff=date_diff($fdate,$cdate);
       $noofdays= $diff->format("%a days");

         $filegroup=$inbox->file->file_groupobj->file_group;


         $obj = new \stdClass;
                $obj->incid = $i + 1;
                $obj->id = $id;
                $obj->fileid = $fileid;
                $obj->file_no = $fileno;
                $obj->title = $title;
                $obj->created_at = $date;
                $obj->createdby = $createdby;
                $obj->confidential = $confidential;

                $obj->reference_number = $refencenumber;
                $obj->from_seat = $forwardedby;
                $obj->forwardedon = $forwardedon."(".$noofdays.")" ;
                $obj->file_group = $filegroup;
                $obj->priority = $priority;
                $obj->filecommentcount = $total_comment_count;
                $obj->fileactioncount = $total_fileaction_count;
                $obj->fileattachmentcount = $total_fileattachment_count;
                $data[$i] = $obj;
                $i++;

      }
        $fileinboxjson = collect($data);
    
        return Datatables::of($fileinboxjson)->make(true);
      } 

      //////comment save in file inbox//////////////////////
public function file_viewpost(Request $request)

    {

     $this->validate($request,[
             
            'comment_type' => 'required',
            'description' => 'required'
            
            
       ]);


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

      
        $seatid_new = $request->session()->get('session_seat');
       
        $comment_type=$request->comment_type;
        $description=$request->description;
        $file_id=$request->creation_of_file;
        
        
        $id = Auth::user()->id;

        $insert_id = \App\Comments::create([//model name database field name

          'comment_type' => $comment_type,
          'comment_description' => $description,
          'file_id' => $file_id,
          'seat_id' => $seatid_new,
          
          'updated_by'=>$id
           ]);


         

      if($insert_id)
       // $sendmessage="Comment Added!";
       notify()->success('Comment Added!');


      else
       // $sendmessage="Error Occurred Pls recheck!";
       smilify('Error', 'Error Occurred Pls recheck!');
      
      return redirect()->back();


    }
    ////////////////////////////////////////////
    
    //////get history////////////////////////////////
public function getfilehistory($id)
{
      $userid = Auth::user()->id;
      
      $file = \App\File::where('id', $id)->first();
      
      $fileno=$file->file_no;
      $filetitle=$file->title;

      $created_at=$file->created_at->format('d-M-Y h:i:s');

      $i=0;
      /////////file comments///////////////////////////////////////////////
      $comments =\App\Comments::where('file_id', $id)->where('comment_type',0)->with('seat')->get();
      foreach ($comments as $comment) 
      {
         $commentdescription=$comment->comment_description;
         $seat=$comment->seat->seat_name;
         $createdat=$comment->created_at->format('d-M-Y h:i:s');

         if($commentdescription)
         {
            $filehistorystr[$i]['date'] = $createdat;
            $filehistorystr[$i]['filestr'] = "<div class='col-md-12'><h3 class='panel-title'><i class='fa fa-fw fa-caret-right'></i>Comments was written by $seat on $createdat</h3></div><br>";
            $i++;
         }
      }
      //////attached tapals in a file////////////
      $tapallinks = \App\File_reference::where('file_id', $id)->with('tapal')->get();
      foreach ($tapallinks as $tapallink) 
      {
        $tapal_id_ref=$tapallink->tapal_id;
        $tapal_no = $tapallink->tapal->tapal_no;
        //////attached tapal comments in a file////////////
        $match_comments_tapal = [['tapal_id',$tapal_id_ref],['comment_type','=',0]];
        $tapal_comment =\App\Comments::where($match_comments_tapal)->with('seat')->get();
        foreach ($tapal_comment as $tcomment) 
        {
           $tapal_commentdescription=$tcomment->comment_description;
           $tapal_seat=$tcomment->seat->seat_name;
           $tapal_createdat=$tcomment->created_at->format('d-M-Y h:i:s');

           if($tapal_commentdescription)
           {
              $filehistorystr[$i]['date'] = $tapal_createdat;
              $filehistorystr[$i]['filestr'] = "<div class='col-md-12'><h3 class='panel-title'><i class='fa fa-fw fa-caret-right'></i>Comments was written by $tapal_seat on $tapal_createdat :: $tapal_no</h3></div><br>";
              $i++;
           }
        }
        //////attached tapal actions in a file////////////
        $match_actions_tapal = [['tapal_id',$tapal_id_ref]];
        $tapal_action_file =\App\Tapal_action::where($match_actions_tapal)->with('seat','action')->get();
        foreach ($tapal_action_file as $taction) 
        {
           $tapalactiondescription=$taction->action_description;
           $tseat=$taction->seat->seat_name;
           $tcreatedat=$taction->created_at->format('d-M-Y h:i:s');

           if($tapalactiondescription)
           {
              $filehistorystr[$i]['date'] = $tcreatedat;
              $filehistorystr[$i]['filestr'] = "<div class='col-md-12'><h3 class='panel-title'><i class='fa fa-fw fa-caret-right'></i>Actions was written by $tseat on $tcreatedat :: $tapal_no</h3></div><br>";
              $i++;
           }
        }
        //////attached tapal attachments in a file////////////
        $match_attach_tapal = [['tapal_id',$tapal_id_ref]];
        $tapal_attach_file =\App\Documents::where($match_attach_tapal)->get();
        foreach ($tapal_attach_file as $tattachment) 
        {
           $tapal_documentname=$tattachment->document_name;
           $tapal_attach_createdat=$tattachment->created_at->format('d-M-Y h:i:s');

           if($tapal_documentname)
           {
              $filehistorystr[$i]['date'] = $tapal_attach_createdat;
              $filehistorystr[$i]['filestr'] = "<div class='col-md-12'><h3 class='panel-title'><i class='fa fa-fw fa-caret-right'></i>Attachment $tapal_documentname was attached on $tcreatedat :: $tapal_no</h3></div><br>";
              $i++;
           }
        }
        /////////////////////////////////////////////////////////////////////////////
      }
      ///////////file action /////////////////////////////////////////////////////
      $actions =\App\File_action::where('file_id', $id)->with('seat','action')->get();
      foreach ($actions as $action) 
      {
         $actiondescription=$action->action_description;
         $seat=$action->seat->seat_name;
         $createdat=$action->created_at->format('d-M-Y h:i:s');

         if($actiondescription)
         {
            $filehistorystr[$i]['date'] = $createdat;
            $filehistorystr[$i]['filestr'] = "<div class='col-md-12'><h3 class='panel-title'><i class='fa fa-fw fa-caret-right'></i>Actions was written by $seat on $createdat</h3></div><br>";
            $i++;
         }
      }
      ///////////file attachments /////////////////////////////////////////////////////
      $file_attchment =\App\Documents::where('file_id', $id)->get();
      foreach ($file_attchment as $fattachment) 
      {
         $file_documentname=$fattachment->document_name;
         $file_attach_createdat=$fattachment->created_at->format('d-M-Y h:i:s');

         if($file_documentname)
         {
            $filehistorystr[$i]['date'] = $file_attach_createdat;
            $filehistorystr[$i]['filestr'] = "<div class='col-md-12'><h3 class='panel-title'><i class='fa fa-fw fa-caret-right'></i>Attachment $file_documentname was attached on $createdat</h3></div><br>";
            $i++;
         }
      }
      ///////file forwards//////////////////////////////////
      $fileforwards =\App\File_forward::where('file_id', $id)->get();
      foreach ($fileforwards as $fileforward) 
      {
         
          $fromseat=$fileforward->from_seat;               
          $toseat=$fileforward->to_seat;
          $capture_flag=$fileforward->capture_flag;

          $createdat=$fileforward->created_at->format('d-M-Y h:i:s');
         
          if(!$fromseat)
          {
              $forwardtoseatobj=\App\Seat::where('id',$toseat)->first();
              $toseat_new=$forwardtoseatobj->seat_code."::".$forwardtoseatobj->seat_name;
              $seat_str="Self Generated by ".$toseat_new;
          }
          else if ($capture_flag==1)
          {
              $forwardseatobj=\App\Seat::where('id',$fromseat)->first();
              $fromseat=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
              $forwardtoseatobj=\App\Seat::where('id',$toseat)->first();
              $toseat_new=$forwardtoseatobj->seat_code."::".$forwardtoseatobj->seat_name;
              $seat_str="Captured From ".$fromseat;   
          }
          else
          {
              $forwardseatobj=\App\Seat::where('id',$fromseat)->first();
              $fromseat=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
              $forwardtoseatobj=\App\Seat::where('id',$toseat)->first();
              $toseat_new=$forwardtoseatobj->seat_code."::".$forwardtoseatobj->seat_name;
              $seat_str="Forwarded to ".$toseat_new." From ".$fromseat;
          }

          $forwardedon=$fileforward->created_at->format('d-M-Y');
          $fdate=new \DateTime($forwardedon);
          $cdate=new \DateTime();

          $diff='';
          $diff=date_diff($fdate,$cdate);
          $noofdays= $diff->format("%a days");

          if($seat_str)
          {
            $filehistorystr[$i]['date'] = $createdat;
            $filehistorystr[$i]['filestr'] = "<div class='col-md-12'><h3 class='panel-title'><i class='fa fa-fw fa-caret-right'></i>$filetitle :: $fileno was $seat_str on $createdat ($noofdays)</h3></div><br>";
            $i++;
          }
      }

      // Sort the tapalhistorystr arraywith date///////////////////////////////////
      foreach ($filehistorystr as $key => $row) 
      {
          $filehistorystr_date[$key]  = $row['date'];
      }
      // Sort the data with date ascending
      // Add $data as the last parameter, to sort by the common key
      $filehistorystr_final = array_multisort($filehistorystr_date, SORT_ASC, $filehistorystr);
      ///////////////////////end sort////////////////////////////////////////////////
      $file_history_view='';
      foreach($filehistorystr as $filehistorystr_list)
      {
          $file_history_view .= $filehistorystr_list['filestr'];
      }
      return $file_history_view;
}
/////end history modal/////////////////////
///////comment modal/////////////////////////
      public function getcomment1($id)
       {
        $userid = Auth::user()->id;
        $comments_arr = \App\Comments::where('file_id', $id)->with('file','seat')->orderBy('created_at','DESC')->get();
        
       $commentstr ='';
       $comments_count = \App\Comments::where('file_id', $id)->with('file','seat')->count();
      
        
        foreach ($comments_arr as $comments_list)
        {
            $created_at=$comments_list->created_at->format('d-M-Y');
            $fileid=$comments_list->file_id;
            $comment_type=$comments_list->comment_type;
            $comment_description=$comments_list->comment_description;
            $seatname=$comments_list->seat->seat_name;
            $commentseatid=$comments_list->seat_id;

           $checkforseatmatch=\App\User_Seat::where('user_id',$userid)->where('seat_id',$commentseatid)->count();
            $currentseat=\App\User_Seat::where('user_id',$userid)->with('seat')->get();
            foreach ($currentseat as $userseat)
              {
                $currentuserseat=$userseat->seat_id;
                //dd($currentuserseat);
                $currenttapalseat=\App\File_forward::where('file_id',$fileid)->where('active_flag',1)->first();
                //foreach ($currenttapalseat as $tapalseat)
                //{
                $seat=$currenttapalseat->to_seat;
                // dd($seat);
                //}
                $seatflag=0;
                if($currentuserseat==$seat)
                  {
                     $seatflag=1;
                  }

              }


           
           
              if(($comment_type==1)&&($checkforseatmatch>0)) 
              {    
              $commentstr .= "<div class='col-md-12'><input type='hidden' name='creation_of_file' id='creation_of_file' value='$id'><div class='panel panel-danger'><div class='panel-heading'>$seatname noted at $created_at</div><div class='panel-body'><div class='form-group'><div class='col-md-6'>$comment_description</div></div></div></div></div>";
              }

              if($comment_type==0) 
              {
                
                $commentstr .= "<div class='col-md-12'><input type='hidden' name='creation_of_file' id='creation_of_file' value='$id'><div class='panel panel-success'><div class='panel-heading'>$seatname noted at $created_at</div><div class='panel-body'><div class='form-group'><div class='col-md-6'>$comment_description</div></div></div></div>     </div>";

              }

               if(($comment_type==2)  && ($comments_list->disgard_flag==0))
              {
                
                $commentstr .= "<div class='col-md-12'><form class=form-horizontal action='".url('filepostit_comment')."'  method='post' > <input type='hidden' name='_token' value=".csrf_token()."><input type='hidden' name='creation_of_file' id='creation_of_file' value='$id'><input type='hidden' name='comment_id' id='comment_id' value='$comments_list->id'><div class='panel panel-warning'><div class='panel-heading'>$seatname noted at $created_at</div>  </form><div class='panel-body'><div class='form-group'><div class='col-md-6'>$comment_description</div></div></div></div>  </div>";

                if($seatflag==1)
                  {
                    $commentstr .= "<button type='submit' class='btn btn-default btn-xm'> <span class='glyphicon glyphicon-trash'></span> Trash </button> "; 
                  }

              }
             
             
             
          }

          //attached tapal comments
          $tapalcomments_arr = \App\File_reference::where('file_id', $id)->with('tapal')->get();
          foreach ($tapalcomments_arr as $tapalcomment_list)
          {
           

           $tapalid=$tapalcomment_list->tapal_id;

           $comments_count += \App\Comments::where('tapal_id', $tapalid)->with('tapal','seat')->count();

           $comments = \App\Comments::where('tapal_id', $tapalid)->with('tapal','seat')->orderBy('created_at','DESC')->get();
           //dd($comments);
           foreach ($comments as $com)
           {
             $created_at=$com->created_at->format('d-M-Y');
             $tapalno=$com->tapal->tapal_no;
             $comment_type=$com->comment_type;
             $comment_description=$com->comment_description;
             $seatname=$com->seat->seat_name;
             $commentseatid=$com->seat->seat_name;

           $checkforseatmatch=\App\User_Seat::where('user_id',$userid)->where('seat_id',$commentseatid)->count();

          if(($comment_type==1)&&($checkforseatmatch>0)) 
            {    
              $commentstr .= "<div class='col-md-12'><div class='panel panel-danger'><div class='panel-heading'>$seatname noted at $created_at  $tapalno</div><div class='panel-body'><div class='form-group'><div class='col-md-6'>$comment_description</div></div></div></div></div>";
            }

          if($comment_type==0) 
            {
              
              $commentstr .= "<div class='col-md-12'><div class='panel panel-success'><div class='panel-heading'>$seatname noted at $created_at  $tapalno</div><div class='panel-body'><div class='form-group'><div class='col-md-6'>$comment_description</div></div></div></div>     </div>";

            }

          if(($comment_type==2)  && ($com->disgard_flag==0))
            {
              
              $commentstr .= "<div class='col-md-12'><form class=form-horizontal action='".url('postit_comment')."'  method='post' > <input type='hidden' name='_token' value=".csrf_token()."><input type='hidden' name='creation_of_tapal' id='creation_of_tapal' value='$id'><input type='hidden' name='comment_id' id='comment_id' value='$comments_list->id'><div class='panel panel-warning'><div class='panel-heading'>$seatname noted at $created_at</div>  <button type='submit' class='btn btn-default btn-xm'> <span class='glyphicon glyphicon-trash'></span> Trash </button></form><div class='panel-body'><div class='form-group'><div class='col-md-6'>$comment_description</div></div></div></div>  </div>";

            }
                }//end of $comments foreach
          }

        if($comments_count==0)
       {
            $commentstr .= "<div class='col-md-12'><span class='primary'>No Comments added yet !!!</span</div>";
       }

          return $commentstr;
              
    
  }

  /////////////action insert in file inbox

    public function file_actionpost(Request $request)

    {

     $this->validate($request,[
             
            'actions' => 'required',
            'description' => 'required'
            
            
       ]);


      $userid = Auth::user()->id;
      
        $seatid_new = $request->session()->get('session_seat');
       

        $action_type=$request->actions;
        $action_description=$request->description;
        $file_id=$request->creation_of_file;
        
        
        $id = Auth::user()->id;

        $insert_id = \App\File_action::create([//model name database field name

          'action_type' => $action_type,
          'action_description' => $action_description,
          'file_id' => $file_id,
          'seat_id' => $seatid_new,
          
          'created_by'=>$userid
           ]);


         

      if($insert_id)
       // $sendmessage="Action Added!";
       notify()->success('Action Added!');


      else
        smilify('Error', 'Error Occurred Pls recheck!');
       // $sendmessage="Error Occurred Pls recheck!";
      
      return redirect()->back();


    }

  //////////////////////////

  //action
  public function getaction1($id)
{
  $userid = Auth::user()->id;
  $actions_arr = \App\File_action::where('file_id', $id)->with('file','seat','action')->orderBy('created_at','DESC')->get();
 

  $actionstr ='';

   $action_count = \App\File_action::where('file_id', $id)->with('file','seat','action')->count();
   

  foreach ($actions_arr as $actions_list)
  {
      $created_at=$actions_list->created_at->format('d-M-Y');
      $fileid=$actions_list->id;
      $action_type=$actions_list->action->actions;
      $action_description=$actions_list->action_description;
      $seatname=$actions_list->seat->seat_name;
      //$actionseatid=$actions_list->seat_id;
      
     
      if($action_type) 
      {    
        $actionstr .= "<div class='col-md-12'><div class='panel panel-info'><div class='panel-heading'><h3 class='panel-title'>$seatname created a $action_type on $created_at</h3></div><div class='panel-body'><div class='form-group'><div class='col-md-12'>$action_description</div></div></div></div>
          </div>";
        }


       
    }

        //attached tapal actions
      $tapalactions_arr = \App\File_reference::where('file_id', $id)->with('tapal')->get();
      foreach ($tapalactions_arr as $tapalactions_arr)
      {
       
       $tapalid=$tapalactions_arr->tapal_id;
       $action_count += \App\Tapal_action::where('tapal_id', $tapalid)->with('tapal','seat')->count();
       $actions = \App\Tapal_action::where('tapal_id', $tapalid)->with('tapal','seat')->orderBy('created_at','DESC')->get();
       //dd($comments);
       foreach ($actions as $act)
       {
         $created_at=$act->created_at->format('d-M-Y');
         $tapalno=$act->tapal->tapal_no;
         $action_type=$act->action_type;
         $action_description=$act->action_description;
         $seatname=$act->seat->seat_name;
        
          if($action_type) 
          {    
            $actionstr .= "<div class='col-md-12'><div class='panel panel-info'><div class='panel-heading'><h3 class='panel-title'>$seatname    $tapalno</h3></div><div class='panel-body'><div class='form-group'><div class='col-md-12'>$action_description</div></div></div></div>
              </div>";
          }
      
        }//end of $comments foreach
      }

      if($action_count==0)
       {
            $actionstr .= "<div class='col-md-12'><span class='primary'>No Actions added yet !!!</span</div>";
       }

    return $actionstr;
  }


  /////attachment insertion in file inbox

    public function file_attachmentpost1(Request $request)

    {

     $this->validate($request,[
             
            'upload1' => 'required'
            
            
       ]);
    // $id = Auth::user()->id;

      $userid = Auth::user()->id;
      
     
        $seatid_new = $request->session()->get('session_seat');
       
        $uploadcount=$request->uploadcount;
        $file_id=$request->creation_of_file;
        for($i=1;$i<=$uploadcount;$i++)
        {

          $filename="upload".$i;
          $filedescrem="filedesc".$i;
          $filedesc=$request->$filedescrem;
          if ($request->hasFile($filename)) {
  

           

            $fileobj=$request->file($filename)->getClientOriginalName();

        $destpath="uploads";

        $custom_file_name = time().'-'.$userid.'-'.$fileobj;

        $path = $request->file($filename)->storeAs($destpath,$custom_file_name);

       
        $document_id = \App\Documents::create([//model name database field name
          'file_id' => $file_id,
          'document_path' => $path,
          'document_name' => $filedesc,
          'updated_by'=>$userid
           ]);


      }

        }


      if($document_id)
       // $sendmessage="Attachment Added!";
        notify()->success('Attachment Added!');

      else
       // $sendmessage="Error Occurred Pls recheck!";
        smilify('Error', 'Error Occurred Pls recheck!');
      return redirect()->back();

    }


  //////////////////////////


  //attachments
    public function getattachment1($id)
    {
      $userid = Auth::user()->id;
      $attachments_arr = \App\Documents::where('file_id', $id)->with('file')->orderBy('created_at','DESC')->get();
       
      //dd($attachments_arr);
      $attachmentstr ='';
      
      $data  = [];
       $attachment_count = \App\Documents::where('file_id', $id)->with('file')->count();

    
            $i=0;

    foreach ($attachments_arr as $attachments_list)
    {
        $created_at=$attachments_list->created_at->format('d-M-Y');
        $fileid=$attachments_list->id;
        $document_path=$attachments_list->document_path;
        $document_name=$attachments_list->document_name;
        
        //$seatname=$attachments_list->seat->seat_name;
      
     
      if($document_path) 
      {    
        $attachmentstr = "<div class='col-md-12'><div class='panel panel-info'><div class='panel-heading'><h3 class='panel-title'>Attachments</h3></div><div class='panel-body'><div class='form-group'><div class='col-md-12'>   <div ><a href='#' onclick=showdocument('$document_path');>$document_name::View File</a></div> <div class='col-md-10' align='center'></div></div></div></div></div> </div>";
        }

       
                  $data[$i] = $attachmentstr;
                $i++;

        
        
      }

           //attached tapal attachments
          $tapalattachments_arr = \App\File_reference::where('file_id', $id)->with('tapal')->get();
          foreach ($tapalattachments_arr as $tapalattachment_arr)
          {
           

           $tapalid=$tapalattachment_arr->tapal_id;
           $attachment_count += \App\Documents::where('tapal_id', $tapalid)->with('tapal')->count();
           $attachments = \App\Documents::where('tapal_id', $tapalid)->with('tapal')->orderBy('created_at','DESC')->get();
           //dd($comments);
           $attachmentstr='';
           foreach ($attachments as $att)
           {
             $created_at=$att->created_at->format('d-M-Y');
             $tapalno=$att->tapal->tapal_no;
             $document_path=$att->document_path;
             $document_name=$att->document_name;
           
            
              if($document_path) 
                {    
                  $attachmentstr = "<div class='col-md-12'><div class='panel panel-info'><div class='panel-heading'><h3 class='panel-title'>Attachments   $tapalno</h3></div><div class='panel-body'><div class='form-group'><div class='col-md-12'>  <div ><a href='#' onclick=showdocument('$document_path');>$document_name::View File</a></div><div class='col-md-10' align='center'>  </div>
                    </div></div></div></div></div>";

                    $data[$i] = $attachmentstr;
                $i++;
                  }
          
                }//end of $comments foreach
                
          }

           if($attachment_count==0)
             {
                  $attachmentstr .= "<div class='col-md-12'><span class='primary'>No Attachments uploaded yet !!!</span</div>";
                  $data[0] = $attachmentstr;
             }

     return $data;
         
  }

  public function tapalreport(Request $request)
  {
    $userid = Auth::user()->id;
    $seatid=$request->session()->get('session_seat');
    $inboxtapals = \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')->get();

      $i=0;
    $data=[];
          foreach($inboxtapals as $inboxtapal)
          {
                  $id=$inboxtapal->id;
                  //tapalno
                  $tapalno=$inboxtapal->tapal->tapal_no;
                  $priority=$inboxtapal->tapal->priority;
                  $prioritylabelobj=\App\Priority::where('id',$priority)->first();
                  //tapalpriority
                  $priority=$prioritylabelobj->priority;
                  $subject=$inboxtapal->tapal->subject_id;
                  $subjectobj=\App\Subject::where('id',$subject)->first();
                  //tapalsubject
                  $subjects=$subjectobj->subject;
                  //tapaltitle
                  $title=$inboxtapal->tapal->title;
                   //tapalforwardedby
                   $forwardedby=$inboxtapal->from_seat;
                   if(!$forwardedby)
                    $forwardedby="Self Generated";
                  else
                  {
                    $forwardseatobj=\App\Seat::where('id',$forwardedby)->first();
                    $forwardedby=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
                  
                  }
      //dd($inboxtapals);
                
                      $forwardedon=$inboxtapal->created_at->format('d-M-Y');
                      $fdate=new \DateTime($forwardedon);
                      $cdate=new \DateTime();

                      $diff='';
                      $diff=date_diff($fdate,$cdate);
                      $noofdays= $diff->format("%a days");
                     
                      if($noofdays>10)
                       {  
                          $data[$i]['tapalno']=$tapalno;
                          $data[$i]['title']=$title;
                          $data[$i]['subject']=$subjects;
                          $data[$i]['priority']=$priority;
                          $data[$i]['forwardedby']=$forwardedby;
                          $data[$i]['pendingdays']=$noofdays;
                           $i++;
                       }

      

           }
                 return view('tapalreports',compact('data'));
  }

public function filereport(Request $request)
    {
      $userid=Auth::user()->id;
      $seatid=$request->session()->get('session_seat');
      $inboxfiles = \App\File_forward::whereHas('file', function ($query) {
              $query->with('user');
          })->where('to_seat', $seatid)->where('active_flag',1)->with('file')->orderBy('created_at','desc')->get();
            //dd($inboxs);
      $data  = [];
            $i=0;

         foreach ($inboxfiles as $inboxfile)
            {
              $id=$inboxfile->id;
              //fileno
              $fileno=$inboxfile->file->file_no;
                            //filetitle
              $title=$inboxfile->file->title;
              //filepriority
              $priority=$inboxfile->file->priority;
              $filegroup=$inboxfile->file->file_group_id;
              $filearr=\App\File_Group::where('id',$filegroup)->first();
              //filesub
              $filesub=$filearr->file_group;
              $referencenumber=$inboxfile->file->reference_number;
              $forwardedby=$inboxfile->from_seat;
               if(!$forwardedby)
                $forwardedby="Self Generated";
              else
              {
                $forwardseatobj=\App\Seat::where('id',$forwardedby)->first();
                $forwardedby=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
              
              }
 /////////////////////////////////////////
                  $forwardedon=$inboxfile->created_at->format('d-M-Y');
                  $fdate=new \DateTime($forwardedon);
                  $cdate=new \DateTime();

                  $diff='';
                 $diff=date_diff($fdate,$cdate);
                 $noofdays= $diff->format("%a days");
                   if($noofdays>10)
                      {
                        $data[$i]['fileno']=$fileno;
                        $data[$i]['filetitle']=$title;
                        $data[$i]['filegroup']=$filesub;
                        $data[$i]['referenceno']=$referencenumber;
                        $data[$i]['priority']=$priority;
                        $data[$i]['forwardedby']=$forwardedby;
                        $data[$i]['pendingdays']=$noofdays;
                        $i++;

                 }

        }
         return view('filereports1',compact('data'));

}
/////////////////////////////tapal and file report for admin...

public function tapalcountlist(Request $request )
   {

      $seats=\App\Seat::get();
      $data=[];
      $i=0;
      foreach($seats as $seat)
         {
          $seatid=$seat->id;
          $seatcode=$seat->seat_code;
          $seatname=$seat->seat_name;
          $inboxfilecount=0;
          $inboxtapalcount=0;
           $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();
                 if(($inboxtapalcount>=0)||($inboxfilecount>=0))
                         {
                                  $data[$i]['seatid']=$seatid;
                                  $data[$i]['seatcode']=$seatcode;
                                  $data[$i]['seatname']=$seatname;
                                  $data[$i]['tapalcount']=$inboxtapalcount;
                                  $data[$i]['filecount']=$inboxfilecount;
                                  $i++;

                          }
          }
          
            return view('admintflist',compact('data'));

          }
/////////////////////// pending tapals and files for seats for morethan 10 days

 public function pendingtapalcountlist(Request $request)

      {
            $data=[];
            $data1=[];
            $i=0;
            $j=0;
            $seats=\App\Seat::get();
              foreach($seats as $seat)
                {
                    $seatid=$seat->id;
                    $seatcode=$seat->seat_code;
                    $inboxtapals = \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')->get();
                    foreach($inboxtapals as $inboxtapal)
                    {
                       $tapalno=$inboxtapal->tapal->tapal_no;
                       $forwardedon=$inboxtapal->created_at->format('d-M-Y');
                       $fdate=new \DateTime($forwardedon);
                       $cdate=new \DateTime();
                       $diff='';
                       $diff=date_diff($fdate,$cdate);
                       $noofdays= $diff->format("%a days");
                        if($noofdays>10)
                           {  
                                $data[$i]['seatid']=$seatid;
                                $data[$i]['seatcode']=$seatcode;
                                $data[$i]['tapalno']=$tapalno;
                                $data[$i]['pendingdays']=$noofdays;
                                $i++;
                            }
                    }
                    
 ////////////////files
                    $inboxfiles = \App\File_forward::whereHas('file', function ($query) {
              $query->with('user');
          })->where('to_seat', $seatid)->where('active_flag',1)->with('file')->orderBy('created_at','desc')->get();
                      foreach($inboxfiles as $inboxfile)
                      {
                          $fileno=$inboxfile->file->file_no;
                          $forwardedfon=$inboxfile->created_at->format('d-M-Y');
                          $fdate=new \DateTime($forwardedfon);
                          $cdate=new \DateTime();
                          $diffe=date_diff($fdate,$cdate);
                          $noofdays=$diffe->format("%a days");
                            if($noofdays>10)
                            {
                              $data1[$j]['seatid']=$seatid;
                              $data1[$j]['seatcode']=$seatcode;
                              $data1[$j]['fileno']=$fileno;
                              $data1[$j]['pendingdays']=$noofdays;
                              $j++;
                            }
                      }
              }
              
                
        return view('pendingtflists',compact('data','data1'));

}
//end of InboxController function
  }