<?php

namespace App\Http\Controllers;
use App\Gira;
use App\Region;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class GiraController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $giras=Gira::with('region')
                ->get();
        return view('giras.lista',compact('giras'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $regiones=Region::all();
        return view('giras.nuevo',compact('regiones'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $where = [
            'descripcion_gira' => $request->descripcion,
            'id_region' => $request->id_region
        ];
        $verificarRepetido=Gira::where($where)->first();
        if($verificarRepetido){
            $arrResponse = [
                'message' => 'La Gira ya existe'
            ];
            return redirect()->route('gira.index')->with($arrResponse);
            dd();
        }
        $gira = new Gira();
        $gira->descripcion_gira = $request->descripcion;
        $gira->id_region = $request->id_region;
        $gira->id_user = Auth::id();
        $gira->save();
        $arrResponse = [
            'message' => 'Gira guardada correctamente'
        ];
        return redirect()->route('gira.index')->with($arrResponse);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Gira $gira)
    {
        //
        $regiones=Region::all();
        return view('giras.editar',compact('regiones','gira'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Gira $gira)
    {
        //
        $gira->descripcion_gira = $request->descripcion;
        $gira->id_region = $request->id_region;
        $gira->id_user = Auth::id();
        $gira->update();
        $arrResponse = [
            'message' => 'Gira actualizada correctamente'
        ];
        return redirect()->route('gira.index')->with($arrResponse);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        Gira::destroy($id);
        $arrResponse = [
            'message' => 'Gira eliminada correctamente'
        ];
        return redirect()->route('gira.index')->with($arrResponse);
    }
}
