<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


use Auth;
use App;

use Yajra\Datatables\Datatables;

class OutboxController extends Controller
{

    //

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

//Tapal outbox

public function outbox(Request $request)
     {

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

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

      $seatid=$request->session()->get('session_seat');
      $department=\App\Department::all();

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

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

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

      $comments = \App\Comments::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();

      $tapal_arr = \App\Tapal_forward::where('from_seat', $seatid)->where('active_flag',0)->orderBy('created_at','desc')->get();

      //$values = \App\Comments::where('tapal_id',$tapal_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('outbox', compact('department','section','tapal_arr','seatid','actions','priority','comments','tapalinboxcount','fileinboxcount','tapaloutboxcount','fileoutboxcount'));

     }

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

       $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');
       $forwardedon=$tapalfrwrd->created_at->format('d-M-Y');
       $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');

         $filepath=storage_path('app/'.$path);
         $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_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>";
                  }


                 
              }  


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


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


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





     return $tapaldetailsarr;

     }



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

      $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','priority','priorityfile'));

     }


    public function outboxjson(Request $request)
    {

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

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

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

      $data  = [];
            $i=0;

      foreach ($outboxs as $outbox)
      {
        $id=$outbox->id;
        $tapalno=$outbox->tapal->tapal_no;
       $tapalid=$outbox->tapal->id;
        $priority=$outbox->tapal->priority;
        $createdby=$outbox->tapal->user->name;
        $prioritylabelobj=\App\Priority::where('id',$priority)->first();

        $priority=$prioritylabelobj->priority;
        $valuables=$outbox->tapal->has_valuables;
        $date=$outbox->tapal->created_at->format('d-M-Y');;
        $title=$outbox->tapal->title;
         $confidential=$outbox->tapal->confidential;
         ////////////to get the forwarded by seat////////////////////
         $forwardedby=$outbox->from_seat;
         if(!$forwardedby)
          $forwardedby="Self Generated";    //////////////first time from_seat will be 0////////////////
        else
        {
          $forwardseatobj=\App\Seat::where('id',$forwardedby)->first();
          $forwardedby=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
        
        }
        //to get forwarded to_seat
       // $outbox_forwrd_toseat = \App\Tapal_forward::where('from_seat', $seatid)->where('tapal_id',$tapalid)->first();
        //dd($outbox_forwrd_toseat);
         $forwardedto=$outbox->to_seat;
         
          $forwardtoseatobj=\App\Seat::where('id',$forwardedto)->first();
          $forwardedtonew=$forwardtoseatobj->seat_code."::".$forwardtoseatobj->seat_name;
        
          /////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();

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

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

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

         $subjectgroup=$outbox->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->to_seat = $forwardedtonew;
                $obj->forwardedon = $forwardedon."(".$noofdays.")" ;
                $obj->subject_group = $subjectgroup;
                $obj->priority = $priority;
                $obj->tapalcommentcount = $tapalcommentcount;
                $obj->tapalactioncount = $tapalactioncount;
                $obj->tapalattachmentcount = $tapalattachmentcount;
                //$obj->priority = $priority;
                 $obj->confidential = $confidential;
                 $obj->valuables = $valuables;

                $data[$i] = $obj;
                $i++;
          }
               
            $outboxjson = collect($data);
     
        
            return Datatables::of($outboxjson)
                    ->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 ='';
  
  foreach ($comments_arr as $comments_list)
  {
      $created_at=$comments_list->created_at->format('d-M-Y');
      $tapalid=$comments_list->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();



     
     
      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</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</div><div class='panel-body'><div class='form-group'><div class='col-md-6'>$comment_description</div></div></div></div>     </div>";

        }
       
       
       
    }
    return $commentstr;
        
    
  }

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

  $actionstr ='';

  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_type;
      $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</h3></div><div class='panel-body'><div class='form-group'><div class='col-md-12'>$action_description</div></div></div></div>
          </div>";
        }


       
    }
    return $actionstr;
  }


     

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

  $attachmenstr ='';
  $data  = [];
            $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'><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 onclick='showdocument('$document_path');''>$document_name::View File</div>
           <div class='col-md-10' align='center'>
            
           </div>

          </div></div></div></div>
          </div>";
        }

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

        
        
    }
     return $data;
         
  }


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

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

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

      

      $seatid=$request->session()->get('session_seat');
      $department=\App\Department::all();

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

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


      $file_arr = \App\File_forward::where('from_seat', $seatid)->where('active_flag',0)->get();

      //$values = \App\Comments::where('tapal_id',$tapal_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('fileoutbox', compact('department','section','file_arr','seatid','priority','tapalinboxcount','fileinboxcount','tapaloutboxcount','fileoutboxcount'));

     }

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

      $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','priorityfile','priority'));

     }


     public function fileoutboxjson(Request $request)
    {

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

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

      $data  = [];
            $i=0;

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

        $date=$outbox->file->created_at->format('d-M-Y');;
        $title=$outbox->file->title;
         $referencenum=$outbox->file->reference_number;

         $forwardedby=$outbox->from_seat;
         if(!$forwardedby)
          $forwardedby="Self Generated";
        else
        {
          $forwardseatobj=\App\Seat::where('id',$forwardedby)->first();
          $forwardedby=$forwardseatobj->seat_code."::".$forwardseatobj->seat_name;
          if($capture_flag==1)
          {
              $fwd_label = "Captured By ";
          }
          else
          {
              $fwd_label = "Forwarded To ";
          }
        }

         //to get forwarded to_seat
       // $outbox_forwrd_toseat = \App\Tapal_forward::where('from_seat', $seatid)->where('tapal_id',$tapalid)->first();
        //dd($outbox_forwrd_toseat);
         $forwardedto=$outbox->to_seat;
         
          $forwardtoseatobj=\App\Seat::where('id',$forwardedto)->first();
          $forwardedtonew=$forwardtoseatobj->seat_code."::".$forwardtoseatobj->seat_name;
        
        ///////////////////////////////////////////////////////////////////
        /////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=$outbox->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=$outbox->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->priority = $priority;
                $obj->createdby = $createdby;
                $obj->confidential = $confidential;
                $obj->reference_number = $referencenum;
                $obj->to_seat = $forwardedtonew;
                $obj->from_seat = $forwardedby;
                $obj->fwd_label = $fwd_label;
                $obj->forwardedon = $forwardedon."(".$noofdays.")" ;
                $obj->file_group = $filegroup;
                $obj->filecommentcount = $total_comment_count;
                $obj->fileactioncount = $total_fileaction_count;
                $obj->fileattachmentcount = $total_fileattachment_count;            
                $data[$i] = $obj;
                $i++;

      }

         

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


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


      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 ='';
        
        foreach ($comments_arr as $comments_list)
        {
            $created_at=$comments_list->created_at->format('d-M-Y');
            $fileid=$comments_list->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();



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

              }
             
             
             
          }
          return $commentstr;
              
    
  }

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

  $actionstr ='';

  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_type;
      $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</h3></div><div class='panel-body'><div class='form-group'><div class='col-md-12'>$action_description</div></div></div></div>
          </div>";
        }


       
    }
    return $actionstr;
  }


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

  $attachmenstr ='';
  $data  = [];
            $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++;

        
        
    }
     return $data;
         
  }

  //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');
       $forwardedon=$filefrwrd->created_at->format('d-M-Y');
       $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);
         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', $id)->with('tapal')->get();
        foreach ($tapalattachments_arr as $tapalattachment_arr)
        {

            $tapalid=$tapalattachment_arr->tapal_id;
            $tapalno=$tapalattachment_arr->tapal->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;

      }




  }