<?php

namespace App\Http\Controllers;

// use App\Http\Controllers\Controller;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Http\Response as res;
use App\MasterEvent;
use App\EventParticipant;
use App\EventAttendance;
use App\Users;
use Cookie;
use Mail;
use DB;

class HomeController extends Controller

{

  public function __construct()

   {

     //  $this->middleware('auth:api');

   }

   /**

    * Display a listing of the resource.

    *

    * @return \Illuminate\Http\Response

    */

   public function index()
   {

    if (Auth::guest())
            {

                 dd('not login');


            }else{

                 // dd('login');
                 $user = Auth::user();
                $uid = $user->id;
                $event = MasterEvent::where('fk_user',$uid)->get();
                return view('admin.show', compact('event'));


            }

    }   

    public function manageevent()
   {

    if (Auth::guest())
            {

                 dd('not login');


            }else{

                 // dd('login');
                 $user = Auth::user();
                $uid = $user->id;
                $event = MasterEvent::where('fk_user',$uid)->get();
                return view('admin.manage', compact('event'));


            }

    }   

    public function manageguest()
    {

    if (Auth::guest())
            {

                 dd('not login');


            }else{

                 // dd('login');
                $user = Auth::user();
                $uid = $user->id;
                $events = MasterEvent::where('fk_user',$uid)->first();
                $msg = Cookie::get('gms');
               
                $notice = '';
                if($msg == null){$notice = '';}else{$notice = $msg;}

                $event = EventParticipant::where('fk_master_event',$events->id)->get();

                return view('admin.guest', compact('event','notice'));


            }

    }   

    public function attendancelist()
    {

    if (Auth::guest())
            {

                 dd('not login');


            }else{

               
                $user = Auth::user();
                $uid = $user->id;
                $events = MasterEvent::where('fk_user',$uid)->first();
                $eventid = $events->id;
                $list = DB::SELECT("SELECT a.*,b.time,(select name from users where id = $uid) as 'by' FROM event_participant a,event_attendance b WHERE b.`fk_event_participant` = a.`id` and a.fk_master_event = $eventid");
                return view('admin.list', compact('events','list'));


            }

    }   

    public function attendance($id,$eventid)
    {

    
           $attendance = new EventAttendance;
           $attendance->fk_master_event = $eventid;
           $attendance->fk_event_participant = $id;
           $attendance->time = date('Y-m-d H:i:s');
           $attendance->save();

            $info = EventParticipant::where('id', $id)->where('fk_master_event',$eventid)->first();
            $event = MasterEvent::where('id', $eventid)->first();
            $slug = $event->event_slug;

            $message='Guest Attendance successfully taken';
            return view('admin.attendance.adminaction',compact('eventid','slug','info','message'));




    } 
    public function goodies($id,$eventid)
    {

    
            $info = EventParticipant::where('id', $id)->where('fk_master_event',$eventid)->first();
            $info->goodies_status = 1;
            $info->save();

            $event = MasterEvent::where('id', $eventid)->first();
            $slug = $event->event_slug;

            $message='Guest has recieved the Goodies ';
            return view('admin.attendance.adminaction',compact('eventid','slug','info','message'));




    } 
     public function registration($id,$eventid)
    {

    
            $info = EventParticipant::where('id', $id)->where('fk_master_event',$eventid)->first();
            $info->registration_status = 1;
            $info->save();

            $event = MasterEvent::where('id', $eventid)->first();
            $slug = $event->event_slug;

            $message='Guest has successfully registered for the event ';
            return view('admin.attendance.adminaction',compact('eventid','slug','info','message'));




    } 
     public function eventattendance($id,$eventid)
    {

    
            $info = EventParticipant::where('id', $id)->where('fk_master_event',$eventid)->first();
            $info->attendance_status = 1;
            $info->save();

            $event = MasterEvent::where('id', $eventid)->first();
            $slug = $event->event_slug;

            $message='Event attendance successfully saved';
            return view('admin.attendance.adminaction',compact('eventid','slug','info','message'));




    } 
     public function lunch($id,$eventid)
    {

    
            $info = EventParticipant::where('id', $id)->where('fk_master_event',$eventid)->first();
            $info->lunch_status = 1;
            $info->save();

            $event = MasterEvent::where('id', $eventid)->first();
            $slug = $event->event_slug;

            $message='Guest has checked for lunch ';
            return view('admin.attendance.adminaction',compact('eventid','slug','info','message'));




    } 
    

    public function checkUser(Request $request) {
         $result =0;
            if ($request->data) {
                $user = User::where('QRpassword',$request->data)->first();
                if ($user) {
                    Sentinel::authenticate($user);
                    $result =1;
                 }else{
                    $result =0;
                 }
                
            }
            
            return $result;
    }


    public function addguest(Request $request) {
       
        $user = Auth::user();
        $uid = $user->id;
        $events = MasterEvent::where('fk_user',$uid)->first();

        $check = EventParticipant::where('email',$request->email)->first();
        if($check)
        {
            return redirect::to('/guest')->withCookie(cookie('gms', 'Guest Email is already used in this event',0.05));
        }else
        {

            $InvCode = str_random(5);

            $guestadd = new EventParticipant;
            $guestadd->fk_master_event= $events->id;
            $guestadd->fullname=$request->name;
            $guestadd->email=$request->email;
            $guestadd->phone = $request->phone;
            $guestadd->address = $request->address;
            $guestadd->invitation_code = $InvCode;
            $guestadd->invitation_status = 1;
            $guestadd->acceptance_status = 0;
            $guestadd->goodies_status = 0;
            $guestadd->registration_status = 0;
            $guestadd->attendance_status = 0;
            $guestadd->lunch_status = 0;
            $guestadd->save();

            $uevent = $events->event_name;
            $uemail = $request->email;
            $uname = $request->name;

            Mail::send('admin.email.invite', array('name' => $uname,'slug' => $events->event_slug,'eventname' => $events->event_name,'uemail' => $uemail), function ($message) use ($uemail,$uevent){

                $message->from('myevent@gmail.com', 'MyEvent');

                $message->to($uemail)->subject('Invitation for : '.$uevent.'');

            });

            return redirect::to('/guest')->withCookie(cookie('gms', 'Successfully add a guest',0.05));

        }



    }



    public function editevent($id)
    {

    if (Auth::guest())
            {

                 dd('not login');


            }else{

                 // dd('login');
                $user = Auth::user();
                $uid = $user->id;
                $event = MasterEvent::where('id',$id)->get();
                // dd($event);

                return view('admin.detail', compact('event'));

                // return view('admin.guest', compact('event'));

                //  <div class="col-lg-4 col-md-6 col-sm-6">
                //     <div class="cards cards-stats">
                //         <div class="cards-header" data-background-color="orange">
                //             <i class="fa fa-percent"></i>
                //         </div>
                //         <div class="cards-content">
                //             <p class="category">Kempen Aktif</p>
                //             <h3 class="title">{{$camcount}}</h3>
                //         </div>
                //         <div class="cards-footer">
                //             <div class="stats">
                //                 <i class="material-icons text-danger">donut_small</i>
                //                 <a href="campaign">Lihat Tetapan Kempen</a>
                //             </div>
                //         </div>
                //     </div>
                // </div>


            }

    }   
} 

?>