<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App;
use Yajra\Datatables\Datatables;
class SubjectController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    //template//////////////////
    public function template()
    {  
        return view('template');   
    }
    public function templatepost(Request $request)
    {
      $office_id=$request->session()->get('session_office');     
      $this->validate($request,[
            'title' => 'required',//name of blade name 
       ]);
      $title=$request->title;
      $top=$request->top;
      $toptype=$request->top_type;
      $top1=$request->top1;
      $top1type=$request->top1_type;
      $top2=$request->top2;
      $top2type=$request->top2_type;
      $top3=$request->top3;
      $top3type=$request->top3_type;
      $mid=$request->mid;
      $midtype=$request->mid_type;
      $mid1=$request->mid1;
      $mid1type=$request->mid1_type;
      $mid2=$request->mid2;
      $mid2type=$request->mid2_type;
      $mid3=$request->mid3;
      $mid3type=$request->mid3_type;
      $bottom=$request->bottom;
      $bottomtype=$request->bottom_type;
      $bottom1=$request->bottom1;
      $bottom1type=$request->bottom1_type;
      $bottom2=$request->bottom2;
      $bottom2type=$request->bottom2_type;
      $bottom3=$request->bottom3;
      $bottom3type=$request->bottom3_type;
      $id = Auth::user()->id;
      $insert_id = \App\Template::create([//model name database field name
        'title' => $title,
        'top' => $top,
        'top_type' => $toptype,
        'top1' => $top1,
        'top1_type' => $top1type,
        'top2' => $top2,
        'top2_type' => $top2type,
        'top3' => $top3,
        'top3_type' => $top3type,
        'mid' => $mid,
        'mid_type' => $midtype,
        'mid1' => $mid1,
        'mid1_type' => $mid1type,
        'mid2' => $mid2,
        'mid2_type' => $mid2type,
        'mid3' => $mid3,
        'mid3_type' => $mid3type,
        'bottom' => $bottom,
        'bottom_type' => $bottomtype,
        'bottom1' => $bottom1,
        'bottom1_type' => $bottom1type,
        'bottom2' => $bottom2,
        'bottom2_type' => $bottom2type,
        'bottom3' => $bottom3,
        'bottom3_type' => $bottom3type,
        'updated_by'=>$id,
        'office_id' => $office_id,
         ]);
      if($insert_id)
        notify()->success('Template Added!');
      else
        smilify('Error', 'Error Occurred Pls recheck!'); 
      return redirect()->back();
    }
    public function templatepostdata(Request $request)
    {     
      $office_id=$request->session()->get('session_office');     
      $templates =\App\Template::where('office_id',$office_id)->get();
      $data  = [];
      $i=0;
      foreach ($templates as $template)
      {
          $id=$template->id;//db name
          $titlee=$template->title;
          $topp=$template->top;
          $topptype=$template->top_type;
          $topp1=$template->top1;
          $topp1type=$template->top1_type;
          $topp2=$template->top2;
          $topp2type=$template->top2_type;
          $topp3=$template->top3;
          $topp3type=$template->top3_type;
          $midd=$template->mid;
          $middtype=$template->mid_type;
          $midd1=$template->mid1;
          $midd1type=$template->mid1_type;
          $midd2=$template->mid2;
          $midd2type=$template->mid2_type;
          $midd3=$template->mid3;
          $midd3type=$template->mid3_type;
          $bottomm=$template->bottom;
          $bottommtype=$template->bottom_type;
          $bottomm1=$template->bottom1;
          $bottomm1type=$template->bottom1_type;
          $bottomm2=$template->bottom2;
          $bottomm2type=$template->bottom2_type;
          $bottomm3=$template->bottom3;
          $bottomm3type=$template->bottom3_type;
          $updated_at=$template->updated_at->format('d-M-Y');
          $obj = new \stdClass;
          $obj->incid = $i + 1;
          $obj->id = $id;
          $obj->title = $titlee;
          $obj->top = $topp;
          $obj->top_type = $topptype;
          $obj->top1 = $topp1;
          $obj->top1_type = $topp1type;
          $obj->top2 = $topp2;
          $obj->top2_type = $topp2type;
          $obj->top3 = $topp3;
          $obj->top3_type = $topp3type;
          $obj->mid = $midd;
          $obj->mid_type = $middtype;
          $obj->mid1 = $midd1;
          $obj->mid1_type = $midd1type;
          $obj->mid2 = $midd2;
          $obj->mid2_type = $midd2type;
          $obj->mid3 = $midd3;
          $obj->mid3_type = $midd3type;
          $obj->bottom = $bottomm;
          $obj->bottom_type = $bottommtype;
          $obj->bottom1 = $bottomm1;
          $obj->bottom1_type = $bottomm1type;
          $obj->bottom2 = $bottomm2;
          $obj->bottom2_type = $bottomm2type;
          $obj->bottom3 = $bottomm3;
          $obj->bottom3_type = $bottomm3type;
          $obj->updated_at = $updated_at;
          $data[$i] = $obj;
          $i++;
      }
      $templatepostdatas = collect($data);
      return Datatables::of($templatepostdatas)
            ->addColumn('action', function ($templatedata) {
                return '<a href="' . url('showtemplatedata') . '/' . $templatedata->id . '" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> Show</a>';
            })
            ->editColumn('id', 'ID: {{$incid}}')
            ->make(true);
    }
    public function showtemplatedata($id)//show button
    {
      $template=\App\Template::where('id',$id)->first();
      return view('showtemplatedata', compact('template'));
    }
    public function updatetemplatedata(Request $request)//update button
    {
      //validation of fields
      $this->validate($request,[
        //form field(blade name)
            'title' => 'required',
       ]);
       //variable name         //blade name
      $titl=$request->title;
      $top=$request->top;
      $toptype=$request->top_type;
      $top1=$request->top1;
      $top1type=$request->top1_type;
      $top2=$request->top2;
      $top2type=$request->top2_type;
      $top3=$request->top3;
      $top3type=$request->top3_type;
      $mid=$request->mid;
      $midtype=$request->mid_type;
      $mid1=$request->mid1;
      $mid1type=$request->mid1_type;
      $mid2=$request->mid2;
      $mid2type=$request->mid2_type;
      $mid3=$request->mid3;
      $mid3type=$request->mid3_type;
      $bottom=$request->bottom;
      $bottomtype=$request->bottom_type;
      $bottom1=$request->bottom1;
      $bottom1type=$request->bottom1_type;
      $bottom2=$request->bottom2;
      $bottom2type=$request->bottom2_type;
      $bottom3=$request->bottom3;
      $bottom3type=$request->bottom3_type;
      $userid = Auth::user()->id;
      $updateobj = \App\Template::find($request->template_id);
      // db name        //variable name
      $updateobj->title = $titl;
      $updateobj->top = $top;
      $updateobj->top_type = $toptype;
      $updateobj->top1 = $top1;
      $updateobj->top1_type = $top1type;
      $updateobj->top2 = $top2;
      $updateobj->top2_type = $top2type;
      $updateobj->top3 = $top3;
      $updateobj->top3_type = $top3type;
      $updateobj->mid = $mid;
      $updateobj->mid_type = $midtype;
      $updateobj->mid1 = $mid1;
      $updateobj->mid1_type = $mid1type;
      $updateobj->mid2 = $mid2;
      $updateobj->mid2_type = $mid2type;
      $updateobj->mid3 = $mid3;
      $updateobj->mid3_type = $mid3type;
      $updateobj->bottom = $bottom;
      $updateobj->bottom_type = $bottomtype;
      $updateobj->bottom1 = $bottom1;
      $updateobj->bottom1_type = $bottom1type;
      $updateobj->bottom2 = $bottom2;
      $updateobj->bottom2_type = $bottom2type;
      $updateobj->bottom3 = $bottom3;
      $updateobj->bottom3_type = $bottom3type;
      $updateobj->updated_by = $userid;
      $updateobj->save();
      return redirect('template');
    }
    ///////////////////////////
    //priority//////////
    public function priority(Request $request)
    {   
      $office_id=$request->session()->get('session_office');      
      return view('priority', compact('office_id'));   
    }
    public function prioritypost(Request $request)
    {
      $this->validate($request,[
          'priority' => 'required',//name of blade name
          'num' => 'required'    
        ]);
      $office_id=$request->session()->get('session_office');   
      $priority=$request->priority;
      $actions=$request->num;
      $id = Auth::user()->id;
      $insert_id = \App\Priority::create([//model name database field name
            'priority' => $priority,
            'actions_per_days' => $actions,
            'created_by' => $id,
            'updated_by'=>$id,
            'office_id' => $office_id,
         ]);
      if($insert_id)
        notify()->success('Priority Added!');
      else
        smilify('Error', 'Error Occurred Pls recheck!');
      return redirect()->back();
    }
    public function prioritypostdata(Request $request)
    {
      $office_id=$request->session()->get('session_office'); 
      $priorities =\App\Priority::where('office_id',$office_id)->get();
      $data  = [];
      $i=0;
      foreach ($priorities as $priority)
      {
          $id=$priority->id;//db name
          $prio=$priority->priority;
          $actn=$priority->actions_per_days;
          $updated_at=$priority->updated_at->format('d-M-Y');
          $obj = new \stdClass;
          $obj->incid = $i + 1;
          $obj->id = $id;
          $obj->priority = $prio;
          $obj->actions_per_days = $actn;
          $obj->updated_at = $updated_at;
          $data[$i] = $obj;
          $i++;
      }
      $prioritypostdata = collect($data);
      return Datatables::of($prioritypostdata)
            ->addColumn('action', function ($prioritydata) {
                return '<a href="' . url('showprioritydata') . '/' . $prioritydata->id . '" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> Show</a>';
            })
            ->editColumn('id', 'ID: {{$incid}}')
            ->make(true);
    }
    public function showprioritydata(Request $request,$id)//show button
    {
      $office_id=$request->session()->get('session_office'); 
      $priority=\App\Priority::where('id',$id)->first();
      return view('showprioritydata', compact('priority'));
    }
    public function updateprioritydata(Request $request)//update button
    {
      //validation of fields
      $this->validate($request,[
        //form field(blade name)
            'priority' => 'required',
            'num' => 'required',
       ]);
      //variable name
      $prior=$request->priority;
      $actndays=$request->num;
      $userid = Auth::user()->id;
      $updateobj = \App\Priority::find($request->priority_id);
      // db name        //variable name
      $updateobj->priority = $prior;
      $updateobj->actions_per_days = $actndays;
      $updateobj->updated_by = $userid;
      $updateobj->created_by = $userid;
      $updateobj->save();
      return redirect('priority');
    }
    //////////////////////////    
    //search//////////////
    public function search(Request $request)
    {
        $office_id=$request->session()->get('session_office');
        $subjects=App\Subject::where('office_id',$office_id)->get();
        $file_groups=App\File_Group::where('office_id',$office_id)->get();
        return view('search',compact('subjects','file_groups'));    
    }
    public function searchpostdata(Request $request)
    {
        //dd($request);
        $office_id=$request->session()->get('session_office');
        $this->validate($request,[
            'type' => 'required',//name of blade name 
          ]);
        $tapaldata=array();
        $filedata=array();
        $type=$request->type;
        $number=$request->number;
        $fromdate=$request->fromdate;
        $todate=$request->todate;
        $keyword=$request->keyword;
        $subject=$request->subject;
        $title=$request->title;
        $file_group=$request->file_group;
        $tapal_info="";
        $file_info="";
        if($type=='tapal') 
        {
          $i=0;
          if($request->number)
          {
            $match[$i]=['tapal_no','like','%'.$number.'%'];
            $i++;
          }
          if($request->fromdate)
          {
            $match[$i]=['created_at','>=',$fromdate];
            $i++;
          }
          if($request->todate)
          {
            $match[$i]=['created_at','<=',$todate];
            $i++;
          }
          if($request->keyword)
          {
             $match[$i]=['keywords','like','%'.$keyword.'%'];
             $i++;
          }
          if($request->subject)
          {
             $match[$i]=['subject_id','like','%'.$subject.'%'];
             $i++;
          }
          $userid = Auth::user()->id;  
          if(isset($match))
            $tapal_info=\App\Tapal::where('office_id',$office_id)->where($match)->with('subject')->get();
          else
            $tapal_info=\App\Tapal::where('office_id',$office_id)->with('subject')->get();
          $a=0;
          //dd($tapal_info);
          foreach ($tapal_info as $tapals) 
          {
              $tapalid=$tapals->id;
              $tapalno=$tapals->tapal_no;
              $flag=0;
              $seatid=$request->session()->get('session_seat');
              $tapalcount =\App\Tapal_forward::where('to_seat',$seatid)->where('tapal_id',$tapalid)->count();
              if($tapalcount>0)
              {
                $flag=1;
              }
              if($flag==1)
              {
                $tapaldata[$a]=$tapals;
                $a++;
              }
          }
        }
        if ($type=='file') 
        {
          $i=0;
          if($request->number)
          {
             $matchlist[$i]=['file_no','like','%'.$number.'%'];
             $i++;
          }
          if($request->fromdate)
          {
             $matchlist[$i]=['created_at','>=',$fromdate];
             $i++;
          }
          if($request->todate)
          {
             $matchlist[$i]=['created_at','<=',$todate];
             $i++;
          }
          if($request->title)
          {
             $matchlist[$i]=['title','like','%'.$title.'%'];
             $i++;
          }
          if($request->file_group)
          {
             $matchlist[$i]=['file_group_id','like','%'.$file_group.'%'];
             $i++;
          }
          $user_id = Auth::user()->id;
          if(isset($matchlist))
            $file_info=\App\File::where('office_id',$office_id)->where($matchlist)->with('file_groupobj')->get();
          else
            $file_info=\App\File::where('office_id',$office_id)->with('file_groupobj')->get();
          $b=0;
          foreach ($file_info as $files) 
          {
            $fileid=$files->id;
            $fileno=$files->file_no;
            $fileflag=0;
            $seat_id=$request->session()->get('session_seat');
            $filecount =\App\File_forward::where('to_seat',$seat_id)->where('file_id',$fileid)->count();
            if($filecount>0)
            {
              $fileflag=1;
            }

            if($fileflag==1)
            {
              $filedata[$b]=$files;
              $b++;
            } 
          }
        }
        return view('searchdisplay', compact('filedata','tapaldata'));
    }
    /*public function searchdisplay()
    {
        $subjects=App\Subject::all();
        return view('searchdisplay',compact('subjects'));    
    }*/
    public function tapal_view($id)
    {    
      $tapal_id= $id;
      $creation_of_tapal=\App\Tapal::where('id',$id)->first();
      $subject_group =\App\Subject_Group::where('id',$creation_of_tapal->subject_group_id)->first();
      $subject =\App\Subject::where('id',$creation_of_tapal->subject_id)->first();
      $department=\App\Department::all();
      $section =\App\Section::all();
      $seats =\App\Seat::all();
      $actions =\App\Action::all();
      $file_groups =\App\File_Group::all();
      $username = Auth::user()->name;
      $values = \App\Comments::where('tapal_id',$tapal_id)->with('seat')->orderBy('created_at','desc')->get();
      $attachmentcount =\App\Documents::where('tapal_id',$tapal_id)->count();
      $attachments=\App\Documents::where('tapal_id',$tapal_id)->get();
      $values1 = \App\Tapal_action::where('tapal_id',$tapal_id)->with('seat')->orderBy('created_at','desc')->get();
      $userid = Auth::user()->id;
      $us_arr =\App\User_Seat::where('user_id',$userid)->with('seat')->get();
      //dd($us_arr);
      $act_flg_status = "";
      foreach($us_arr as $us_arrlist)
      {
        $seatid_new = $us_arrlist->seat_id;
        $seatname= $us_arrlist->seat->seat_name;
        $match=[['to_seat',$seatid_new],['tapal_id',$id]];
        //print_r($match);
        $current_tapaluser=\App\Tapal_forward::where($match)->get();
        foreach($current_tapaluser as $tapvallist)
        {
          $active_flag = $tapvallist->active_flag;
          if($active_flag == 1)
          {
            $act_flg_status = 1;
          } 
        }
      }
      $tapal_forward = \App\Tapal_forward::where('to_seat', $seatid_new)
          ->where('tapal_id', $tapal_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;
      return view('tapal_view', compact('values','values1','seatid_new','username','subject_group','subject','creation_of_tapal','department','section','seats','act_flg_status','actions','actions','file_groups','fileprint','us_arr','seatname','attachmentcount', 'attachments','tapal_id','tapal_forward'));
    }
    public function file_view($id)
    {    
      $file_id= $id;
      $creation_of_file=\App\File::where('id',$id)->first();
      $file_group =\App\File_Group::where('id',$creation_of_file->file_group_id)->first();
      $actions =\App\Action::all();
      $department=\App\Department::all();
      $section =\App\Section::all();
      $seat =\App\Seat::all();
      $template =\App\Template::all();
      $ftemplate =\App\Fileview_template::where('file_id',$file_id)->where('cancel_flag',0)->with('template')->get();
      $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 = \App\Comments::where('tapal_id', $tapalid)->with('tapal','seat')->orderBy('created_at','DESC')->get();
        //dd($comments);
        /*foreach ($comments as $com)
        {
           $created_at=$com->created_at->format('d-M-Y');
           $tapalno=$com->tapal->tapal_no;
           $comment_type=$com->comment_type;
           $comment_description=$com->comment_description;
           $seatname=$com->seat->seat_name;
           $commentseatid=$com->seat->seat_name;
        }*/
      }
      $tapalactionscount=0;
      // $checkforseatmatch=\App\User_Seat::where('user_id',$userid)->where('seat_id',$commentseatid)->count();
      $tapalactions_arr = \App\File_reference::where('file_id', $id)->with('tapal')->get();
      foreach ($tapalactions_arr as $tapalactions_arr)
      {
        $tapalid=$tapalactions_arr->tapal_id;
        $tapalactionscount = \App\Tapal_action::where('tapal_id', $tapalid)->count();
        $tapalactions = \App\Tapal_action::where('tapal_id', $tapalid)->with('tapal','seat')->orderBy('created_at','DESC')->get();
        //dd($comments);
        /* foreach ($tapalactions as $act)
        {
           $created_at=$act->created_at->format('d-M-Y');
           $tapalnoact=$act->tapal->tapal_no;
           $action_type=$act->action_type;
           $action_description=$act->action_description;
           $seatname=$act->seat->seat_name;
        }*/
      }
      $tapalattachmentscount=0;
      $tapalattachments_arr = \App\File_reference::where('file_id', $id)->with('tapal')->get();
      foreach ($tapalattachments_arr as $tapalattachment_arr)
      {
        $tapalid=$tapalattachment_arr->tapal_id;
        $tapalattachmentscount = \App\Documents::where('tapal_id', $tapalid)->count();
        $tapalattachments = \App\Documents::where('tapal_id', $tapalid)->with('tapal','seat')->orderBy('created_at','DESC')->get();
        /*foreach ($tapalattachments as $att)
        {
          $created_at=$att->created_at->format('d-M-Y');
          $tapalnoatt=$att->tapal->tapal_no;
          $document_path=$att->document_path;
          $document_name=$att->document_name;
        }*/
      }
      $username = Auth::user()->name;
      $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)->get();
      $values1 = \App\File_action::where('file_id',$file_id)->with('seat')->orderBy('created_at','desc')->get();
      $userid = Auth::user()->id;
      $act_flg_status = "";
      $us_arr =\App\User_Seat::where('user_id',$userid)->with('seat')->get();
      foreach($us_arr as $us_arrlist)
      {
        $seatid_new = $us_arrlist->seat_id;
        $seatname= $us_arrlist->seat->seat_name;
        $match=[['to_seat',$seatid_new],['file_id',$id]];
        $current_fileuser=\App\File_forward::where($match)->get();
        foreach($current_fileuser as $filevallist)
        {
          $active_flag = $filevallist->active_flag;
          if($active_flag == 1)
          {
            $act_flg_status = 1;
          }
        }
      }
      $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;
      return view('file_view', compact('values','values1','seatid_new','username','file_group','creation_of_file','department','section','seat','act_flg_status','fileprint','us_arr','seatname','attachmentcount','attachments','actions','ftemplate','template','flags','tapalcomments_arr','comments','tapalno','tapalactions_arr','tapalactions','tapalnoact','tapalattachments_arr','tapalattachments','tapalnoatt','commentscount','active_flag','tapalactionscount','tapalattachmentscount','fileview_template_id'));
    }
    ///////////////////////////
    //subject
    public function subject(Request $request)
    {  
      $office_id=$request->session()->get('session_office');  
      $officenames=\App\Office::where('id',$office_id)->get();
      return view('subject', compact('officenames'));  
    }
    public function subjectpost(Request $request)
    {
      $this->validate($request,[
          'subject' => 'required',//name of blade name  
          ]);
      $office_id=$request->session()->get('session_office'); 
      $subjectname=$request->subject;
      $id = Auth::user()->id;
      $insert_id = \App\Subject::create([//model name database field name
            'subject' => $subjectname,
            'updated_by'=>$id,
            'office_id' => $office_id,
         ]);
      if($insert_id)
         notify()->success('Subject Added!');
      else
        smilify('Error', 'Error Occurred Pls recheck!');
      return redirect()->back();
    }
    public function subjectpostdata(Request $request)
    {
      $office_id=$request->session()->get('session_office'); 
      $subjects =\App\Subject::where('office_id',$office_id)->get();
      $data  = [];
      $i=0;
      foreach ($subjects as $subject)
      {
          $id=$subject->id;//db name
          $subname=$subject->subject;
          $updated_at=$subject->updated_at->format('d-M-Y');
          $obj = new \stdClass;
          $obj->incid = $i + 1;
          $obj->id = $id;
          $obj->subject = $subname;
          $obj->updated_at = $updated_at;
          $data[$i] = $obj;
          $i++;
      }
      $subjectpostdata = collect($data);
      return Datatables::of($subjectpostdata)
            ->addColumn('action', function ($subjectdata) {
                return '<a href="' . url('showsubjectdata') . '/' . $subjectdata->id . '" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> Show</a>';
            })
            ->editColumn('id', 'ID: {{$incid}}')
            ->make(true);    
    }
    public function showsubjectdata(Request $request,$id)//show button
    {
      $subject=\App\Subject::where('id',$id)->first();
      return view('showsubjectdata', compact('subject'));
    }
    public function updatesubjectdata(Request $request)//update button
    {
      //validation of fields
      $this->validate($request,[
        //form field(blade name)
          'subject' => 'required',
        ]);
      //variable name
      $sbjname=$request->subject;
      $userid = Auth::user()->id;
      $updateobj = \App\Subject::find($request->subject_id);
      // db name        //variable name
      $updateobj->subject = $sbjname;
      $updateobj->updated_by = $userid;
      $updateobj->save();
      return redirect('subject');
    }
    //subject_group
    public function subject_group()
    {   
        return view('subject_group');
    }
    public function subject_grouppost(Request $request)
    {
      $this->validate($request,[
            //name of blade name
            'subject_group' => 'required'
        ]);
      $office_id=$request->session()->get('session_office');  
      $subject_grpnme=$request->subject_group;
      $id = Auth::user()->id;
      $insert_id = \App\Subject_Group::create([//model name database field name
        'subject_group' => $subject_grpnme,
        'updated_by'=>$id,
        'office_id' => $office_id,
           ]);
      if($insert_id)
       notify()->success('Subject Group Added!');
      else
        smilify('Error', 'Error Occurred Pls recheck!'); 
      return redirect()->back();
    }
    public function subject_grouppostdata(Request $request)
    {
      $office_id=$request->session()->get('session_office');  
      $subject_groups =\App\Subject_Group::where('office_id',$office_id)->with('subject')->get();
      $data  = [];
      $i=0;
      foreach ($subject_groups as $subject_group)
      {
          $id=$subject_group->id;//db name
          $sub_grp=$subject_group->subject_group;
          $updated_at=$subject_group->updated_at->format('d-M-Y');
          $obj = new \stdClass;
          $obj->incid = $i + 1;
          $obj->id = $id;
          $obj->subject_group = $sub_grp;
          $obj->updated_at = $updated_at;
          $data[$i] = $obj;
          $i++;
      }
      $subject_grouppostdata = collect($data);
      return Datatables::of($subject_grouppostdata)
            ->addColumn('action', function ($subject_groupdata) {
                return '<a href="' . url('showsubject_groupdata') . '/' . $subject_groupdata->id . '" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> Show</a>';
            })
            ->editColumn('id', 'ID: {{$incid}}')
            ->make(true);
    }
    public function showsubject_groupdata($id)//show button
    {
      $subject_group =\App\Subject_Group::where('id',$id)->first();
      return view('showsubject_groupdata', compact('subject_group'));
    }
    public function updatesubject_groupdata(Request $request)//update button
    {
      //validation of fields
      $this->validate($request,[
        //form field(blade name)
        'subject_group' => 'required'
       ]);
      //variable name
      $sub_group=$request->subject_group;
      $userid = Auth::user()->id;
      $updateobj = \App\Subject_Group::find($request->subject_group_id);
      $updateobj->subject_group = $sub_group;
      $updateobj->updated_by = $userid;
      $updateobj->save();
      return redirect('subject_group');
    }
    //actions
    public function action()
    {  
        return view('action');
    }
    public function actionpost(Request $request)
    {
      $this->validate($request,[
            'actions' => 'required',//name of blade name
            'description' => 'required'    
        ]);
      $office_id=$request->session()->get('session_office'); 
      $actionname=$request->actions;
      $desc=$request->description;
      $id = Auth::user()->id;
      $insert_id = \App\Action::create([//model name database field name
          'actions' => $actionname,
          'description' => $desc,
          'updated_by'=>$id,
          'office_id' => $office_id,
           ]);
      if($insert_id)
        notify()->success('Action Added!');
      else
        smilify('Error', 'Error Occurred Pls recheck!'); 
      return redirect()->back();
    }
    public function actionpostdata(Request $request)
    {
      $office_id=$request->session()->get('session_office'); 
      $actions =\App\Action::where('office_id',$office_id)->get();
      $data  = [];
      $i=0;
      foreach ($actions as $action)
      {
        $id=$action->id;//db name
        $actn=$action->actions;
        $desc=$action->description;
        $updated_at=$action->updated_at->format('d-M-Y');
        $obj = new \stdClass;
        $obj->incid = $i + 1;
        $obj->id = $id;
        $obj->actions = $actn;
        $obj->description = $desc;
        $obj->updated_at = $updated_at;
        $data[$i] = $obj;
        $i++;
      }
      $actionpostdata = collect($data);
      return Datatables::of($actionpostdata)
            ->addColumn('action', function ($actiondata) {
                return '<a href="' . url('showactiondata') . '/' . $actiondata->id . '" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> Show</a>';
            })
            ->editColumn('id', 'ID: {{$incid}}')
            ->make(true);
    }
    public function showactiondata($id)//show button
    {
      $action=\App\Action::where('id',$id)->first();
      return view('showactiondata', compact('action'));
    }
    public function updateactiondata(Request $request)//update button
    {
        //validation of fields
        $this->validate($request,[
            //form field(blade name)
                'actions' => 'required',//name of blade name
                'description' => 'required'
           ]);
        //variable name
        $actnnme=$request->actions;
        $descrip=$request->description;
        $userid = Auth::user()->id;
        $updateobj = \App\Action::find($request->action_id);
        $updateobj->actions = $actnnme;
        $updateobj->description = $descrip;
        $updateobj->updated_by = $userid;
        $updateobj->save();
        return redirect('action');
    }
    //action_subjectgroup
    public function action_subjectgroup(Request $request)
    {
        $office_id=$request->session()->get('session_office'); 
        $actions=App\Action::where('office_id',$office_id)->get();
        $subject_groups=App\Subject_Group::where('office_id',$office_id)->get();
        return view('action_subjectgroup', compact('actions','subject_groups'));
    }
    public function action_subjectgrouppost(Request $request)
    {
      $this->validate($request,[
            'actions' => 'required',
            'subject_group' => 'required'//name of blade name    
       ]);
      $office_id=$request->session()->get('session_office'); 
      $action_id=$request->actions;
      $subject_group_id=$request->subject_group;
      $count=\App\Action_subjectgroup::where('action_id',$action_id)->where('subject_group_id',$subject_group_id)->count();
      if($count==1)
      {
        smilify('Error', 'Already assigned!');
        return redirect()->back();
      }     
      $id = Auth::user()->id;
      $insert_id = \App\Action_subjectgroup::create([//model name database field name
          'action_id' => $action_id,
          'subject_group_id' => $subject_group_id,
          'updated_by'=>$id,
          'office_id' => $office_id,
           ]);
      if($insert_id)
      notify()->success('Action Assigned to Subjectgroup!');
      else
      smilify('Error', 'Error Occurred Pls recheck!');
      return redirect()->back();
    }
    public function action_subjectgrouppostdata(Request $request)
    {
      $office_id=$request->session()->get('session_office'); 
      $action_subjectgroups =\App\Action_subjectgroup::where('office_id',$office_id)->with('action','subject_group')->get();
      $data  = [];
      $i=0;
      foreach ($action_subjectgroups as $action_subjectgroup)
      {
          $id=$action_subjectgroup->id;//db name
          $actnnme=$action_subjectgroup->action->actions;
          $sub_grp=$action_subjectgroup->subject_group->subject_group;
          $updated_at=$action_subjectgroup->updated_at->format('d-M-Y');
          $obj = new \stdClass;
          $obj->incid = $i + 1;
          $obj->id = $id;
          $obj->actions = $actnnme;
          $obj->subject_group = $sub_grp;
          $obj->updated_at = $updated_at;
          $data[$i] = $obj;
          $i++;
      }
      $action_subjectgrouppostdata = collect($data);
      return Datatables::of($action_subjectgrouppostdata)
            ->addColumn('action', function ($action_subjectgroupdata) {
                return '<a href="' . url('showaction_subjectgroupdata') . '/' . $action_subjectgroupdata->id . '" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> Show</a>';
            })
            ->editColumn('id', 'ID: {{$incid}}')
            ->make(true);
    }
    public function showaction_subjectgroupdata(Request $request,$id)//show button
    {
      $office_id=$request->session()->get('session_office'); 
      $actions=App\Action::where('office_id',$office_id)->get();
      $subject_groups=App\Subject_Group::where('office_id',$office_id)->get();
      $action_subjectgroup=\App\Action_subjectgroup::where('id',$id)->with('action','subject_group')->first();
      return view('showaction_subjectgroupdata', compact('action_subjectgroup','actions','subject_groups'));
    }
    public function cancelaction_subjectgroupdata(Request $request)//update button
    {
      $id=$request->action_subjectgroup_id;
      $delete=\App\Action_subjectgroup::where('id',$id)->delete();
      return redirect('action_subjectgroup');
    }
    //subjectgroup_subject
    public function subjectgroup_subject(Request $request)
    {
        $office_id=$request->session()->get('session_office');
        $subject_groups=\App\Subject_Group::where('office_id',$office_id)->get();
        $subjects=\App\Subject::where('office_id',$office_id)->get();
        return view('subjectgroup_subject', compact('subject_groups','subjects'));   
    }
    public function subjectgroup_subjectpost(Request $request)
    {
      $this->validate($request,[
            'subject_group' => 'required',
            'subject' => 'required'//name of blade name 
       ]);
      $office_id=$request->session()->get('session_office');
      $subject_group_id=$request->subject_group;
      $subject_id=$request->subject;
      $count=\App\Subjectgroup_subject::where('subject_group_id',$subject_group_id)->where('subject_id',$subject_id)->where('office_id',$office_id)->count();
      if($count==1)
      {
        smilify('Error', 'Already assigned!'); 
        return redirect()->back();
      }
      $id = Auth::user()->id;
      $insert_id = \App\Subjectgroup_subject::create([//model name database field name
        'subject_group_id' => $subject_group_id,
        'subject_id' => $subject_id,
        'updated_by'=>$id,
        'office_id' => $office_id,
         ]);
      if($insert_id)
        notify()->success('Subjectgroup Assigned to Subject!');
      else
        smilify('Error', 'Error Occurred Pls recheck!'); 
      return redirect()->back();
    }
    public function subjectgroup_subjectpostdata(Request $request)
    {
      $office_id=$request->session()->get('session_office');
      // $role_permissions =\App\Role_Permission::all();
      $subjectgroup_subjects =\App\Subjectgroup_subject::where('office_id',$office_id)->with('subject_group','subject')->get();
      $data  = [];
      $i=0;
      foreach ($subjectgroup_subjects as $subjectgroup_subject)
      {
          $id=$subjectgroup_subject->id;//db name
          $sub_grp=$subjectgroup_subject->subject_group->subject_group;
          $sub=$subjectgroup_subject->subject->subject;
          $updated_at=$subjectgroup_subject->updated_at->format('d-M-Y');
          $obj = new \stdClass;
          $obj->incid = $i + 1;
          $obj->id = $id;
          $obj->subject_group = $sub_grp;
          $obj->subject = $sub;
          $obj->updated_at = $updated_at;
          $data[$i] = $obj;
          $i++;
      }
      $subjectgroup_subjectpostdata = collect($data);
      return Datatables::of($subjectgroup_subjectpostdata)
            ->addColumn('action', function ($subjectgroup_subjectdata) {
                return '<a href="' . url('showsubjectgroup_subjectdata') . '/' . $subjectgroup_subjectdata->id . '" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> Show</a>';
            })
            ->editColumn('id', 'ID: {{$incid}}')
            ->make(true);
    }
    public function showsubjectgroup_subjectdata(Request $request, $id)//show button
    {
      $office_id=$request->session()->get('session_office');
      $subject_groups =\App\Subject_Group::where('office_id',$office_id)->get();
      $subjects =\App\Subject::where('office_id',$office_id)->get();
      $subjectgroup_subject=\App\Subjectgroup_subject::where('id',$id)->with('subject_group','subject')->first();
      return view('showsubjectgroup_subjectdata', compact('subjectgroup_subject','subject_groups','subjects'));
    }
    public function cancelsubjectgroup_subjectdata(Request $request)//update button
    {
      $id=$request->subjectgroup_subject_id;
      $delete=\App\Subjectgroup_subject::where('id',$id)->delete();
      return redirect('subjectgroup_subject');
    }
    //file_group
    public function file_group(Request $request)
    {   
        $office_id=$request->session()->get('session_office');
        $officenames=App\Office::all();
        return view('file_group', compact('officenames'));   
    }
    public function file_grouppost(Request $request)
    {
      $this->validate($request,[
            //name of blade name
            'file_group' => 'required',
            'description' => 'required'
       ]);
      $office_id=$request->session()->get('session_office');
      $file_grpnme=$request->file_group;
      $descriptio=$request->description;
      $id = Auth::user()->id;
      $insert_id = \App\File_Group::create([//model name database field name
        'file_group' => $file_grpnme,
        'description' => $descriptio,
         'updated_by'=>$id,
         'office_id' => $office_id,
         ]);
      if($insert_id)
        notify()->success('File Group Added!');
      else
        smilify('Error', 'Error Occurred Pls recheck!'); 
      return redirect()->back();
    }
    public function file_grouppostdata(Request $request)
    {
      $office_id=$request->session()->get('session_office');
      $file_groups =\App\File_Group::where('office_id',$office_id)->get();
      $data  = [];
      $i=0;
      foreach ($file_groups as $file_group)
      {
          $id=$file_group->id;//db name
          $file_grp=$file_group->file_group;
          $desc=$file_group->description;
          $updated_at=$file_group->updated_at->format('d-M-Y');
          $obj = new \stdClass;
          $obj->incid = $i + 1;
          $obj->id = $id;
          $obj->file_group = $file_grp;
          $obj->description = $desc;
          $obj->updated_at = $updated_at;
          $data[$i] = $obj;
          $i++;
      }
      $file_grouppostdata = collect($data);
      return Datatables::of($file_grouppostdata)
            ->addColumn('action', function ($file_groupdata) {
                return '<a href="' . url('showfile_groupdata') . '/' . $file_groupdata->id . '" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> Show</a>';
            })
            ->editColumn('id', 'ID: {{$incid}}')
            ->make(true);
    }
    public function showfile_groupdata($id)//show button
    {
      $file_group =\App\File_Group::where('id',$id)->first();
      return view('showfile_groupdata', compact('file_group'));
    }
    public function updatefile_groupdata(Request $request)//update button
    {
      //validation of fields
      $this->validate($request,[
        //form field(blade name)
            'file_group' => 'required',
            'description' => 'required' 
       ]);
      //variable name
      $sub_group=$request->file_group;
      $descr=$request->description;
      $userid = Auth::user()->id;
      $updateobj = \App\File_Group::find($request->file_group_id);
      $updateobj->file_group = $sub_group;
      $updateobj->description = $descr;
      $updateobj->updated_by = $userid;
      $updateobj->save();
      return redirect('file_group');
    }
}