<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Reservations;
use App\Status;
use App\Festive;
use App\Http\Requests\ReservationsRequest;
use Auth;
use Carbon\Carbon;
class CalendarController extends Controller
{
		public function __construct()
    {
        $this->middleware('auth');
    }

    public function index(Request $request)
    {

       if(Auth::user()->role->role_permission('view_reservation')){
				$reservation = Reservations::orderBy('date_start', 'id','desc')->with('boat')->get();

				echo('index CalendarController');
				return print_r($reservation);
				exit();

                $festivos = Festive::orderBy('created_at', 'desc')->get();
                $status = Status::get();
                $boatId = (isset($request->boat_id)) ? $request->boat_id : null;

                $eventos = [];

                foreach($reservation as $reserva)
                {

                     if($reserva->status_id == 1){
												$color = '#f39c12';
                 		 }
											elseif($reserva->status_id == 2) {
												$color = '#5cb85c';
                  	 }
											else{
												$color = '#dd4b39';
										 }

                    $date_start= Carbon::createFromFormat('d/m/Y',$reserva->date_start)->format('Y-m-d');
                    $date_end= Carbon::createFromFormat('d/m/Y',$reserva->date_end)->format('Y-m-d');

                    $start = date( "H:i:s", strtotime( $reserva->time ) );
                    $eventos[] = \Calendar::event(
                        $reserva->boat->name . ' - ' . $reserva->schedule->name,
                        true,
                        $reserva->date_start,
                        $reserva->date_end,
                        0,
                        [
                            'url' =>'reservations/'.$reserva->id.'/edit',
                            'color' => $color,
                             //rendering: 'background',
                        ]);


                }

                $festive = [];

                foreach($festivos as $festivo)
                {
                    $color = '#EF5773';
                    $date= Carbon::createFromFormat('d/m/Y',$festivo->date)->format('Y-m-d');

                    $festive[] = \Calendar::event(
                        'Festivo: '.$festivo->name,
                        true,
                        $festivo->date,
                        $festivo->date,
                        0,
                        [
                            'url' =>'',
                            'color' => $color,
                             //rendering: 'background',
                        ]);


                }

                $calendar = \Calendar::addEvents($eventos) //add an array with addEvents
                   /* ->addEvent($eloquentEvent, [ //set custom color fo this event
                        'color' => '#800',
                    ])->setOptions([ //set fullcalendar options
                        'firstDay' => 1
                    ])->setCallbacks([ //set fullcalendar callback options (will not be JSON encoded)
                        'viewRender' => 'function() {alert("Callbacks!");}'
                    ])*/;

                $calendar = \Calendar::addEvents($festive);

				return view('calendar.activities', compact('calendar','status','festive','request', 'reservation'));
			}else{
				abort(403);
			}
    }




}
