<?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 App\Models\Training\Feedback;

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('material_type')->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('material_type')
                         ->offset($start)
                         ->limit($limit)
                         ->orderBy('id','desc')
                         ->get();
        }
        else 
        {
            $search = $request->input('search.value'); 
            $posts =  Schedule::with('material_type')
                            ->where('training_title', 'LIKE',"%{$search}%")
                            ->orWhere('venue', 'LIKE',"%{$search}%")
                            ->offset($start)
                            ->limit($limit)
                            ->orderBy($order,$dir)
                            ->get();
            $totalFiltered = Schedule::with('material_type')
                            ->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;
                $user_id = Auth::user()->id;
                if($registration_status==1)
                {
                    $schedule_participant_count = Participant::where('schedule_id',$schedule_id)->where('user_id',$user_id)->count();
                    if($schedule_participant_count!=0)
                    {
                        $reg_url = "&nbsp;&nbsp;<label style='color: red;'>Registered</label>";
                    }
                    else
                    {
                        $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 register_training($schedule_id)
    {
        $user_id = Auth::user()->id;
        $user_details = User::where('id',$user_id)->first();
        $schedules = Schedule::where('id',$schedule_id)->with('organisation','department','section','material_type','schedule_role','schedule_participants')->first();
        $role_id_arr = [];
        $schedule_roles = ScheduleRoles::where('schedule_id',$schedule_id)->get();
        foreach ($schedule_roles as $schedule_role) 
        {
           $role_id_arr[] = $schedule_role->role_id;
        }
        $role_name = [];
        foreach($role_id_arr as $role_id)
        {
            $user_roles = UserRoles::where('user_id',$user_id)->where('role_id',$role_id)->with('role')->first();
            if(isset($user_roles))
            {
                $role_name[] = $user_roles->role->name;
            }    
        }       
        //dd($role_name);
        return view('Trainee/register_training', compact('schedules','user_details','role_name'));
    }
    public function register_training_post (Request $request)
    {
        $user_id =$request->user_id;
        $schedule_id = $request->schedule_id;
        $role_id_arr = [];
        $schedule_roles = ScheduleRoles::where('schedule_id',$schedule_id)->get();
        foreach ($schedule_roles as $schedule_role) 
        {
           $role_id_arr[] = $schedule_role->role_id;
        }
        foreach($role_id_arr as $role_id)
        {
            $user_roles_count = UserRoles::where('user_id',$user_id)->where('role_id',$role_id)->count();
            if($user_roles_count!=0)
            {
                $created_by = Auth::user()->id;
                $schedule_participant_count = Participant::where('schedule_id',$schedule_id)->where('role_id',$role_id)->where('user_id',$user_id)->count();
                if($schedule_participant_count==0)
                {
                    $insert_schedule_participant_id = Participant::create([
                        'schedule_id' => $schedule_id,
                        'role_id' => $role_id,
                        'user_id' => $user_id,
                        'created_by' => $created_by,
                    ]);
                }
            }
        }
        return redirect('active_trainings');
    }
    /////training session//////////////////////////////////
    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')->first();
        if(isset($schedule_trainings))
        {
            $schedule_id = $schedule_trainings->schedule_id;
        }
        $topic_schedule_date_arr = [];
        $content_name = [];
        $topic_name =[];
        $topic_schedules = ScheduleTopics::where('schedule_id',$schedule_id)->select('topic_schedule_date')->groupBy('topic_schedule_date')->get();
        foreach ($topic_schedules as $topic_schedule) 
        {
           $topic_schedule_date_arr[] = $topic_schedule->topic_schedule_date;
        }
        $schedule_topics = ScheduleTopics::where('schedule_id',$schedule_id)->with('topic')->orderBy('topic_schedule_date','asc')->get();
        foreach($topic_schedule_date_arr as $topic_schedule_dates)
        {
            echo "topic_schedule_dates=$topic_schedule_dates..<br>";
            $n=0;
            $schedule_topics = ScheduleTopics::where('schedule_id',$schedule_id)->where('topic_schedule_date',$topic_schedule_dates)->with('topic')->orderBy('topic_schedule_date','asc')->get();
            foreach($schedule_topics as $schedule_topic)
            {
                $n++;
                $topic_id = $schedule_topic->topic_id;
                $topic_id_arr[$topic_schedule_dates][] = $topic_id;
                echo "topic_id=$topic_id....topicname=";
                echo $topic_name[$topic_schedule_dates][$topic_id] = $schedule_topic->topic->topic_name;
                echo "<br>";
                foreach($schedule_topic->topic->content_assignment as $topic_content_assignment)
                {
                    $content_id = $topic_content_assignment->content_id;
                    $content_id_arr[$topic_schedule_dates][$topic_id][] = $content_id;
                    echo "content_id=$content_id...content name = ";
                    echo $content_name[$topic_schedule_dates][$topic_id][$content_id] = $topic_content_assignment->contentname->content_subject;
                    echo "<br>";
                }
                
            }
        }
        //dd($content_id_arr);
        return view('Trainee/view_training_session_details', compact('schedule_trainings','schedule_topics','topic_schedule_date_arr'));
    }

    //////training attendance////////////////
    public function training_attendance()
    {
        return view('Trainee/training_attendance');
    }
    public function get_training_attendance_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_attendance_details').'/'.$participant_id.'" title="View Attendance Details" class="btn btn-xs btn-primary">Training Attendance 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_attendance_details($participant_id)
    {
        $schedule_trainings = Participant::where('user_id',$participant_id)->with('schedule')->first();
        if(isset($schedule_trainings))
        {
            $schedule_id = $schedule_trainings->schedule_id;
        }
        $topic_schedule_date_arr = [];
        $topic_schedules = ScheduleTopics::where('schedule_id',$schedule_id)->select('topic_schedule_date')->groupBy('topic_schedule_date')->get();
        foreach ($topic_schedules as $topic_schedule) 
        {
           $topic_schedule_date_arr[] = $topic_schedule->topic_schedule_date;
        }
        $attendance_status =[];
        $schedule_topic_name_arr = [];
        foreach($topic_schedule_date_arr as $topic_schedule_dates)
        {
            $schedule_topic_name ="";
            $topic_name = "";
            $schedule_topics_arr = ScheduleTopics::where('schedule_id',$schedule_id)->where('topic_schedule_date',$topic_schedule_dates)->with('topic')->orderBy('topic_schedule_date','asc')->get();
            foreach($schedule_topics_arr as $schedule_topic)
            {
                $topic_name = $schedule_topic->topic->topic_name;
                if($schedule_topic_name)
                {
                    $schedule_topic_name = $schedule_topic_name.", ".$topic_name;
                }
                else
                {
                    $schedule_topic_name = $topic_name;
                }
            }
            $schedule_topic_name_arr[$topic_schedule_dates] = $schedule_topic_name;
            $user_id = Auth::user()->id;
            $schedule_attendances = Attendance::where('schedule_id',$schedule_id)->where('user_id',$user_id)->where('attendance_date',$topic_schedule_dates)->get();
            if(isset($schedule_attendances))
            {
                foreach($schedule_attendances as $schedule_attendance)
                {
                    $attendance_status[$topic_schedule_dates] = $schedule_attendance->attendance_status;
                }
            }
        }
        
        //dd($attendance_status);
        return view('Trainee/view_training_attendance_details', compact('schedule_trainings','schedule_topic_name_arr','attendance_status','topic_schedule_date_arr'));
    }

    //////training certificate////////////////
    public function training_certificates()
    {
        return view('Trainee/training_certificates');
    }
    public function get_training_certificate_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 = "";
            $training_certificates_status ="";
            $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;
                if(isset($post->schedule->training_certificates))
                    $training_certificates_status = $post->schedule->training_certificates;
                $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;
                $id = $schedule_id."~".$participant_id;
                if($training_certificates_status==1)
                {
                    $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_certificate').'/'.$id.'" title="View Attendance Details" class="btn btn-xs btn-primary">View Ceritificate</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); 
    }
    //////cerificate preview/////////////////////////////////////
    public function view_training_certificate($schedule_participant_id)
    {
        $schedule_participants = explode("~", $schedule_participant_id);
        $schedule_id = $schedule_participants[0];
        $participant_id = $schedule_participants[1];
        $certificate = Certificate::where('schedule_id',$schedule_id)->where('user_id',$participant_id)->with('users','schedule')->where('certificate_status',1)->first();
        return view('Trainee/certificatepreview', compact('schedule_id','certificate'));
        /*$view1 = \View::make('certificatepreview', compact('schedule_id','certificate'));
        $html1 = $view1->render();
        $pdf = new PDF();
        $pdf::SetTitle('Certificate');
        $pdf::AddPage('P', 'A4');
        $pdf::writeHTML($html1, true, false, true, false, '');
        $pdf::Output(uniqid().'Certificate.pdf');
        $pdf::reset();*/
    }

    //////training training_question_answer////////////////
    public function training_question_answer()
    {
        return view('Trainee/training_question_answer');
    }
    public function get_training_qa_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 = "";
            $question_answer_status ="";
            $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;
                if(isset($post->schedule->question_answer_status))
                    $question_answer_status = $post->schedule->question_answer_status;
                $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;
                $id = $schedule_id."~".$participant_id;
                if($question_answer_status==1)
                {
                    $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_qa_details').'/'.$participant_id.'" title="View Schedule 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_qa_details($participant_id)
    {
        $schedule_trainings = Participant::where('user_id',$participant_id)->with('schedule')->first();
        if(isset($schedule_trainings))
        {
            $schedule_id = $schedule_trainings->schedule_id;
        }
        $schedule_topics = ScheduleTopics::where('schedule_id',$schedule_id)->with('topic')->orderBy('topic_schedule_date','asc')->get();
        $user_id = Auth::user()->id;
        $attendance_status =[];
        
        //dd($attendance_status);
        return view('Trainee/view_training_qa_details', compact('schedule_trainings','schedule_topics'));
    }

    ////////////////feedback////////////////
    public function feedback()
    {
        return view('Trainee/feedback');
    }
    public function get_training_feedback_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 = "";
            $review_status ="";
            $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;
                if(isset($post->schedule->review_status))
                    $review_status = $post->schedule->review_status;
                $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;
                $id = $schedule_id."~".$participant_id;
                if($review_status==1)
                {
                    $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('feedback_create').'/'.$id.'" title="Create Feedback" class="btn btn-xs btn-primary">Create Feedback</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 feedback_create($schedule_participant_id)
    {
        $schedule_participants = explode("~", $schedule_participant_id);
        $schedule_id = $schedule_participants[0];
        $participant_id = $schedule_participants[1];
        $schedule_trainings = Participant::where('user_id',$participant_id)->with('schedule')->first();
        if(isset($schedule_trainings))
        {
            $schedule_id = $schedule_trainings->schedule_id;
        }
        $schedule_topics = ScheduleTopics::where('schedule_id',$schedule_id)->with('topic')->orderBy('topic_schedule_date','asc')->get();
        $user_id = Auth::user()->id;
        $attendance_status =[];
        
        //dd($attendance_status);
        return view('Trainee/feedback_create', compact('schedule_trainings','schedule_topics','participant_id'));
    }
    public function feedback_create_post(Request $request)
    {
        $insert_feedback="";
        $schedule_id = $request->schedule_id;
        $participant_id = $request->participant_id;
        $id = Auth::user()->id;
        $feedback = $request->feedback;
        $feedback_count = Feedback::where('schedule_id',$schedule_id)->where('user_id',$participant_id)->where('feedback',$feedback)->count();
        if($feedback_count==0)
        {
            $insert_feedback = Feedback::create([
                'schedule_id' => $schedule_id,
                'user_id' => $participant_id,
                'feedback' => $feedback,
            ]);
        }
        
        
        if($insert_feedback)  
            notify()->success('Feedback has been posted.');
        else
            smilify('Error','');
        return redirect()->back();
    }

}
