<?php

namespace App\Http\Controllers;

use App\Models\Duration;
use Illuminate\Http\Request;

use Illuminate\View\View;
use Illuminate\Routing\Controller;

class DurationController extends Controller
{
  public function __construct() {
    $this->middleware('can:duration.index')->only('index','show');
    $this->middleware('can:duration.create')->only('create','store');
    $this->middleware('can:duration.edit')->only('edit','update');
    $this->middleware('can:duration.destroy')->only('destroy');
    }

    public function index(): View
    {
        $durations = Duration::all();
        return view('duration.index',compact('durations'));
    }

    public function create(): View
    {
        return view('duration.create');
    }

    public function store(Request $request)
    {
        $duration =Duration::create($request->all());
        return redirect()->route('duration.edit',compact('duration'))->with('info','Se creó la duración correctamente');
    }

    public function edit(Duration $duration )
    {
        return view('duration.edit',compact('duration'));
    }

    public function update(Request $request,Duration $duration )
    {
        $duration ->update($request->all());
        return redirect()->route('duration.edit',compact('duration'))->with('info','Se modificó la duración correctamente');
    }

    public function show(Duration $duration )
    {
        return view('duration.show',compact('duration'));
    }

    public function destroy(Duration $duration )
    {
        $duration ->delete();
        session()->flash('swal',[
            'icon'=>'success',
            'title'=>'Eliminado!',
            'text'=>'Se ha eliminado el registro correctamente'
        ]);
        return redirect()->route('duration.index');
    }
}
