<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use App\MasterEvent;
use Cookie;
use App\EventParticipant;
use Illuminate\Support\Facades\Redirect;
use PDF;
use QrCode;

class EventPageController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
       // $this->middleware('auth');
    }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($slug)
    {
        // $todo = Auth::user()->todo()->get();
        // return response()->json(['status' => 'success','result' => $todo]);


        $event = MasterEvent::where('event_slug', $slug)->first();
        if($event)
        {
            $eventid = $event->id;
            $session = Cookie::get('invitation');
            $message = Cookie::get('message');
            $qrcode = Cookie::get('qrcode');
            $email = Cookie::get('email');
            $invcode = Cookie::get('invcode');
            $inv_status = Cookie::get('inv_status');
            $notice = '';

             // dd($inv_status);

            if($inv_status !== null){

                // dd('yesy');

                if($inv_status == 0)
                {
                     $info = EventParticipant::where('email', $email)->where('fk_master_event',$eventid)->first();

                     $name = $info->fullname;

                     return view('event.action',compact('eventid','notice','inv_status','email','invcode','name'));

                }else{

                    $notice = 'You Have Rejected this invitation';
                    return view('event.reject',compact('eventid','notice'));

                }



            }else{

                 // return redirect::to('/');
            if($message == null){$notice = '';}else{$notice = $message;}

            if($session == null){

                if($qrcode == 1){

                    return view('event.indexqr',compact('eventid','notice','email','invcode'));
                }else{

                    return view('event.index',compact('eventid','notice'));

                }

            }else{

                $info = EventParticipant::where('id', $session)->first();
                return redirect::to('/event/detail/'.$slug);
                // return view('detail.index',compact('eventid','info'));

            }

        }

            

            

        }else{

            if($slug == 'find')
            {

                // dd('finding');
                return view('find');

            }

           return redirect::to('/');

        }


    }


    public function action($slug,$email)
    {

        // dd($slug);

        $validate = MasterEvent::where('end_date','>',date('Y-m-d'))->where('event_slug',$slug)->first();
        if($validate)
        {
            $info = EventParticipant::where('email', $email)->where('fk_master_event',$validate->id)->first();
            if($info->acceptance_status == 1){

                   return redirect::to('page/detail/'.$slug.'/')->withCookie(cookie('email', $email,60))->withCookie(cookie('invitation', $info->id,1));


            }else if($info->acceptance_status == 0){

                return redirect::to('/event/'.$slug)->withCookie(cookie('inv_status', '0',0.2))->withCookie(cookie('email', $email,0.2))->withCookie(cookie('invitation', '1',0.2))->withCookie(cookie('invcode', $info->invitation_code,0.2));

            }else{

                 return redirect::to('/event/'.$slug)->withCookie(cookie('message', 'You Have Rejected The Invitation',0.2))->withCookie(cookie('inv_status', '2',0.2));
            }

        }else{

          return redirect::to('/event/'.$slug)->withCookie(cookie('message', 'The Event that you are invited has endded',60));
        }

    }

    public function qrscan($slug,$email,$invcode)
    {
        // dd('slug:'.$slug.',email:'.$email.',code:'.$invcode);
        // 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'));


        //     }
        
        $event = MasterEvent::where('event_slug', $slug)->first();
        if($event)
        {
            $eventid = $event->id;
            $session = Cookie::get('invitation');
            $message = Cookie::get('message');
            $notice = '';
            if($message == null){$notice = '';}else{$notice = $message;}
            if (Auth::guest())
            {
                if($session == null)
                {


                     $info = EventParticipant::where('email', $email)->where('fk_master_event',$eventid)->first();

                     if($info)
                     {
                            if($info->acceptance_status == 1)
                            {

                                return redirect::to('page/detail/'.$slug.'/')->withCookie(cookie('email', $email,60))->withCookie(cookie('invitation', $info->id,1));

                            }else if($info->acceptance_status == 0)
                            {

                                return redirect::to('/event/'.$slug)->withCookie(cookie('inv_status', '0',0.2))->withCookie(cookie('email', $email,0.2))->withCookie(cookie('invitation', '1',0.2))->withCookie(cookie('invcode', $info->invitation_code,0.2));

                            }else{

                                 return redirect::to('/event/'.$slug)->withCookie(cookie('message', 'You Have Rejected The Invitation',0.2))->withCookie(cookie('inv_status', '2',0.2));
                            }


                     }else{

                         return redirect::to('/event/'.$slug)->withCookie(cookie('message', 'You are not in the guess list',0.2));
                     }
                }else{

                    $info = EventParticipant::where('id', $session)->first();
                    return redirect::to('/event/detail/'.$slug);
                }
            }else
            {
                $user = Auth::user();
                $uid = $user->id;

                $event = MasterEvent::where('fk_user', $uid)->first();

                if($event)
                {

                    $info = EventParticipant::where('email', $email)->where('fk_master_event',$eventid)->first();

                    if($info)
                    {
                       $message='';
                       return view('detail.adminaction',compact('eventid','slug','info','message'));

                    }else{

                         $message = 'The guest that you scan is not in your event guest list';
                         return view('detail.noaction',compact('eventid','slug','message'));
                     }
                }else
                {
                      $message = 'You are not the admin for this guess event';
                      return view('detail.noaction',compact('eventid','slug','message'));

                }



            }

        
        }else{

           return redirect::to('/');

        }


    }

    public function detail($slug)
    {
        $session = Cookie::get('invitation');
        
        $event = MasterEvent::where('event_slug', $slug)->first();
        $eventid = $event->id;

        if($session == null){

               
                $notice = 'Your Session is expired';
                return redirect::to('/event/'.$slug)->withCookie(cookie('message', $notice,1));
                // return view('event.index',compact('eventid','notice'));

            }else{

                $info = EventParticipant::where('id', $session)->first();
                // return redirect::to('/event/'.$slug.'/detail');
                return view('detail.index',compact('eventid','info','slug'));

            }
        // dd($res2);

    }

     public function download($slug)
    {
        $session = Cookie::get('invitation');
        
        $event = MasterEvent::where('event_slug', $slug)->first();
        $eventid = $event->id;

        if($session == null){

               
                $notice = 'Your Session is expired';
                return redirect::to('/event/'.$slug)->withCookie(cookie('message', $notice,1));
                // return view('event.index',compact('eventid','notice'));

            }else{

                $info = EventParticipant::where('id', $session)->first();
                // return redirect::to('/event/'.$slug.'/detail');
                return view('detail.download',compact('eventid','info','slug'));

            }
        // dd($res2);

    }

    public function downloadpdf($slug)
    {
        $session = Cookie::get('invitation');
        
        $event = MasterEvent::where('event_slug', $slug)->first();
        $eventid = $event->id;

        if($session == null){

               
                $notice = 'Your Session is expired';
                return redirect::to('/event/'.$slug)->withCookie(cookie('message', $notice,1));

            }else{


                $info = EventParticipant::where('id', $session)->first();
                $create =QrCode::format('png')->size(275)->generate(url('event/qrscan/'.$slug.'/'.$info->email.'/'.$info->invitation_code),'qrcode/'.$session.'.png');

               
                $pdf = PDF::loadView('detail.pdfdownload', compact('info','slug','eventid'));
                return $pdf->stream( 'invitation-digital-pass.pdf');

                // return view('detail.download',compact('eventid','info','slug'));

            }
        // dd($res2);

    }


     public function findevent($slug)
    {
        
        $event = MasterEvent::where('event_slug', $slug)->first();
        if($event){

            return 1;
        }else{

            return 0;
        }

    }


}