<?php 

namespace App\Helper;
use App\Models\Mail\MailSettings;
use App\Helper\MailHelper as MailHelper;
use App\Models\Training\Schedule;
use App\Models\Training\ScheduleRoles;
use App\Models\Training\Participant;
use App\Models\User;

class RegisterInvitationHelper
{
	public static function send_invitation($schedule_id,$created_by)
    {
    	$department_id= "";
        $section_id = "";
        $send_invitation_flag = 0;
        $schedules = Schedule::where('id',$schedule_id)->first();
        if(isset($schedules))
        {
            $training_title = $schedules->training_title;
            $venue = $schedules->venue;
            $period_from = $schedules->period_from;
            $period_to = $schedules->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;
            $department_id = $schedules->department_id;
            $section_id = $schedules->section_id;
            $organisation_id = $schedules->organisation_id;
        }
        $reg_required_status = "";
        $schedule_roles = ScheduleRoles::where('schedule_id',$schedule_id)->with('roles','schedule')->get();
        foreach($schedule_roles as $schedule_role)
        {
            $role_id = $schedule_role->role_id;
            $role_name = $schedule_role->roles->name;
            if($organisation_id)
            {
                $user_roles = User::where('organisation_id',$organisation_id)->with('userrole')->whereHas('userrole',function($usr) use($role_id) { $usr->where('role_id',$role_id); })->get();
            }
            if($department_id)
            {
                $user_roles = User::where('organisation_id',$organisation_id)->where('department_id',$department_id)->with('userrole')->whereHas('userrole',function($usr) use($role_id) { $usr->where('role_id',$role_id); })->get();
            }
            if($section_id)
            {
                $user_roles = User::where('organisation_id',$organisation_id)->where('department_id',$department_id)->where('section_id',$section_id)->with('userrole')->whereHas('userrole',function($usr) use($role_id) { $usr->where('role_id',$role_id); })->get();
            }
            //dd($user_roles);
            foreach($user_roles as $user_role)
            {
                $user_id = $user_role->id;
                $user_name = $user_role->name;
                $emailid = $user_role->email;
                $reg_required_status = $schedule_role->schedule->registration_status;
                //if($reg_required_status==2)
                //{
                    $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,
                        ]);
                    }
                //}
                
                $subject="Training Invitation";
                $html="<div style='color: #343434; font-family: Open Sans; font-size: 20px; text-align: justify;'>Dear $user_name,<br><br> A Training Session for $training_title is Scheduled at $venue from $training_period. </div>";
                $text=$html;
                $mailobj = new MailHelper();
                $result=$mailobj->sendmailbymailgun($subject,$html,$text,$emailid);
                if($reg_required_status==2)
                {
                    $schedule_participant_count_new = Participant::where('schedule_id',$schedule_id)->where('role_id',$role_id)->where('user_id',$user_id)->where('mail_send_flag',1)->count();
                    if($schedule_participant_count_new==0)
                    {
                        $schedule_participantarr = Participant::where('schedule_id',$schedule_id)->where('role_id',$role_id)->where('user_id',$user_id)->first();
                        if(isset($schedule_participantarr))
                        {
                            $schedule_participantarr->mail_send_flag = 1;
                            $schedule_participantarr->updated_by = $created_by;
                            $schedule_participantarr->save();
                        }
                    }
                }
                $send_invitation_flag =1;
            }
        }
        $results = $send_invitation_flag;
        return $results;
    }

    public static function send_invitation_add($schedule_id,$user_id,$created_by)
    {
        $department_id= "";
        $section_id = "";
        $send_invitation_flag = 0;
        $schedules = Schedule::where('id',$schedule_id)->first();
        if(isset($schedules))
        {
            $training_title = $schedules->training_title;
            $venue = $schedules->venue;
            $period_from = $schedules->period_from;
            $period_to = $schedules->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;
            $department_id = $schedules->department_id;
            $section_id = $schedules->section_id;
            $organisation_id = $schedules->organisation_id;
        }
        $schedule_roles = ScheduleRoles::where('schedule_id',$schedule_id)->with('roles','schedule')->get();
        foreach($schedule_roles as $schedule_role)
        {
            $role_id = $schedule_role->role_id;
            $role_name = $schedule_role->roles->name;
            if($organisation_id)
            {
                $user_roles = User::where('id',$user_id)->where('organisation_id',$organisation_id)->with('userrole')->whereHas('userrole',function($usr) use($role_id) { $usr->where('role_id',$role_id); })->get();
            }
            if($organisation_id && $department_id)
            {
                $user_roles = User::where('id',$user_id)->where('organisation_id',$organisation_id)->where('department_id',$department_id)->with('userrole')->whereHas('userrole',function($usr) use($role_id) { $usr->where('role_id',$role_id); })->get();
            }
            if($organisation_id && $department_id && $section_id)
            {
                $user_roles = User::where('id',$user_id)->where('organisation_id',$organisation_id)->where('department_id',$department_id)->where('section_id',$section_id)->with('userrole')->whereHas('userrole',function($usr) use($role_id) { $usr->where('role_id',$role_id); })->get();
            }
            //dd($user_roles);
            foreach($user_roles as $user_role)
            {
                $user_id = $user_role->id;
                $user_name = $user_role->name;
                $emailid = $user_role->email;
                $reg_required_status = $schedule_role->schedule->registration_status;
                //if($reg_required_status==2)
                //{
                    $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,
                        ]);
                    }
                //}
                $subject="Training Invitation";
                $html="<div style='color: #343434; font-family: Open Sans; font-size: 20px; text-align: justify;'>Dear $user_name,<br><br> A Training Session for $training_title is Scheduled at $venue from $training_period. </div>";
                $text=$html;
                $mailobj = new MailHelper();
                $result=$mailobj->sendmailbymailgun($subject,$html,$text,$emailid);
                if($reg_required_status==2)
                {
                    $schedule_participant_count_new = Participant::where('schedule_id',$schedule_id)->where('role_id',$role_id)->where('user_id',$user_id)->count();
                    if($schedule_participant_count_new==0)
                    {
                        $schedule_participantarr = Participant::where('schedule_id',$schedule_id)->where('role_id',$role_id)->where('user_id',$user_id)->first();
                        if(isset($schedule_participantarr))
                        {
                            $schedule_participantarr->mail_send_flag = 1;
                            $schedule_participantarr->updated_by = $id;
                            $schedule_participantarr->save();
                        }
                    }
                }
                $send_invitation_flag =1;
            }
        }
        $results = $send_invitation_flag;
        return $results;
    }
}

?>