<?php
namespace App\Http\Controllers;
use Yajra\Datatables\Datatables as Datatables;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Facades\Excel;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use App\Helper\MailHelper as MailHelper;
use Notification;
use Auth;
use App\Models\User;
use App\Models\Settings\Organisation;
use App\Models\Settings\Department;
use App\Models\Settings\Section;
use App\Models\Settings\MaterialType;
use App\Models\Settings\MaterialSubType;
use App\Models\Settings\Topic;
use App\Models\Settings\ContentType;
use App\Models\Settings\MimeType;
use App\Models\Settings\Roles;
use App\Models\Settings\Permissions;
use App\Models\Settings\RolePermission;
use App\Models\Settings\UserRoles;
use App\Models\Content\Content;
use App\Models\Content\ContentDocument;
use App\Models\Content\ContentAssignment;
use App\Models\Content\RejectClarify;
use App\Models\Content\Marks;
use App\Models\Training\Schedule;
use App\Models\Training\ScheduleRoles;
use App\Models\Training\ScheduleTopics;
use App\Models\Training\Participant;
use App\Models\Training\Certificate;
use App\Models\Training\Attendance;
use App\Models\Training\Accept;

use Request as req;

class TraineeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function active_trainings()
    {
        return view('Trainee/active_trainings');
    }
    public function get_active_training_details (Request $request)
    {
        $columns = array(
            0=> 'id',
            1=>'material_type',
            2=>'training_title',
            3=>'venue',
            4=>'training_period',
            5=> 'action',
        );
        $user_id = Auth::user()->id;
        $totalData = Schedule::with('schedule_participants','material_type')->whereHas('schedule_participants',function($usr) use($user_id) { $usr->where('user_id',$user_id); })->count();
        $totalFiltered = $totalData; 
        $limit = $request->input('length');
        $start = $request->input('start');
        $order = $columns[$request->input('order.0.column')];
        $dir = $request->input('order.0.dir');
        if(empty($request->input('search.value')))
        {       
            $posts = Schedule::with('schedule_participants','material_type')->whereHas('schedule_participants',function($usr) use($user_id) { $usr->where('user_id',$user_id); })
                         ->offset($start)
                         ->limit($limit)
                         ->orderBy('id','desc')
                         ->get();
        }
        else 
        {
            $search = $request->input('search.value'); 
            $posts =  Schedule::with('schedule_participants','material_type')->whereHas('schedule_participants',function($usr) use($user_id) { $usr->where('user_id',$user_id); })
                            ->where('training_title', 'LIKE',"%{$search}%")
                            ->orWhere('venue', 'LIKE',"%{$search}%")
                            ->offset($start)
                            ->limit($limit)
                            ->orderBy($order,$dir)
                            ->get();
            $totalFiltered = Schedule::with('schedule_participants','material_type')->whereHas('schedule_participants',function($usr) use($user_id) { $usr->where('user_id',$user_id); })
                            ->where('training_title', 'LIKE',"%{$search}%")
                            ->orWhere('venue', 'LIKE',"%{$search}%")
                            ->count();
        }
        $data = array();
       // dd($posts);
        if(!empty($posts))
        {
            $material_type_name = "";
            $organisation_name = "";
            $training_title ="";
            $venue = "";
            $reg_url = "";

            foreach ($posts as $post)
            {   
                $schedule_id = $post->id;
                if(isset($post->material_type_id))
                    $material_type_name = $post->material_type->material_type_name;
                $training_title = $post->training_title;
                $venue = $post->venue;
                $period_from = $post->period_from;
                $period_to = $post->period_to;
                if($period_from)
                {
                    $period_from = date("d-M-Y", strtotime($period_from));
                }
                if($period_to)
                {
                    $period_to = date("d-M-Y", strtotime($period_to));
                }
                $training_period = $period_from." to ".$period_to;
                $registration_status= $post->registration_status;
                if($registration_status==1)
                {
                    //$reg_url = '&nbsp;&nbsp;<a href="'.url('register_training').'/'.$post->id.'" title="Register Now" class="btn btn-xs btn-warning">Register Now</a>';
                }
                //&nbsp;&nbsp;<a href="'.url('accept_training_invitation').'/'.$post->id.'" title="Accept Invitation" class="btn btn-xs btn-success">Accept Invitation</a>
                $nestedData['DT_RowIndex'] = $post->id;
                $nestedData['material_type'] = $material_type_name;
                $nestedData['training_title'] = $training_title;
                $nestedData['venue'] = $venue;
                $nestedData['training_period'] = $training_period;
                $nestedData['action'] = '<a href="'.url('view_training_details').'/'.$post->id.'" title="View Details" class="btn btn-xs btn-primary">View Details</a>'.$reg_url;
                $data[] = $nestedData;
            }
        }
        $json_data = array(
                    "draw"            => intval($request->input('draw')),  
                    "recordsTotal"    => intval($totalData),  
                    "recordsFiltered" => intval($totalFiltered), 
                    "data"            => $data   
                    );
        echo json_encode($json_data); 
    }

    public function view_training_details($id)
    {
        $schedules = Schedule::where('id',$id)->with('organisation','department','section','material_type','schedule_role','schedule_participants')->first();
        return view('Trainee/view_training_details', compact('schedules'));
    }

    public function accept_training_invitation($id)
    {
        $user_id = Auth::user()->id;
        $schedules = Schedule::where('id',$id)->with('organisation','department','section','material_type','schedule_role','schedule_participants')->first();
        $accept_training_count = Accept::where('schedule_id',$id)->where('user_id',$user_id)->count();
        return view('Trainee/accept_training_invitation', compact('schedules','accept_training_count'));
    }

    public function training_session()
    {
        return view('Trainee/training_session');
    }
    public function get_training_details (Request $request)
    {
        $columns = array(
            0=> 'id',
            1=>'material_type',
            2=>'training_title',
            3=>'venue',
            4=>'training_period',
            5=> 'action',
        );
        $user_id = Auth::user()->id;
        $totalData = Participant::where('user_id',$user_id)->with('schedule')->count();
        $totalFiltered = $totalData; 
        $limit = $request->input('length');
        $start = $request->input('start');
        $order = $columns[$request->input('order.0.column')];
        $dir = $request->input('order.0.dir');
        if(empty($request->input('search.value')))
        {       
            $posts = Participant::where('user_id',$user_id)->with('schedule')
                         ->offset($start)
                         ->limit($limit)
                         ->orderBy('id','desc')
                         ->get();
        }
        else 
        {
            $search = $request->input('search.value'); 
            $posts =  Participant::where('user_id',$user_id)->with('schedule')
                            ->where('training_title', 'LIKE',"%{$search}%")
                            ->orWhere('venue', 'LIKE',"%{$search}%")
                            ->offset($start)
                            ->limit($limit)
                            ->orderBy($order,$dir)
                            ->get();
            $totalFiltered = Participant::where('user_id',$user_id)->with('schedule')
                            ->where('training_title', 'LIKE',"%{$search}%")
                            ->orWhere('venue', 'LIKE',"%{$search}%")
                            ->count();
        }
        $data = array();
       // dd($posts);
        if(!empty($posts))
        {
            $material_type_name = "";
            $organisation_name = "";
            $training_title ="";
            $venue = "";
            $reg_url = "";
            $n =0;
            foreach ($posts as $post)
            {   
                $n++;
                $schedule_id = $post->schedule_id;
                $participant_id = $post->user_id;
                if(isset($post->schedule->material_type_id))
                    $material_type_name = $post->schedule->material_type->material_type_name;
                $training_title = $post->schedule->training_title;
                $venue = $post->schedule->venue;
                $period_from = $post->schedule->period_from;
                $period_to = $post->schedule->period_to;
                if($period_from)
                {
                    $period_from = date("d-M-Y", strtotime($period_from));
                }
                if($period_to)
                {
                    $period_to = date("d-M-Y", strtotime($period_to));
                }
                $training_period = $period_from." to ".$period_to;
                
                $nestedData['DT_RowIndex'] = $n;
                $nestedData['material_type'] = $material_type_name;
                $nestedData['training_title'] = $training_title;
                $nestedData['venue'] = $venue;
                $nestedData['training_period'] = $training_period;
                $nestedData['action'] = '<a href="'.url('view_training_session_details').'/'.$participant_id.'" title="View Session Details" class="btn btn-xs btn-primary">Training Session Details</a>'.$reg_url;
                $data[] = $nestedData;
            }
        }
        $json_data = array(
                    "draw"            => intval($request->input('draw')),  
                    "recordsTotal"    => intval($totalData),  
                    "recordsFiltered" => intval($totalFiltered), 
                    "data"            => $data   
                    );
        echo json_encode($json_data); 
    }
    public function view_training_session_details($participant_id)
    {
        $schedule_trainings = Participant::where('user_id',$participant_id)->with('schedule')->get();
        return view('Trainee/view_training_session_details', compact('schedule_trainings'));
    }

}
