<?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');
            $notice = '';
            if($message == null){$notice = '';}else{$notice = $message;}

            if($session == null){

                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{

           return redirect::to('/');

        }

    // ,compact('trail','lkp_status')


        // if($slug == 'zurich'){

        //     if (Auth::guest())
        //     {
        //         return view('event.index');

        //     }else{

        //         return view('welcome');
        //     }
        // }else{

        //      return view('welcome');

        // }

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

    }

}