<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\DatesFestius;
use App\Http\Requests;

class CalendariFestiusController extends Controller
{
    /**
     * CalendariFestiusController constructor.
     */
    public function __construct()
    {
        //midelware per demanar que s'estigui registrat
        $this->middleware('auth');
    }

    /** retorna a la vista amb dates llistades
     * @return vista
     */
    public function index()
    {
        $calendari = new DatesFestius();
        $dates = $calendari->llistarDates();
        return view('festius')->with('dates',$dates);

    }

    /** Inserta el periode de dates enviat des de la vista
     * @param Request $request
     * @return redireccio a la vista
     */
    public function insertData(Request $request)
    {
        $calendari = new DatesFestius();
        $dades = collect([
            'dataInici' => $request->input('start'),
            'dataFI' => $request->input('end'),
            'descripcio' => $request->input('descripcio')
        ]);
        $calendari->insertDatesFestius($dades);
        
        return redirect('/festius');
    }

    /**
     * @param Request $request
     * @return redireccio a la vista
     */
    public function borrarData(Request $request)
    {
        $calendari = new DatesFestius();
        $id = $request->input('id');
        $dates = $calendari->borrarDatesFestius($id);
        return redirect('/festius');
    }
}
