<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


use Auth;
use App;
use PDF;
use Yajra\Datatables\Datatables;

class FileController extends Controller
{

    //
    ///GLOBAL VARIABLE FOR GET THE CHILD SEATS FROM PARENT SEAT///////////////
    protected $childarray=[];
    ///////////////////////////////////////////////////////////////////

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



//creation of file

    public function creation_of_file(Request $request)
    {
       
       $office_id=$request->session()->get('session_office');
        $file_groups=App\File_Group::where('office_id',$office_id)->get();

        $id = Auth::user()->id;
       
        //dd($currentseat);
       $priority=App\Priority::where('office_id',$office_id)->get();

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

        //$seatid_new = $us_arrlist->seat_id;
        //$seatname= $us_arrlist->seat_name;


       $permission=\App\Permission::where('permission','Creation of File')->first();
       $count =\App\Seat_Permission::where('seat_id',$seat)->where('permission_id',$permission->id)->count();
      
        if($count==0)
        {
          return view('permissiondenied', compact('permission','seat'));
        }



        //get tapal number
        $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;

        return view('creation_of_file', compact('seat','file_groups','fileprint','priority','office_id'));
        
    }


     public function permissiondenied()
    {
        return view('permissiondenied', compact('permission','seat'));

    }


    public function creation_of_filepost(Request $request)
    {
      $office_id=$request->session()->get('session_office');
      $this->validate($request,[
            //'fileno' => 'required',//name of blade name
            'title' => 'required',
            'description' => 'required',
            'referencenumber' => 'required',
            'priority' => 'required',
            'file_group' => 'required'
       ]);
      $id = Auth::user()->id;
      $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;
      $filetitle=$request->title;
      $description=$request->description;
      $referencenum=$request->referencenumber;
      $priority=$request->priority;
      $confidential=$request->confidential;
      $file_group=$request->file_group;
   	
      //////check max number of files exceeds or not///////////////////////////
        $max_num_files = 0;
        $file_count = 0;
        $seat_id_new ="";
        $next_file_count =0;
        ////api call to check the number of users///////////////////
        $user_checks = App\User::where('office_id',$office_id)->where('verified',1)->get();
        foreach($user_checks as $user_chk)
        {
          $user_id = $user_chk->id;
          $user_seat_obj = App\User_Seat::where('user_id',$user_id)->get();
          foreach($user_seat_obj as $user_seat_list)
          {
            $seat_id = $user_seat_list->seat_id;
            $role_seat_obj = App\Seat_Role::where('seat_id',$seat_id)->where('role_id',6)->first();
            if(isset($role_seat_obj))
            {
                $seat_id_new = $role_seat_obj->seat_id;
            }
          }
        }
        $user_seat_obj_new = App\User_Seat::where('seat_id',$seat_id_new)->first();
        if(isset($user_seat_obj_new))
        {
            $user_id_new = $user_seat_obj_new->user_id;
            $user_obj = App\User::where('id',$user_id_new)->where('verified',1)->first();
            if(isset($user_obj))
            {
              $user_email = $user_obj->email;
            }
        }
        //dd($user_email);
        $data = [
                          'email' => $user_email,
                          'product_slugname' => 'digifile',
                ];
        //$api_url = "http://139.59.43.152/raintelsplan/public/";
        $server_config = App\ServerConfig::first();
        if(isset($server_config))
        {
            $api_url = $server_config->server_url;
        }
        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => $api_url.'api/getplan',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($data),
        CURLOPT_HTTPHEADER => array(
                // Set here requred headers
                "accept: */*",
                "accept-language: en-US,en;q=0.8",
                "content-type: application/json",
                "X-Authorization: B78QZhy7rGG9EJvBQTa9bzVYzg4NT9jRdQ5JMV7rgmiDWCv0iBuaub4SAEXCBbP2",     //////api key generated for check user
            ),
        ));
        $response = curl_exec($curl);
        $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) 
        {
            echo "cURL Error #:" . $err;
        } 
        else 
        {
          //dd($response);
          $plan = json_decode($response);
          if(isset($plan))
          {
              $plan_name = $plan->plan_name;
              $payment_interval = $plan->payment_interval;
              if($payment_interval=="year")
            {
                $payment_interval_val = "annual";
            }
            if($payment_interval=="month")
            {
                $payment_interval_val = "monthly";
            }
            if($plan_name)
            {
              $prd_det = App\PaymentLink::where('slugname','=', $plan_name)->where('frequency',$payment_interval_val)->first();  
              if(isset($prd_det))
              {
                  $max_num_files = $prd_det->no_of_files;
              }
            }
            $file_count = App\File::where('office_id',$office_id)->count();
          }
        }
        $next_file_count = $file_count+1;
      ////////////////////////////////////////////////////////////////////////////
      if($next_file_count<=$max_num_files)
      {
        $id = Auth::user()->id;
        $insert_id = \App\File::create([//model name database field name
          'file_no' => $fileprint,
          'title' => $filetitle,
          'file_description' => $description,
          'reference_number' => $referencenum,
          'priority' => $priority,
          'confidential' => $confidential,
          'file_group_id' => $file_group,
          'created_by' =>$id,
          'updated_by'=>$id,
          'office_id' => $office_id,
          'status'=>0,
           ]);
        if($insert_id)
        {
          $id = Auth::user()->id;
          $creation_of_files =\App\File::where('id',$id)->first();
          //File Forward entry
          //get seat_id of current user       
          $seatid=$request->session()->get('session_seat');       
          $file_forward_id = \App\File_forward::create([//model name database field name
                  'file_id' => $insert_id->id,
                  'from_seat' => 0,
                  'to_seat' => $seatid,
                  'updated_by' =>$id,
                  'active_flag' => 1,
                  'read_flag' => 1
             ]);
          $uploadcount=$request->uploadcount;
          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().'-'.$id.'-'.$fileobj;
              $path = $request->file($filename)->storeAs($destpath,$custom_file_name);
              $document_id = \App\Documents::create([//model name database field name
                'file_id' => $insert_id->id,
                'document_path' => $path,
                'document_name' => $filedesc,
                'updated_by'=>$id
                 ]);
            }
          }
          notify()->success('File Added!');
          return redirect('fileinbox/');
        }
        else
         smilify('Error', 'Error Occurred Pls recheck!'); 
      }
      else
      {
          smilify('Error', 'Maximum Number of Files Exceeds for this Organization.'); 
      }
      return redirect()->back();
    }




public function creation_of_filepostdata(Request $request)
{
     
  $office_id=$request->session()->get('session_office');
  $id = Auth::user()->id;

        
     $creation_of_files =\App\File::where('office_id',$office_id)->with('file_groupobj','documents','priorityobj')->where('created_by',$id)->get();

//dd($creation_of_files);
            $data  = [];
            $i=0;

            foreach ($creation_of_files as $creation_of_file)
            {   
                $id=$creation_of_file->id;
                $filenum=$creation_of_file->file_no;//db name
                $filetitle=$creation_of_file->title;
                $desc=$creation_of_file->file_description;
                $referencenumber=$creation_of_file->reference_number;
                $priorities=$creation_of_file->priorityobj->priority;
                $conf=$creation_of_file->confidential;
                $file_group=$creation_of_file->file_groupobj->file_group;
                $updated_at=$creation_of_file->updated_at->format('d-M-Y');
                $attachments="";

                foreach($creation_of_file->documents as $document)
                {
                  $attachments.=$document->document_path." ";

                }

                $obj = new \stdClass;
                $obj->incid = $i + 1;
                $obj->id = $id;
                $obj->file_no = $filenum;
                $obj->title = $filetitle;
                $obj->file_description = $desc;
                $obj->reference_number = $referencenumber;
                $obj->priority = $priorities;
                $obj->confidential = $conf;
                $obj->file_group = $file_group;
                $obj->attachments = $attachments;
                $obj->updated_at = $updated_at;

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

            }

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

    
    //return Datatables::of(\App\Office::query())->make(true);
}

  public function newfilecomment(Request $request)

    {

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


      $userid = Auth::user()->id;
      
     
     
        $seatid_new = $request->session()->get('session_seat');
      
        $comment_type=$request->comment_type;
        $description=$request->commentdesc;
        //$tapal_id=$request->creation_of_comment;
        $file_id=$request->creation_of_file_id;
        //$tapal_id=$tapalid;
        //$hidden=$request->commentval;
        //dd($tapalid);
        



        $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
        smilify('Error', 'Error Occurred Pls recheck!');
      //  $sendmessage="Error Occurred Pls recheck!";

     // return $sendmessage;
      
      return redirect()->back();
      //return view('showcreation_of_tapaldata/'.$tapal_id);


    }

    public function newfileaction(Request $request)

    {

     $this->validate($request,[
             
            'action_type' => 'required',
            'actiondescription' => 'required'
            
            
       ]);


      
        $seatid_new = $request->session()->get('session_seat');
      
        $action_type=$request->action_type;
        $description=$request->actiondescription;
        //$tapal_id=$request->creation_of_comment;
        $file_id=$request->creation_of_file_id;
        //$tapal_id=$tapalid;
        //$hidden=$request->commentval;
        //dd($tapalid);
        
        if(strcmp($action_type, 'Clarification required') == 0) {
          $action_type=1;
        }
        else if(strcmp($action_type, 'Non Pertaining') == 0) {
          $action_type=2;
        }
        else if(strcmp($action_type, 'Close') == 0) {
          $action_type=3;
        }
        else if(strcmp($action_type, 'Testing') == 0) {
          $action_type=4;
        }


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

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

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


         

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

      else
        smilify('Error', 'Error Occurred Pls recheck!');
        //$sendmessage="Error Occurred Pls recheck!";

      //return $sendmessage;
      
      return redirect()->back();
      //return view('showcreation_of_tapaldata/'.$tapal_id);


    }

    






public function showcreation_of_filedata($id,Request $request)//show button
    {
      $office_id=$request->session()->get('session_office');
      $creation_of_file=\App\File::where('id',$id)->first();

      $file_group =\App\File_Group::where('office_id',$office_id)->get();

      $priorities=App\Priority::where('office_id',$office_id)->get();

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

      $actions=\App\Action::where('office_id',$office_id)->get();
      $comments =\App\Comments::where('file_id',$id)->with('seat','file')->orderBy('created_at','DESC')->get();

      $attachments =\App\Documents::where('file_id',$id)->with('file')->orderBy('created_at','DESC')->get();
      $attachmentcount =\App\Documents::where('file_id',$id)->with('file')->count();

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

      $office=\App\Office::where('id',$office_id)->get();
      $department=\App\Department::where('office_id',$office_id)->get();
      $section =\App\Section::where('office_id',$office_id)->get();
     
      $seatid=$request->session()->get('session_seat');

      $currentuserstatus = \App\File_forward::where('to_seat', $seatid)->where('file_id', $id)
                         ->where('active_flag',1)->count();

    
      return view('showcreation_of_filedata', compact('file_group','creation_of_file','userid','seatid','currentuserstatus','priorities','actions','comments','attachments','fileactions','attachmentcount','office','department','section'));

}

public function updatecreation_of_filedata(Request $request)//update button
    {

        //validation of fields
        $this->validate($request,[
        //form field(blade name)
            //'fileno' => 'required',//name of blade name
            'title' => 'required',
            'description' => 'required',
            'referencenumber' => 'required',
            'priority' => 'required',
            'file_group' => 'required',
            //'upload' => 'required'
       ]);


       //variable name
        //$filenum=$request->fileno;
        $title=$request->title;
        $desc=$request->description;
        $referencenumber=$request->referencenumber;
        $prio=$request->priority;
        $confd=$request->confidential;
        $file_grp=$request->file_group;
      

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

         $updateobj = \App\File::where('id',$request->creation_of_file_id)->first();

                // db name        //variable name
        //$updateobj->file_no = $filenum;
        $updateobj->title = $title;
        $updateobj->file_description = $desc;
        $updateobj->reference_number = $referencenumber;
        $updateobj->priority = $prio;
        $updateobj->confidential = $confd;
        $updateobj->file_group_id = $file_grp;       
        //$updateobj->attachments = $uploads;
        $updateobj->updated_by = $userid;

        $updateobj->save();
        $uploadcount=$request->uploadcount;
        $id = Auth::user()->id;
        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().'-'.$id.'-'.$fileobj;

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

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

        }


        }
    

      return redirect('showcreation_of_filedata/'.$request->creation_of_file_id);
    }

  public function filepostit_comment(Request $request)

        {

            $fileid=$request->creation_of_file;
            $commentid=$request->comment_id;
            //dd($commentid);
           
            $postcommentstatus = \App\Comments::where('id',$commentid)->where('comment_type',2)->where('disgard_flag',0)->update(['disgard_flag' => 1]);
            
         // dd($postcommentstatus);
            return redirect()->route('fileinbox');

        }


//file view
public function file_view($id,Request $request)
{
        
    $office_id=$request->session()->get('session_office');
    $file_id= $id; //id of the file

    //get file details
    $creation_of_file=\App\File::where('id',$id)->first();
    //dd($creation_of_file);
    $file_group =\App\File_Group::where('id',$creation_of_file->file_group_id)->first();

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

    $actions =\App\Action::where('office_id',$office_id)->get();

    $office=\App\Office::where('id',$office_id)->get();

    $department=\App\Department::where('office_id',$office_id)->get();

    $section =\App\Section::where('office_id',$office_id)->get();

    $seat =\App\Seat::where('office_id',$office_id)->get();

    $template =\App\Template::where('office_id',$office_id)->get();


    $approveduser='';
    $fileviewtemplatearr=[];
    $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;
    } 
    $commentscount=0;
     
    $tapalcomments_arr = \App\File_reference::where('file_id', $id)->with('tapal')->get();
    foreach ($tapalcomments_arr as $tapalcomment_list)
    {

        $tapalid=$tapalcomment_list->tapal_id;

        $commentscount += \App\Comments::where('tapal_id', $tapalid)->count();

        $comments[$tapalid] = \App\Comments::where('tapal_id', $tapalid)->with('tapal','seat')->orderBy('created_at','DESC')->get();

    }

    $tapalactionscount=0;
    $tapalactions=[];
    // $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;

        $tapalactionscount += \App\Tapal_action::where('tapal_id', $tapalid)->count();

        $tapalactions[$tapalid] = \App\Tapal_action::where('tapal_id', $tapalid)->with('tapal','seat')->orderBy('created_at','DESC')->get();

    }

    $tapalattachmentscount=0;
    $tapalattachments=[];

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

        $tapalid=$tapalattachment_arr->tapal_id;

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

        $tapalattachments[$tapalid] = \App\Documents::where('tapal_id', $tapalid)->with('tapal','user')->orderBy('created_at','DESC')->get();
    }
    $tapallinkscount = \App\File_reference::where('file_id', $id)->with('tapal','user')->count();
    $tapallinks = \App\File_reference::where('file_id', $id)->with('tapal','user')->get();
    $tapallinkseat="";
    foreach ($tapallinks as $tapallink) 
    {
           $tapallinkuser=$tapallink->user->id;
           $tapallinkseat =\App\User_Seat::where('user_id',$tapallinkuser)->with('seat')->first();
    }

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

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

    $attachments=\App\Documents::where('file_id',$file_id)->with('user')->get();
    foreach ($attachments as $userattach) 
    {
      $userattach_id=$userattach->user->id;
      $userseatattachment =\App\User_Seat::where('user_id',$userattach_id)->with('seat')->first();
    }

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

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

   
    $seatid=$request->session()->get('session_seat');
     $seatid_new=$seatid; //logged in seat

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



        //capture
      $comments="";
      
        $filestatus = \App\File::where('id',$file_id)->where('status',2)->count();
        if($filestatus>0)
        {
            return view('duplicatefile', compact('filestatus'));
        }
        else
        {
          $fileseat = \App\File_forward::where('file_id',$file_id)->where('active_flag',1)->first();
          $filetoseat=$fileseat->to_seat; //current seat of the file

          //get current seat user and seatname
          $currentseatoffileobj=\App\Seat::where('id',$filetoseat)->first();
         $currentseatofile=$currentseatoffileobj->seat_name;

         //get username of current file owner
         $userseatobj=\App\User_Seat::where('seat_id',$filetoseat)->with('user')->first();
          if(isset($userseatobj->user))
                   $currentuseroffile=$userseatobj->user->name;
          else
                  $currentuseroffile="No user assigned to this seat";


        }
        
        
        //get child seats of $seatid
        $charray=$this->getchildseats($seatid);
        $enablecapture="";
        if(in_array($filetoseat, $charray))
        {
          $enablecapture=1;
        }
        else
        {
          $enablecapture=0;
        }
        /*if($enablecapture==0)
          return view('denied', compact('enablecapture','filetoseat','seatid'));*/
        //dd($enablecapture);

      $act_flg_status = "";
      //dd($us_arr);
      $seatobj=\App\Seat::where('id',$seatid)->first();
        $seatname= $seatobj->seat_name;
      
        $active_flag = "";
      
      $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;
        }
        
      }
      //dd($act_flg_status);
      //////////////
      $fileconfflag=0;
       $newflag="";
      //echo "$filetoseat....$seatid_new";
      if($filetoseat!=$seatid_new)
      {
        //check if loggedin user is in fileforward fromseat for this fileid
        $fileckecking = \App\File_forward::where('file_id',$file_id)->where('from_seat',$seatid_new)->count();
        if($fileckecking==0)
        {
         $fileconf = \App\File::where('id',$file_id)->where('confidential',1)->count();
         if($fileconf>0)         
            $fileconfflag=1;
        }
         
      }
//return 4;
       if($fileconfflag==1)
          return view('denied', compact('fileconfflag','filetoseat','seatid_new'));
       //if the loggedin user doesnt comes in the history of file
      $filecondition = \App\File_forward::where('file_id',$file_id)->where('to_seat',$seatid_new)->count();
      //dd($filecondition);
     // return 5;
      if($filecondition==0)
      {
        $fileconfflag=1;

      }
      
        $charray=$this->getchildseats($seatid_new);
        
        if(in_array($filetoseat, $charray)||($fileconfflag==0))
        {
          $newflag=1;
        }
        else
        {
          $newflag=0;
        }
        if($filetoseat==$seatid_new)
        {
          $newflag=1;
        }
        if($newflag==0)
          return view('denied', compact('newflag','filetoseat','seatid_new'));
     
  
      ////////////////get atleast one count of current user/////////////////////////////////////// 
      //$file_comment_count = \App\Comments::where('file_id',$file_id)->where('seat_id','=',$seatid_new)->count();
      $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;
      }
      ///////////////////////////////////////////////////////////////////////////////////////////////

      $file_forward = \App\File_forward::where('to_seat', $seatid_new)
          ->where('file_id', $file_id)->where('read_flag',0)
          ->update(['read_flag' => 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;
      $userseatattachment =\App\User_Seat::where('user_id',$userid)->with('seat')->first();

      
     return view('file_view', compact('values','values1','seatid_new','username','file_group','creation_of_file','department','section','seat',
      'act_flg_status','fileprint','seatname','attachmentcount','attachments','actions','ftemplate','template','tapalcomments_arr','comments',
      'tapalactions_arr','tapalactions','tapalattachments_arr','tapalattachments','commentscount','active_flag','tapalactionscount',
      'tapalattachmentscount','priority','tapallinks','tapallinkseat','tapallinkscount','fileviewtemplatearr','user_comment_flag','fromseat',
      'toseat','readflag','enablecapture','office','fileconfflag','newflag','filestatus','currentseatofile','currentuseroffile','userseatattachment'));

     }

     public function denied()
    {
        return view('denied', compact('fileconfflag','filetoseat','seatid_new','newflag'));

    }

    public function duplicatefile()
    {
        return view('duplicatefile', compact('filestatus'));

    }


     //pull back/////////////
      public function move_to_inboxfile(Request $request)

        {

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

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


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


//modal for view the template with label//////////
        public function getapprovetemp($id)
          {
          $userid = Auth::user()->id;
          $approve_arr = \App\Fileview_template::where('id', $id)->where('approval_flag',1)->with('file','template')->get();
          //dd($approve_arr);

         

          foreach ($approve_arr as $approve_list)
          {
              $approvearr['top'] = $approve_list->top;
              $approvearr['toplabel'] = $approve_list->template->top;
              $approvearr['top1'] = $approve_list->top1;
              $approvearr['top1label'] = $approve_list->template->top1;
              $approvearr['top2'] = $approve_list->top2;
              $approvearr['top2label'] = $approve_list->template->top2;
              $approvearr['top3'] = $approve_list->top3;
              $approvearr['top3label'] = $approve_list->template->top3;
              $approvearr['mid'] = $approve_list->mid;
              $approvearr['midlabel'] = $approve_list->template->mid;
              $approvearr['mid1'] = $approve_list->mid1;
              $approvearr['mid1label'] = $approve_list->template->mid1;
              $approvearr['mid2'] = $approve_list->mid2;
              $approvearr['mid2label'] = $approve_list->template->mid2;
              $approvearr['mid3'] = $approve_list->mid3;
              $approvearr['mid3label'] = $approve_list->template->mid3;
              $approvearr['bottom'] = $approve_list->bottom;
              $approvearr['bottomlabel'] = $approve_list->template->bottom;
              $approvearr['bottom1'] = $approve_list->bottom1;
              $approvearr['bottom1label'] = $approve_list->template->bottom1;
              $approvearr['bottom2'] = $approve_list->bottom2;
              $approvearr['bottom2label'] = $approve_list->template->bottom2;
              $approvearr['bottom3'] = $approve_list->bottom3;
              $approvearr['bottom3label'] = $approve_list->template->bottom3;
             
               
            }
            return $approvearr;
          }
//////close of modal////////////////

          public function word($id)
              {

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

                 $file =\App\File::all();

               //$ftemplate =\App\Fileview_template::where('template_id',$template->id)->with('template')->first();

               $word_arr = \App\Fileview_template::where('id', $id)->where('approval_flag',1)->with('file','template')->get();

                  return view('word',compact('word_arr','template','file'));

                  
              }

//get details (word format) for template//////////////         

       public function wordtemppost(Request $request)
          {
          $userid = Auth::user()->id;

          $id=$request->fileview_templateapprove_id;

          $word_arr = \App\Fileview_template::where('id', $id)->where('approval_flag',1)->with('file','template')
          ->get();
          //dd($approve_arr);

         

          foreach ($word_arr as $word_list)
          {
              $wordarr['top'] = $word_list->top;
              $wordarr['toplabel'] = $word_list->template->top;
              $wordarr['top1'] = $word_list->top1;
              $wordarr['top1label'] = $word_list->template->top1;
              $wordarr['top2'] = $word_list->top2;
              $wordarr['top2label'] = $word_list->template->top2;
              $wordarr['top3'] = $word_list->top3;
              $wordarr['top3label'] = $word_list->template->top3;
              $wordarr['mid'] = $word_list->mid;
              $wordarr['midlabel'] = $word_list->template->mid;
              $wordarr['mid1'] = $word_list->mid1;
              $wordarr['mid1label'] = $word_list->template->mid1;
              $wordarr['mid2'] = $word_list->mid2;
              $wordarr['mid2label'] = $word_list->template->mid2;
              $wordarr['mid3'] = $word_list->mid3;
              $wordarr['mid3label'] = $word_list->template->mid3;
              $wordarr['bottom'] = $word_list->bottom;
              $wordarr['bottomlabel'] = $word_list->template->bottom;
              $wordarr['bottom1'] = $word_list->bottom1;
              $wordarr['bottom1label'] = $word_list->template->bottom1;
              $wordarr['bottom2'] = $word_list->bottom2;
              $wordarr['bottom2label'] = $word_list->template->bottom2;
              $wordarr['bottom3'] = $word_list->bottom3;
              $wordarr['bottom3label'] = $word_list->template->bottom3;
             
               
            }
            return $wordarr;

            //return view('word');
          }

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

          public function pdf($id)
              {

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

                 $file =\App\File::all();

               //$ftemplate =\App\Fileview_template::where('template_id',$template->id)->with('template')->first();

               $pdf_arr = \App\Fileview_template::where('id', $id)->where('approval_flag',1)->with('file','template')->get();

                  return view('pdf',compact('pdf_arr','template','file'));

                  
              }

              //get details (pdf format) for template//////////////         

       public function pdftemppost(Request $request)
          {
          $userid = Auth::user()->id;

          $id=$request->pdffileview_template_id;

          $pdf_arr = \App\Fileview_template::where('id', $id)->where('approval_flag',1)->with('file','template')
          ->get();
          //dd($approve_arr);

         

          foreach ($pdf_arr as $pdf_list)
          {
              $pdfarr['top'] = $pdf_list->top;
              $pdfarr['toplabel'] = $pdf_list->template->top;
              $pdfarr['top1'] = $pdf_list->top1;
              $pdfarr['top1label'] = $pdf_list->template->top1;
              $pdfarr['top2'] = $pdf_list->top2;
              $pdfarr['top2label'] = $pdf_list->template->top2;
              $pdfarr['top3'] = $pdf_list->top3;
              $pdfarr['top3label'] = $pdf_list->template->top3;
              $pdfarr['mid'] = $pdf_list->mid;
              $pdfarr['midlabel'] = $pdf_list->template->mid;
              $pdfarr['mid1'] = $pdf_list->mid1;
              $pdfarr['mid1label'] = $pdf_list->template->mid1;
              $pdfarr['mid2'] = $pdf_list->mid2;
              $pdfarr['mid2label'] = $pdf_list->template->mid2;
              $pdfarr['mid3'] = $pdf_list->mid3;
              $pdfarr['mid3label'] = $pdf_list->template->mid3;
              $pdfarr['bottom'] = $pdf_list->bottom;
              $pdfarr['bottomlabel'] = $pdf_list->template->bottom;
              $pdfarr['bottom1'] = $pdf_list->bottom1;
              $pdfarr['bottom1label'] = $pdf_list->template->bottom1;
              $pdfarr['bottom2'] = $pdf_list->bottom2;
              $pdfarr['bottom2label'] = $pdf_list->template->bottom2;
              $pdfarr['bottom3'] = $pdf_list->bottom3;
              $pdfarr['bottom3label'] = $pdf_list->template->bottom3;
             
               
            }
            //return $pdfarr;
            //return view('word');
            //return view('pdf',compact('pdfarr','pdf_arr','id','userid'));
            $view = \View::make('pdf',compact('pdfarr','pdf_arr','id','userid'));
       
            $html = $view->render();       
            $pdf = new PDF();
            $pdf::SetTitle('FFMS::report');
            $pdf::AddPage('P', 'A4');
          
$pdf::writeHTML($html, true, false, true, false, '');
            $pdf::Output(uniqid().'ffms.pdf');
            $pdf::reset();

          }

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

//modaledit temp//////////////////

    public function getfiletemp($id)
       {
            $userid = Auth::user()->id;
           $title_arr1 = \App\Fileview_template::where('id', $id)->with('file','template')->get();
            
           $templatestr ='';
            
            foreach ($title_arr1 as $title_list1)

            {
              $top = $title_list1->top;
              $temptop = $title_list1->template->top;
              $temptoptype = $title_list1->template->top_type;
              $top1 = $title_list1->top1;
              $temptop1 = $title_list1->template->top1;
              $temptop1type = $title_list1->template->top1_type;
              $top2 = $title_list1->top2;
              $temptop2 = $title_list1->template->top2;
              $temptop2type = $title_list1->template->top2_type;
              $top3 = $title_list1->top3;
              $temptop3 = $title_list1->template->top3;
              $temptop3type = $title_list1->template->top3_type;
              $mid = $title_list1->mid;
              $tempmid = $title_list1->template->mid;
              $tempmidtype = $title_list1->template->mid_type;
              $mid1 = $title_list1->mid1;
              $tempmid1 = $title_list1->template->mid1;
              $tempmid1type = $title_list1->template->mid1_type;
              $mid2 = $title_list1->mid2;
              $tempmid2 = $title_list1->template->mid2;
              $tempmid2type = $title_list1->template->mid2_type;
              $mid3 = $title_list1->mid3;
              $tempmid3 = $title_list1->template->mid3;
              $tempmid3type = $title_list1->template->mid3_type;
              $bottom = $title_list1->bottom;
              $tempbottom = $title_list1->template->bottom;
              $tempbottomtype = $title_list1->template->bottom_type;
              $bottom1 = $title_list1->bottom1;
              $tempbottom1 = $title_list1->template->bottom1;
              $tempbottom1type = $title_list1->template->bottom1_type;
              $bottom2 = $title_list1->bottom2;
              $tempbottom2 = $title_list1->template->bottom2;
              $tempbottom2type = $title_list1->template->bottom2_type;
              $bottom3= $title_list1->bottom3;
              $tempbottom3= $title_list1->template->bottom3;
              $tempbottom3type= $title_list1->template->bottom3_type;
             
               
     
     
      if(($top!="") && ($temptop!="")) 
       {    
         if($temptoptype==1)
         {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$temptop</label><div class='col-md-9'><input type='date' id='top' name='top' 
            class='form-control'  value='{$top}''></div>";
         }

         if($temptoptype==2)
         {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$temptop</label><div class='col-md-9'><input type='number' id='top' name='top' 
            class='form-control'  value='{$top}''></div>";
         }

         if($temptoptype==3)
         {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$temptop</label><div class='col-md-9'><input type='text' id='top' name='top' 
            class='form-control'  value='{$top}''></div>";
         }

         if($temptoptype==4)
         {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$temptop</label><div class='col-md-9'><textarea row='3' id='top' name='top' 
            class='form-control'  value='{$top}''></textarea></div>";
         }
        
       //echo ("fsv");
        }

      if(($top1!="") && ($temptop1!="")) 
       {   

         if($temptop1type==1)
         {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label
            class='col-md-3 control-label' for='addr'>$temptop1</label><div class='col-md-9'><input type='date' id='top1' name='top1' 
            class='form-control' value='{$top1}''></div>";
         } 

         if($temptop1type==2)
         {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label
            class='col-md-3 control-label' for='addr'>$temptop1</label><div class='col-md-9'><input type='number' id='top1' name='top1' 
            class='form-control' value='{$top1}''></div>";
         } 

         if($temptop1type==3)
         {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label
            class='col-md-3 control-label' for='addr'>$temptop1</label><div class='col-md-9'><input type='text' id='top1' name='top1' 
            class='form-control' value='{$top1}''></div>";
         }

         if($temptop1type==4)
         {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label
            class='col-md-3 control-label' for='addr'>$temptop1</label><div class='col-md-9'><textarea id='top1' name='top1' 
            class='form-control' value='{$top1}''></textarea></div>";
         }  
        
       // echo ("fsv");
        }

      if(($top2!="") && ($temptop2!="")) 
       {    
          if($temptop2type==1)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label
            class='col-md-3 control-label' for='addr'>$temptop2</label><div class='col-md-9'><input type='date' id='top2' name='top2' 
            class='form-control'  value='{$top2}''></div>";
          }

          if($temptop2type==2)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label
            class='col-md-3 control-label' for='addr'>$temptop2</label><div class='col-md-9'><input type='number' id='top2' 
            name='top2'  class='form-control'  value='{$top2}''></div>";
          }

          if($temptop2type==3)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label
            class='col-md-3 control-label' for='addr'>$temptop2</label><div class='col-md-9'><input type='text' id='top2' name='top2' 
            class='form-control'  value='{$top2}''></div>";
          }

          if($temptop2type==4)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label
            class='col-md-3 control-label' for='addr'>$temptop2</label><div class='col-md-9'><textarea id='top2' name='top2' 
            class='form-control'  value='{$top2}''></textarea></div>";
          }

        
       
        }
  
      if(($top3!="") && ($temptop3!="")) 
       {    
          if($temptop3type==1)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$temptop3</label><div class='col-md-9'><input type='date' id='top3' name='top3' 
            class='form-control'  value='{$top3}''></div>";
          }

          if($temptop3type==2)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$temptop3</label><div class='col-md-9'><input type='number' id='top3' name='top3' 
            class='form-control'  value='{$top3}''></div>";
          }

          if($temptop3type==3)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$temptop3</label><div class='col-md-9'><input type='text' id='top3' name='top3' 
            class='form-control'  value='{$top3}''></div>";
          }

          if($temptop3type==4)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$temptop3</label><div class='col-md-9'><textarea id='top3' name='top3' 
            class='form-control'  value='{$top3}''></textarea></div>";
          }

        
       
        }

      if(($mid!="") && ($tempmid!="")) 
       {    
          if($tempmidtype==1)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid</label><div class='col-md-9'><input type='date' id='mid' name='mid' 
            class='form-control'  value='{$mid}''></div>";
          }

          if($tempmidtype==2)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid</label><div class='col-md-9'><input type='number' id='mid' name='mid' 
            class='form-control'  value='{$mid}''></div>";
          }

          if($tempmidtype==3)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid</label><div class='col-md-9'><input type='text' id='mid' name='mid' 
            class='form-control'  value='{$mid}''></div>";
          }

          if($tempmidtype==4)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid</label><div class='col-md-9'><textarea id='mid' name='mid' 
            class='form-control'>{$mid}</textarea></div>";
          }
        
       
        }

      if(($mid1!="") && ($tempmid1!="")) 
       {    
          if($tempmid1type==1)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid1</label><div class='col-md-9'><input type='date' id='mid1' name='mid1' 
            class='form-control' value='{$mid1}''></div>";
          }

          if($tempmid1type==2)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid1</label><div class='col-md-9'><input type='number' id='mid1' 
            name='mid1' class='form-control' value='{$mid1}''></div>";
          }

          if($tempmid1type==3)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid1</label><div class='col-md-9'><input type='text' id='mid1' 
            name='mid1' class='form-control' value='{$mid1}''></div>";
          }

          if($tempmid1type==4)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid1</label><div class='col-md-9'><textarea id='mid1' 
            name='mid1' class='form-control'>{$mid1}</textarea></div>";
          }
        
       
        } 

      if(($mid2!="") && ($tempmid2!="")) 
       {    
          if($tempmid2type==1)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid2</label><div class='col-md-9'><input type='date' id='mid2' name='mid2' 
            class='form-control' value='{$mid2}''></div>";
          }

          if($tempmid2type==2)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid2</label><div class='col-md-9'><input type='number' id='mid2' 
            name='mid2' class='form-control' value='{$mid2}''></div>";
          }

          if($tempmid2type==3)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid2</label><div class='col-md-9'><input type='text' id='mid2' 
            name='mid2' class='form-control' value='{$mid2}''></div>";
          }

          if($tempmid2type==4)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid2</label><div class='col-md-9'><textarea id='mid2' 
            name='mid2' class='form-control'>{$mid2}</textarea></div>";
          }
        
       
        }

      if(($mid3!="") && ($tempmid3!="")) 
       {    
          if($tempmid3type==1)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid3</label><div class='col-md-9'><input type='date' id='mid3' name='mid3' 
            class='form-control' value='{$mid3}''></div>";
          }

          if($tempmid3type==2)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid3</label><div class='col-md-9'><input type='number' id='mid3' 
            name='mid3' class='form-control' value='{$mid3}''></div>";
          }

          if($tempmid3type==3)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid3</label><div class='col-md-9'><input type='text' id='mid3' 
            name='mid3' class='form-control' value='{$mid3}''></div>";
          }

          if($tempmid3type==4)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempmid3</label><div class='col-md-9'><textarea id='mid3' 
            name='mid3' class='form-control'>{$mid3}</textarea></div>";
          }
        
       
        } 

      if(($bottom!="") && ($tempbottom!="")) 
       {    
          if($tempbottomtype==1)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom</label><div class='col-md-9'><input type='date' id='bottom' name='bottom' 
            class='form-control' value='{$bottom}''></div>";
          }

          if($tempbottomtype==2)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom</label><div class='col-md-9'><input type='number' id='bottom' 
            name='bottom' class='form-control' value='{$bottom}''></div>";
          }

          if($tempbottomtype==3)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom</label><div class='col-md-9'><input type='text' id='bottom' 
            name='bottom' class='form-control' value='{$bottom}''></div>";
          }

          if($tempbottomtype==4)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom</label><div class='col-md-9'><textarea id='bottom' 
            name='bottom' class='form-control'>{$bottom}</textarea></div>";
          }
        
       
        } 

      if(($bottom1!="") && ($tempbottom1!="")) 
       {    
          if($tempbottom1type==1)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom1</label><div class='col-md-9'><input type='date' id='bottom1' 
            name='bottom1' class='form-control' value='{$bottom1}''></div>";
          }

          if($tempbottom1type==2)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom1</label><div class='col-md-9'><input type='number' id='bottom1' 
            name='bottom1' class='form-control' value='{$bottom1}''></div>";
          }

          if($tempbottom1type==3)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom1</label><div class='col-md-9'><input type='text' id='bottom1' 
            name='bottom1' class='form-control' value='{$bottom1}''></div>";
          }

          if($tempbottom1type==4)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom1</label><div class='col-md-9'><textarea id='bottom1' 
            name='bottom1' class='form-control'>{$bottom1}</textarea></div>";
          }
        
       
        }

      if(($bottom2!="") && ($tempbottom2!="")) 
       {    
          if($tempbottom2type==1)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom2</label><div class='col-md-9'><input type='date' id='bottom2' 
            name='bottom2' class='form-control' value='{$bottom2}''></div>";
          }

          if($tempbottom2type==2)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom2</label><div class='col-md-9'><input type='number' id='bottom2'
            name='bottom2' class='form-control' value='{$bottom2}''></div>";
          }

          if($tempbottom2type==3)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom2</label><div class='col-md-9'><input type='text' id='bottom2'
            name='bottom2' class='form-control' value='{$bottom2}''></div>";
          }

          if($tempbottom2type==4)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom2</label><div class='col-md-9'><textarea id='bottom2'
            name='bottom2' class='form-control'>{$bottom2}</textarea></div>";
          }
        
       
        } 

      if(($bottom3!="") && ($tempbottom3!="")) 
       {    
          if($tempbottom3type==1)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom3</label><div class='col-md-9'><input type='date' id='bottom3' 
            name='bottom3' class='form-control' value='{$bottom3}''></div>";
          }

          if($tempbottom3type==2)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom3</label><div class='col-md-9'><input type='number' id='bottom3' 
            name='bottom3' class='form-control' value='{$bottom3}''></div>";
          }

          if($tempbottom3type==3)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom3</label><div class='col-md-9'><input type='text' id='bottom3' 
            name='bottom3' class='form-control' value='{$bottom3}''></div>";
          }

          if($tempbottom3type==4)
          {

            $templatestr .= "<input type='hidden' name='fileview_template_id' id='fileview_template_id' value='$id'><label 
            class='col-md-3 control-label' for='addr'>$tempbottom3</label><div class='col-md-9'><textarea id='bottom3' 
            name='bottom3' class='form-control'>{$bottom3}</textarea></div>";
          }
        
       
        }
           
     }

    return $templatestr;
    
  }



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


//comment
   

   public function file_viewpost(Request $request)
    {

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

      $userid = Auth::user()->id;
      
     
     
        $seatid_new = $request->session()->get('session_seat');
        $seatnameobj=\App\Seat::where('id',$seatid_new)->first();
        $seatname=$seatnameobj->seat_name;
      
        $comment_type=$request->comment_type;
        $description=$request->description;
        $file_id=$request->creation_of_file;
        //$file_id=$request->creation_of_comment;

        //$tapalid=$request->creation_of_tapalcomment;
        //$tapal_id=$tapalid;
        //$hidden=$request->commentval;
        //dd($file_id);
        if(strcmp($comment_type, 'Public') == 0) {
          $comment_type=0;
        }
        else if(strcmp($comment_type, 'Private') == 0) {
          $comment_type=1;
        }
        else if(strcmp($comment_type, 'Yellow Notes') == 0) {
          $comment_type=2;
        }



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

        $checkforseatmatch=\App\User_Seat::where('user_id',$id)->where('seat_id',$seatid_new)->count();
         $currentfileseat=\App\File_forward::where('file_id',$file_id)->where('active_flag',1)->first();
            $seat=$currentfileseat->to_seat;
          $filearr =[];
            $seatflag=0;
            if($seatid_new==$seat)
              {
                 $seatflag=1;
              }
              $commentstr ='';
    if($insert_id)
    {
        $sendmessage=1;
        
     $comments_list=\App\Comments::where('file_id',$file_id)->where('seat_id',$seatid_new)->orderBy('id','desc')->first();
     

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

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

            }

            if(($comment_type==2)  && ($comments_list->disgard_flag==0))
            {
              //<img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'>
              $commentstr .= "<div class='box-comment'><form class=form-horizontal action='".url('filepostit_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'><div class='comment-text'><span class='username'>$seatname posted Yellow Note<span class='text-muted pull-right'>$created_at</span></span><font color='#ad890a'>$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> "; 
              }

            }
      }
      else
      {
        $sendmessage=0;
      }
      
    $commentarr['comment']=$commentstr;
    //return $sendmessage;
     return $commentarr;
     // return redirect()->back();

    }

      public function filecapture_viewpost(Request $request)
    {

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

      $userid = Auth::user()->id;
      
     
     
        $seatid_new = $request->session()->get('session_seat');
        $seatnameobj=\App\Seat::where('id',$seatid_new)->first();
        $seatname=$seatnameobj->seat_name;
      
        $comment_type=$request->comment_type;
        $description=$request->description;
        $file_id=$request->creation_of_file;
        //$file_id=$request->creation_of_comment;

        //$tapalid=$request->creation_of_tapalcomment;
        //$tapal_id=$tapalid;
        //$hidden=$request->commentval;
        //dd($file_id);
        if(strcmp($comment_type, 'Public') == 0) {
          $comment_type=0;
        }
        else if(strcmp($comment_type, 'Private') == 0) {
          $comment_type=1;
        }
        else if(strcmp($comment_type, 'Yellow Notes') == 0) {
          $comment_type=2;
        }



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

        $checkforseatmatch=\App\User_Seat::where('user_id',$id)->where('seat_id',$seatid_new)->count();
         $currentfileseat=\App\File_forward::where('file_id',$file_id)->where('active_flag',1)->first();
            $seat=$currentfileseat->to_seat;
          $filearr =[];
            $seatflag=0;
            if($seatid_new==$seat)
              {
                 $seatflag=1;
              }
              $commentstr ='';
    if($insert_id)
    {
        $sendmessage=1;
        
     $comments_list=\App\Comments::where('file_id',$file_id)->where('seat_id',$seatid_new)->orderBy('id','desc')->first();
     

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

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

            }

            if(($comment_type==2)  && ($comments_list->disgard_flag==0))
            {
              // /<img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'>
              $commentstr .= "<div class='box-comment'><form class=form-horizontal action='".url('filepostit_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'><div class='comment-text'><span class='username'>$seatname<span class='text-muted pull-right'>$created_at</span></span><font color='#ad890a'>$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> "; 
              }

            }
      }
      else
      {
        $sendmessage=0;
      }
      
    $commentarr['comment']=$commentstr;
    //return $sendmessage;
     //return $commentarr;
     return redirect()->back();

    }

    public function file_viewcommentpost(Request $request)

    {

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


      $userid = Auth::user()->id;
      
     
     
        $seatid_new = $request->session()->get('session_seat');
      
        $comment_type=$request->comment_type;
        $description=$request->description1;
        //$tapal_id=$request->creation_of_comment;
        $file_id=$request->creation_of_filecomment;
        //$tapal_id=$tapalid;
        //$hidden=$request->commentval;
        //dd($tapalid);
        



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


    }


//tapal_actions

  public function file_actionpost(Request $request)

    {
      $office_id=$request->session()->get('session_office');
     $this->validate($request,[
             
            'action_type' => 'required',
            'actdescription' => 'required'
            
            
       ]);


      $userid = Auth::user()->id;
      
        $seatid_new = $request->session()->get('session_seat');
        $seatnameobj=\App\Seat::where('id',$seatid_new)->first();
        $seatname=$seatnameobj->seat_name;
      
        $action_type=$request->action_type;
        //dd($actions);
        $action_description=$request->actdescription;
        $file_id=$request->creation_of_action;
        //$tapalid=$request->creation_of_tapalaction;
        //$tapal_id=$tapalid;
        
        /*if(strcmp($actions, 'Clarification required') == 0) {
          $actions=1;
          $action_name = "Clarification Required";
        }
        else if(strcmp($actions, 'Non Pertaining') == 0) {
          $actions=2;
          $action_name = "Non Pertaining";
        }
        else if(strcmp($actions, 'Close') == 0) {
          $actions=3;
          $action_name = "Close";
        }
        else if(strcmp($actions, 'Testing') == 0) {
          $actions=4;
          $action_name = "Testing";
        }*/

       // $actions = "";
        $actions_list = \App\Action::where('office_id',$office_id)->where('actions',$action_type)->first();
        if(isset($actions_list))
        {
          $actions = $actions_list->id;
        }

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

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

          'action_type' => $actions,
          'action_description' => $action_description,
          'file_id' => $file_id,
          'seat_id' => $seatid_new,
          
          'created_by'=>$id
           ]);
        if($action_type=="Close")
        {
            $closeddate = date("Y-m-d");
            $updateobj = \App\File::where('id',$file_id)->first();
            $updateobj->status =1;
            $updateobj->closed_by = $id;
            $updateobj->closed_date = $closeddate;
            $updateobj->save();
        }
        $created_at=$insert_id->created_at->format('d-M-Y');

         
      $actionstr ='';
      if($insert_id)
      {

        $sendmessage=1;
        if($actions) 
          {    
            //<img class='img-circle img-sm' src='./public/img/user3-128x128.jpg' alt='User Image'>
            $actionstr .= "<div class='box-comment'><div class='comment-text'><span class='username'>$seatname put action on $action_type<span class='text-muted pull-right'>$created_at</span></span>$action_description</div></div>";
            }
      }

      else{
        $sendmessage=0;
      }
      
      $actionarr['action']=$actionstr;
      $actionarr['action_type']=$action_type;
    //return $sendmessage;

     return $actionarr;

    }

    public function filecapture_actionpost(Request $request)

    {

     $this->validate($request,[
             
            'action_type' => 'required',
            'actdescription' => 'required'
            
            
       ]);


      $userid = Auth::user()->id;
      
        $seatid_new = $request->session()->get('session_seat');
        $seatnameobj=\App\Seat::where('id',$seatid_new)->first();
        $seatname=$seatnameobj->seat_name;
      
        $actions=$request->action_type;
        $action_description=$request->actdescription;
        $file_id=$request->creation_of_action;
        //$tapalid=$request->creation_of_tapalaction;
        //$tapal_id=$tapalid;
        
        if(strcmp($actions, 'Clarification required') == 0) {
          $actions=1;
        }
        else if(strcmp($actions, 'Non Pertaining') == 0) {
          $actions=2;
        }
        else if(strcmp($actions, 'Close') == 0) {
          $actions=3;
        }
        else if(strcmp($actions, 'Testing') == 0) {
          $actions=4;
        }



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

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

          'action_type' => $actions,
          'action_description' => $action_description,
          'file_id' => $file_id,
          'seat_id' => $seatid_new,
          
          'created_by'=>$id
           ]);
        $created_at=$insert_id->created_at->format('d-M-Y');

         
      $actionstr ='';
      if($insert_id)
      {

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

      else{
        $sendmessage=0;
      }
      
      $actionarr['action']=$actionstr;
    //return $sendmessage;
    // return $actionarr;
       return redirect()->back();
    }

    

    public function file_action(Request $request)

    {

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


      $userid = Auth::user()->id;
      
        $seatid_new = $request->session()->get('session_seat');
      
        $actions=$request->actions;
        $action_description=$request->actdescription1;
        //$tapal_id=$request->creation_of_action;
        $file_id=$request->creation_of_fileaction;
        //$tapal_id=$tapalid;
        
       

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

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

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


         

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

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


    }
///////////////////////////////////
/////attachment insertion in tapal view

    public function file_attachmentpost(Request $request)
    {
        $id = Auth::user()->id;   
        //return $request;  
      
            $seatid_new = $request->session()->get('session_seat');
        $document_id="";
        $attachmentstr="";
        $uploadcount=$request->uploadcount1;
        $file_id=$request->creation_of_attachment;
        $filename="upload";
        $filedescrem="filedesc";
        $filedesc=$request->$filedescrem;
        if ($request->hasFile($filename)) 
        {
            $fileobj=$request->file($filename)->getClientOriginalName();
            $destpath="uploads";
            $custom_file_name = time().'-'.$id.'-'.$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'=>$id
             ]);
        }
        if($document_id)
        {

          $sendmessage=1;
          if($path) 
            {    

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

              $attachmentstr = "<li><div class='mailbox-attachment-info'><a href='#' class='mailbox-attachment-name'><i class='fa fa-paperclip'></i> $filedesc</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>";
            }
        }
        else
          $sendmessage=0;
      
         $attacharr['attachment']=$attachmentstr;
    //return $sendmessage;
     return $attacharr;
    }

    public function filecapture_attachmentpost(Request $request)
    {
        $id = Auth::user()->id;   
        //return $request;  
      
        $seatid_new = $request->session()->get('session_seat');
        $document_id="";
        $attachmentstr="";
        $uploadcount=$request->uploadcount1;
        $file_id=$request->creation_of_attachment;
        $filename="upload";
        $filedescrem="filedesc";
        $filedesc=$request->$filedescrem;
        if ($request->hasFile($filename)) 
        {
            $fileobj=$request->file($filename)->getClientOriginalName();
            $destpath="uploads";
            $custom_file_name = time().'-'.$id.'-'.$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'=>$id
             ]);
        }
        if($document_id)
        {

          $sendmessage=1;
          if($path) 
            {    

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

              $attachmentstr = "<li><div class='mailbox-attachment-info'><a href='#' class='mailbox-attachment-name'><i class='fa fa-paperclip'></i> $filedesc</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>";
            }
        }
        else
          $sendmessage=0;
      
         $attacharr['attachment']=$attachmentstr;
    //return $sendmessage;
     //return $attacharr;
      return redirect()->back();

    }


  //////////////////////////
    public function file_attachmentpostdata(Request $request)
    {
        $userid = Auth::user()->id;     
      
            $seatid_new = $request->session()->get('session_seat');
        
        $uploadcount=$request->uploadcount;
        $file_id=$request->creation_of_fileattachment;
        $filename="upload1";
        $filedescrem="filedesc1";
        $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
          smilify('Error', 'Error Occurred Pls recheck!'); 
          //$sendmessage="Error Occurred Pls recheck!";
      
        return redirect()->back();
    }











    //file view template/////////////////////

    public function fileview_templatepost(Request $request)
    {

     
        $this->validate($request,[
             
            'title' => 'required'
          
       ]);
        $template_id=$request->title;
        $file_id=$request->filetemplate;
        $top=$request->top;
        $top1=$request->top1;
        $top2=$request->top2;
        $top3=$request->top3;
        $mid=$request->mid;
        $mid1=$request->mid1;
        $mid2=$request->mid2;
        $mid3=$request->mid3;
        $bottom=$request->bottom;
        $bottom1=$request->bottom1;
        $bottom2=$request->bottom2;
        $bottom3=$request->bottom3;



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

        $insert_id = \App\Fileview_template::create([//model name database field name
          'template_id' => $template_id,
          'file_id' => $file_id,
          'top' => $top,
          'top1' => $top1,
          'top2' => $top2,
          'top3' => $top3,
          'mid' => $mid,
          'mid1' => $mid1,
          'mid2' => $mid2,
          'mid3' => $mid3,
          'bottom' => $bottom,
          'bottom1' => $bottom1,
          'bottom2' => $bottom2,
          'bottom3' => $bottom3,
          'updated_by'=>$id
           ]);


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

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

    }


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

//modal template///////////////

     public function edit_templatepost(Request $request)
        {

     
        $id=$request->fileview_template_id;
        $template_id=\App\Fileview_template::where('id',$id)->first();
        $file_id=$request->creation_of_file;
        $top=$request->top;
        $top1=$request->top1;
        $top2=$request->top2;
        $top3=$request->top3;
        $mid=$request->mid;
        $mid1=$request->mid1;
        $mid2=$request->mid2;
        $mid3=$request->mid3;
        $bottom=$request->bottom;
        $bottom1=$request->bottom1;
        $bottom2=$request->bottom2;
        $bottom3=$request->bottom3;


         $modaltemp = \App\Fileview_template::where('id',$id)->where('cancel_flag',0)->update(['cancel_flag' => 1]);


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

        
        $insert_id = \App\Fileview_template::create([//model name database field name
          'template_id' => $template_id->template_id,
          'file_id' => $file_id,
          'top' => $top,
          'top1' => $top1,
          'top2' => $top2,
          'top3' => $top3,
          'mid' => $mid,
          'mid1' => $mid1,
          'mid2' => $mid2,
          'mid3' => $mid3,
          'bottom' => $bottom,
          'bottom1' => $bottom1,
          'bottom2' => $bottom2,
          'bottom3' => $bottom3,
          'cancel_flag' => 0,
          'approval_flag' => 0,
          'updated_by'=>$id
           ]);


      if($insert_id)
       // $sendmessage="FileView Template Edited!";
        notify()->success('FileView Template Edited!');

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

    }


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


//modal approve template/////////////
   
      public function approve_templatepost(Request $request)

        {

            $id=$request->fileview_templateapprove_id;
            //dd($id);
            $template_id=\App\Fileview_template::where('id',$id)->first();

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

            

            $modalapprovetemp = \App\Fileview_template::where('id',$id)->where('approval_flag',0)->update(['approval_flag' => 1, 'approved_by' => $userid]);
            //->update(['approved_by' => $userid]);

            //$retstr="file_view".$template_id->fileid;

            //return view($retstr);

            //return redirect('file_view');
            $fileid=$template_id->file_id;


            return redirect()->route('fileinbox');


        }



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

    //details from template///////////////////

        public function getdetails($id)
          {
            $userid = Auth::user()->id;
            $templatedetails=\App\Template::where('id',$id)->first();

            return $templatedetails;
           
            }

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


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

   public function capturefile(Request $request)
     {
      $office_id=$request->session()->get('session_office');
      $id = Auth::user()->id;

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

      $allseats=\App\Seat::where('office_id',$office_id)->get();

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

      $charray=$this->getchildseats($seatid);
     // print_r($charray);
      
      return view('capturefile', compact('charray','allseats'));

     }

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

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

      public function getfilelist($id,Request $request)
          {
            $office_id=$request->session()->get('session_office');   
            //dd($office_id);
            $filedetails=\App\File_forward::where('to_seat',$id)->where('active_flag',1)->with('file')->whereHas('file', function($q) use ($office_id) { $q->where('confidential', NULL)->where('office_id',$office_id); })->get();
             $data  = [];
            $i=0;

            foreach ($filedetails as $file)
            {   
                $fileid=$file->file_id;
                $filenum=$file->file->file_no;//db name
                $filetitle=$file->file->title;
                $filegrp=\App\File::where('id',$fileid)->with('file_groupobj')->first();
               // dd( $filegrp);
                $file_group=$filegrp->file_groupobj->file_group;
                $date=$file->created_at->format('d-M-Y');
             
                $updated_at=$file->updated_at->format('d-M-Y');
       
               

                $obj = new \stdClass;
                $obj->incid = $i + 1;
                $obj->id = $fileid;
                $obj->file_no = $filenum;
                $obj->title = $filetitle;
                $obj->file_group = $file_group;
                $obj->created_at = $date;          
                $obj->updated_at = $updated_at;

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

            }

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


  
      public function capture(Request $request)

        {

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

            $fileseat = \App\File_forward::where('file_id',$id)->where('active_flag',1)->first();
            //dd($fileseat);
            $filetoseat=$fileseat->to_seat;

            $update_arr = \App\File_forward::where('file_id',$id)->where('to_seat',$filetoseat)->where('active_flag',1)->orderBy('id','desc')->first();
              //dd($update_arr);
            if(isset($update_arr))
            {
              $update_arr->active_flag= 0;
              $update_arr->save();
            }

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

                          'file_id' => $id,
                          'to_seat' => $seatid,
                          'from_seat' => $filetoseat,
                          'active_flag' => 1,
                          'read_flag' => 0,
                          'capture_flag' => 1,
                          'updated_by'=>$userid
                          ]);
        
                return redirect()->route('fileinbox');
        }
////////////////////


  public function file_forwardpost(Request $request)

    {

       
         $fileid=$request->creation_of_file_id;
        //$user=$request->user_seat->user_id;

        $name = Auth::user()->name;
        $fileroute = $request->fileroute;
        $office = $request->officename;
        $department=$request->deptname;
        $section=$request->secname;
        $toseat=$request->seatname;
        $check=$request->key;

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

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

        //$username=$seat->name;

      

         // $tapal_forward=\App\Tapal_forward::where('tapal_id',$tapalid)->where('to_seat', $seatid)->where('active_flag',1)->get()
  

          /*if($section)
            $seats = \App\Seat::where('section_id', $section)->get();
          else if($department)
            $seats = \App\Seat::where('department_id', $department)->get();
          else
            $seats = \App\Seat::where('office_id', $office)->get();
          */
          /*$seats = [];
          $trouts=\App\Trouteconf::where('troute_id',$fileroute)->where('seat_id','!=',$seatid)->with('seat')->get();
          $i=0;
          foreach($trouts as $trout)
          {
             $seat_id=$trout->seat_id;
             $seats[] = $seat_id;
          }*/
          //dd($seats);
          $seats = \App\Seat::where('office_id', $office_id)->get();
          //dd($seats);
          $seatcount=0;
           $check_flag=0;
          //to check the total seats selected
          foreach($seats as $seat)
          {
            $toseat=$seat->id;
            //echo "toseat=$toseat...<br>";
            $seatobj=$request->$toseat;
            //echo "seatobj=$seatobj...<br>";
            if($seatobj==1)
            {
              $check_flag = 1;
                $selectedseat=$toseat;
                 $seatcount++;
             }

          }
          //echo "check_flag=$check_flag..<br>";
          //dd($seatcount);
           if($check_flag==0)
          {
              //$sendmessage="Please select atleast one seat to forward !!!";
               smilify('Error', 'Please select atleast one seat to forward !!!'); 

              return redirect()->back();
          }
          if($check_flag==1)
          {
              //update the current active flag of current user and this tapalid to 0
        
              $file_forward = \App\File_forward::where('to_seat', $seatid)
              ->where('file_id', $fileid)->where('active_flag',1)
              ->update(['active_flag' => 0]);

              //$file_forward1 = \App\File_forward::where('to_seat', $seatid)->where('file_id', $fileid)->where('read_flag',1)->update(['read_flag' => 0]);

          //seatcount has the total selected seats

            if($seatcount==1)
            {
            //single seat forward
            $insert_id = \App\File_forward::create([//model name database field name

            'file_id' => $request->creation_of_file_id,
            'to_seat' => $selectedseat,
            'from_seat' => $seatid,
            'active_flag' => 1,
            'read_flag' => 0,
            'updated_by'=>$id
            ]);


            }
          elseif($seatcount>1)
          {
            //multiple seat forward..duplicate file must be created
                $i=0;
                 foreach($seats as $seat)
                {
                  $toseat=$seat->id;
                  $seatobj=$request->$toseat;
                  $title=$request->title;

                  
                  if($seatobj==1)
                  {
                      $selectedseat=$toseat;

                      //duplicate this file
                       $filedetails = \App\File::where('id',$fileid)->first();
                       $i++;
                       $newfileno=$filedetails->file_no."(".$i.")";

                    $dupfile = \App\File::create([//model name database field name
                    'file_no' => $newfileno,
                    'initiated_file_id' => $fileid,
                    'title' => $filedetails->title,
                    'file_description' => $filedetails->file_description,
                    'reference_number' => $filedetails->reference_number,
                    'priority' => $filedetails->priority,
                    'file_group_id' => $filedetails->file_group_id,
                    'created_by' =>$id,
                    'updated_by'=>$id
                     ]);

                    //get comments of orginal file
                        $comments=\App\Comments::where('file_id',$fileid)->get();
                        foreach($comments as $comment)
                        {
                          $orginalcommentseatid=$comment->seat_id;
                          $orginalcomment_type=$comment->comment_type;
                          $orginalcommentdescription=$comment->comment_description;
                          $orginalcommentcreated_at=$comment->created_at;
                          $orginalcommentupdated_at=$comment->updated_at;
                          $orginalcommentupdated_by=$comment->updated_by;
                          $commentfrwd = \App\Comments::create([
                          'file_id' => $dupfile->id,
                          'seat_id' =>  $orginalcommentseatid,
                          'comment_type' => $orginalcomment_type,
                          'comment_description' => $orginalcommentdescription,
                          'created_at'  => $orginalcommentcreated_at,
                          'updated_at'  => $orginalcommentupdated_at,
                          'updated_by'=>$orginalcommentupdated_by
                          ]);
                        }

                        //get actions of orginal file
                        $actions=\App\File_action::where('file_id',$fileid)->get();
                        foreach($actions as $action)
                        {
                            $orginalactionseatid=$action->seat_id;
                            $orginalactiontype=$action->action_type;
                            $orginalactiondescription=$action->action_description;
                            $orginalactioncreated_at=$action->created_at;
                            $orginalactionupdated_at=$action->updated_at;
                            $orginalactionupdated_by=$action->created_by;
                            $actionfrwd = \App\File_action::create([
                            'file_id' => $dupfile->id,
                            'seat_id' =>  $orginalactionseatid,
                            'action_type' => $orginalactiontype,
                            'action_description' => $orginalactiondescription,
                            'created_at'  => $orginalactioncreated_at,
                            'updated_at'  => $orginalactionupdated_at,
                            'created_by'=>$orginalactionupdated_by
                            ]);
                        }                       
                        //get attachments of orginal file
                        $attachments=\App\Documents::where('file_id',$fileid)->get();
                        foreach($attachments as $attachment)
                        {

                            $orginalattachmentseatid=$attachment->seat_id;
                            $orginalattachmentdocumentpath=$attachment->document_path;
                            $orginalattachmentdocumentname=$attachment->document_name;
                            $orginalattachmentcreated_at=$attachment->created_at;
                            $orginalattachmentupdated_at=$attachment->updated_at;
                            $orginalattachmentupdated_by=$attachment->updated_by;
                            $attachmentfrwd = \App\Documents::create([
                            'file_id' => $dupfile->id,
                            'seat_id' =>  $orginalattachmentseatid,
                            'document_path' => $orginalattachmentdocumentpath,
                            'document_name' => $orginalattachmentdocumentname,
                            'created_at'  => $orginalattachmentcreated_at,
                            'updated_at'  => $orginalattachmentupdated_at,
                            'updated_by'=>$orginalattachmentupdated_by
                            ]);
                         }

                      //forward duplicated file 

                    $firstforwardinsert_id = \App\File_forward::create([//model name database field name

                          'file_id' => $dupfile->id,
                          'to_seat' => $seatid,
                          'from_seat' => 0,
                          'active_flag' => 0,
                          'read_flag' => 1,
                          'updated_by'=>$id
                          ]);

                    $insert_id = \App\File_forward::create([//model name database field name
                    'file_id' => $dupfile->id,
                    'to_seat' => $toseat,
                    'from_seat' => $seatid,
                    'active_flag' => 1,
                    'read_flag' => 0,
                    'updated_by'=>$id
                     ]);
                      
                    }  /////end of if(seatobj)

                } //endforeach

                //change the status of initiated file to 2

                $status = \App\File::where('id', $fileid)->update(['status' => 2]);

          }
        }

      if($insert_id)
          {
              //$sendmessage="Forwarded Successfully!";
               notify()->success('Forwarded Successfully!');

              return redirect('fileinbox');
          }

          else
          {
             // $sendmessage="Error Occurred Pls recheck!";
               smilify('Error', 'Error Occurred Pls recheck!'); 

              return redirect()->back();
          }

}

public function filenewforwardpost(Request $request)

    {

       $this->validate($request,[
             
            'officename' => 'required'
            
            
       ]);
         $fileid=$request->creation_of_file_id;
        //$user=$request->user_seat->user_id;

        $name = Auth::user()->name;
        $office = $request->officename;
        $department=$request->deptname;
        $section=$request->secname;
        $toseat=$request->seatname;
        $check=$request->key;

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

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

        //$username=$seat->name;

      

         // $tapal_forward=\App\Tapal_forward::where('tapal_id',$tapalid)->where('to_seat', $seatid)->where('active_flag',1)->get()
  

          if($section)
            $seats = \App\Seat::where('section_id', $section)->get();
          else if($department)
            $seats = \App\Seat::where('department_id', $department)->get();
          else
            $seats = \App\Seat::where('office_id', $office)->get();
          $seatcount=0;
           $check_flag=0;
          //to check the total seats selected
          foreach($seats as $seat)
          {
            $toseat=$seat->id;
            $seatobj=$request->$toseat;
            if($seatobj==1)
            {
              $check_flag = 1;
                $selectedseat=$toseat;
                 $seatcount++;
             }

          }
           if($check_flag==0)
          {
              //$sendmessage="Please select atleast one seat to forward !!!";
               smilify('Error', 'Please select atleast one seat to forward !!!'); 

              return redirect()->back();
          }
          if($check_flag==1)
          {
              //update the current active flag of current user and this tapalid to 0
        
              $file_forward = \App\File_forward::where('to_seat', $seatid)
              ->where('file_id', $fileid)->where('active_flag',1)
              ->update(['active_flag' => 0]);

              $file_forward1 = \App\File_forward::where('to_seat', $seatid)
              ->where('file_id', $fileid)->where('read_flag',1)
              ->update(['read_flag' => 0]);

          //seatcount has the total selected seats

            if($seatcount==1)
            {
            //single seat forward
            $insert_id = \App\File_forward::create([//model name database field name

            'file_id' => $request->creation_of_file_id,
            'to_seat' => $selectedseat,
            'from_seat' => $seatid,
            'active_flag' => 1,
            'read_flag' => 0,
            'updated_by'=>$id
            ]);


            }
          elseif($seatcount>1)
          {
            //multiple seat forward..duplicate file must be created
                $i=0;
                 foreach($seats as $seat)
                {
                  $toseat=$seat->id;
                  $seatobj=$request->$toseat;
                  $title=$request->title;

                  
                  if($seatobj==1)
                  {
                      $selectedseat=$toseat;

                      //duplicate this file
                       $filedetails = \App\File::where('id',$fileid)->first();
                       $i++;
                       $newfileno=$filedetails->file_no."(".$i.")";

                    $dupfile = \App\File::create([//model name database field name
                    'file_no' => $newfileno,
                    'initiated_file_id' => $fileid,
                    'title' => $filedetails->title,
                    'file_description' => $filedetails->file_description,
                    'reference_number' => $filedetails->reference_number,
                    'priority' => $filedetails->priority,
                    'file_group_id' => $filedetails->file_group_id,
                    'created_by' =>$id,
                    'updated_by'=>$id
                     ]);

                    //get comments of orginal file
                        $comments=\App\Comments::where('file_id',$fileid)->get();
                        foreach($comments as $comment)
                        {
                          $orginalcommentseatid=$comment->seat_id;
                          $orginalcomment_type=$comment->comment_type;
                          $orginalcommentdescription=$comment->comment_description;
                          $orginalcommentcreated_at=$comment->created_at;
                          $orginalcommentupdated_at=$comment->updated_at;
                          $orginalcommentupdated_by=$comment->updated_by;
                          $commentfrwd = \App\Comments::create([
                          'file_id' => $dupfile->id,
                          'seat_id' =>  $orginalcommentseatid,
                          'comment_type' => $orginalcomment_type,
                          'comment_description' => $orginalcommentdescription,
                          'created_at'  => $orginalcommentcreated_at,
                          'updated_at'  => $orginalcommentupdated_at,
                          'updated_by'=>$orginalcommentupdated_by
                          ]);
                        }

                        //get actions of orginal file
                        $actions=\App\File_action::where('file_id',$fileid)->get();
                        foreach($actions as $action)
                        {
                            $orginalactionseatid=$action->seat_id;
                            $orginalactiontype=$action->action_type;
                            $orginalactiondescription=$action->action_description;
                            $orginalactioncreated_at=$action->created_at;
                            $orginalactionupdated_at=$action->updated_at;
                            $orginalactionupdated_by=$action->created_by;
                            $actionfrwd = \App\File_action::create([
                            'file_id' => $dupfile->id,
                            'seat_id' =>  $orginalactionseatid,
                            'action_type' => $orginalactiontype,
                            'action_description' => $orginalactiondescription,
                            'created_at'  => $orginalactioncreated_at,
                            'updated_at'  => $orginalactionupdated_at,
                            'created_by'=>$orginalactionupdated_by
                            ]);
                        }                       
                        //get attachments of orginal file
                        $attachments=\App\Documents::where('file_id',$fileid)->get();
                        foreach($attachments as $attachment)
                        {

                            $orginalattachmentseatid=$attachment->seat_id;
                            $orginalattachmentdocumentpath=$attachment->document_path;
                            $orginalattachmentdocumentname=$attachment->document_name;
                            $orginalattachmentcreated_at=$attachment->created_at;
                            $orginalattachmentupdated_at=$attachment->updated_at;
                            $orginalattachmentupdated_by=$attachment->updated_by;
                            $attachmentfrwd = \App\Documents::create([
                            'file_id' => $dupfile->id,
                            'seat_id' =>  $orginalattachmentseatid,
                            'document_path' => $orginalattachmentdocumentpath,
                            'document_name' => $orginalattachmentdocumentname,
                            'created_at'  => $orginalattachmentcreated_at,
                            'updated_at'  => $orginalattachmentupdated_at,
                            'updated_by'=>$orginalattachmentupdated_by
                            ]);
                         }

                      //forward duplicated file 

                    $firstforwardinsert_id = \App\File_forward::create([//model name database field name

                          'file_id' => $dupfile->id,
                          'to_seat' => $seatid,
                          'from_seat' => 0,
                          'active_flag' => 0,
                          'read_flag' => 1,
                          'updated_by'=>$id
                          ]);

                    $insert_id = \App\File_forward::create([//model name database field name
                    'file_id' => $dupfile->id,
                    'to_seat' => $toseat,
                    'from_seat' => $seatid,
                    'active_flag' => 1,
                    'read_flag' => 0,
                    'updated_by'=>$id
                     ]);
                      
                    }  /////end of if(seatobj)

                } //endforeach

                //change the status of initiated file to 2

                $status = \App\File::where('id', $fileid)->update(['status' => 2]);

          }
        }

      if($insert_id)
          {
             // $sendmessage="Forwarded Successfully!";
              notify()->success('Forwarded Successfully!');
              return redirect('fileinbox');
          }

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

}
   
}
