<?php

namespace App\Http\Controllers;

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

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

        $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($session == null){

                return redirect::to('/event/'.$slug)->withCookie(cookie('qrcode', 1,1))->withCookie(cookie('email', $email,1))->withCookie(cookie('invcode', $invcode,1));

                // return view('event.indexqr',compact('eventid','notice','email','invcode'));

            }else{

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

            }

            

            

        }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'));

            }
        // dd($res2);

    }

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

            return 1;
        }else{

            return 0;
        }

    }


}