<?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\Users;
use Cookie;
use Mail;
use PDF;
use QrCode;

class InvitationController extends Controller

{

  public function __construct()

   {

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

   }

   /**

    * Display a listing of the resource.

    *

    * @return \Illuminate\Http\Response

    */

   public function postAccept(Request $request)
   {



      $validate = MasterEvent::where('end_date','>',date('Y-m-d'))->where('id',$request->evenid)->first();
      if($validate)
      {

          $data = EventParticipant::where('invitation_code', $request->name)->where('fk_master_event',$request->evenid)->where('email',$request->email)->first();

          if($data)
          {

              if($data->acceptance_status == 1)
              {            
              
                    return redirect::to('page/detail/'.$validate->event_slug)->withCookie(cookie('invitation', $data->id,5));

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

                    if($request->actionid == 1)
                    {

                        $data->acceptance_status = 1;
                        $data->save();

                          $uevent = $validate->event_name;
                          $uemail = $request->email;
                          $uname = $data->fullname;
                          $uids = $data->id;
                           $create =QrCode::format('png')->size(275)->generate(url('event/qrscan/'.$validate->event_slug.'/'.$uemail.'/'.$request->name),'qrcode/'.$data->id.'.png');

                          Mail::send('admin.email.accept', array('name' => $uname,'slug' => $validate->event_slug,'eventname' => $uevent,'uemail' => $uemail,'invcode' =>$data->invitation_code,'uids' => $uids), function ($message) use ($uemail,$uevent){

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

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

                          });


                        return redirect::to('download/detail/'.$validate->event_slug)->withCookie(cookie('invitation', $data->id,5));


                    }else if($request->actionid == 2)
                    {

                           $data->acceptance_status = 2;
                        $data->save();
                          return redirect::to('/event/'.$validate->event_slug)->withCookie(cookie('message', 'You Have Rejected The Invitation',0.1))->withCookie(cookie('inv_status', '2',0.1));
                    }else{

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

                  }

              }else
              {

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

            

          }else
          {

             $qrcode = Cookie::get('qrcode');
              if($qrcode == 1){

                    return redirect::to('/event/'.$validate->event_slug)->withCookie(cookie('message', 'Invalid QRcode or your invatiation has canceled',1));
                }else{

                  return redirect::to('/event/'.$validate->event_slug)->withCookie(cookie('message', 'You have entered incorrect email/invitation code or you are not invited to this event',1));
                }


            // dd('you hav entered incorrect email/invitation codes or you are not invited to this event');
             

          }
      }else{

          // dd('The Event that you are invited has endded');
          $slug = MasterEvent::where('id',$request->evenid)->first();
          return redirect::to('/event/'.$slug->event_slug)->withCookie(cookie('message', 'The Event that you are invited has endded',1));
      }



   }

   public function index(Request $request)
   {

       $this->validate($request, [

       'email' => 'required',

       'password' => 'required'

        ]);

       dd($request);

      $user = Users::where('email', $request->input('email'))->first();
      if($user){
      // dd(Hash::make($user->password));

         if(Hash::check($request->input('password'), $user->password)){

                if($user->api_key){

                     return response()->json(['status' => 'success','api_key' => $user->api_key]);

                }else{

                  $apikey = base64_encode(str_random(40));
                  Users::where('email', $request->input('email'))->update(['api_key' => "$apikey"]);
                  return response()->json(['status' => 'success','api_key' => $user->api_key]);

                }

                      

          }else{

              return response()->json(['status' => 'fail'],401);

          }
        }else{

            return response()->json(['status' => 'fail','message' => 'Email is not registered']);
        }

   }

}    

?>