<?php

namespace App\Http\Controllers;

use App\Models\Event;
use Illuminate\Http\Request;
use Spatie\GoogleCalendar\Event as Events;
use Carbon\Carbon;

class FullCalenderController extends Controller
{
   /**

     * Write code on Method

     *

     * @return response()

     */

    public function index(Request $request)
    {
        if($request->ajax()) {
           $data = Event::whereDate('start', '>=', $request->start)
                       ->whereDate('end',   '<=', $request->end)
                       ->get(['id', 'title', 'start', 'end']);
           return response()->json($data);
        }
        return view('calendario.fullcalendar');
    }

 

    /**

     * Write code on Method

     *

     * @return response()

     */

    public function ajax(Request $request)
    {
        switch ($request->type) {
           case 'add':
              $event = Event::create([
                  'title' => $request->title,
                  'description' => $request->description,
                  'start' => $request->start,
                  'end' => $request->end,
              ]);
              return response()->json($event);
             break;

           case 'update':
              $event = Event::find($request->id)->update([
                  'title' => $request->title,
                  'description' => $request->description,
                  'start' => $request->start,
                  'end' => $request->end,
              ]);
              return response()->json($event);
             break;

           case 'delete':
              $event = Event::find($request->id)->delete();
              return response()->json($event);
             break; 

           default:
             # code...
             break;

        }

    }

    public function showDemo()
    {
      return view('calendario.fullcalendar');
    }

    public function saveEventToGoogle(Request $request){
      $event = new Events;
      $event->name = $request['title'];
      $event->description=$request['descrizione'];
      $tzStart = $request['DateStart'].'T'.$request['HourStart'].' +02:00';
      $tzEnd = $request['DateEnd'].'T'.$request['HourEnd'].' +02:00';
      $event->startDateTime = Carbon::parse($tzStart);
      $event->endDateTime = Carbon::parse($tzEnd);
      $event->save();
      return view('calendario.fullcalendar');
    }
}
