<?php

namespace App\Http\Controllers;

use App\Models\Carrera;
use App\Models\Nivel;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

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

    public function index()
    {
        $carreras=Carrera::all();
        return view('carrera.index',compact('carreras'));
    }


    public function create()
    {
        $nivels=Nivel::where('status','=','1')->get();
        return view('carrera.create',compact('nivels'));
    }


    public function store(Request $request,Carrera $carrera)
    {
        $carrera= Carrera::create($request->all());
        return redirect()->route('carrera.edit',compact('carrera'))->with('info','Se creo la carrera correctamente');
    }


    public function show(Carrera $carrera)
    {
        return view('carrera.show',compact('carrera'));
    }


    public function edit(Carrera $carrera)
    {
        $nivels=Nivel::where('status','=','1')->get();
        return view('carrera.edit',compact('carrera','nivels'));
    }

    public function update(Request $request,Carrera $carrera)
    {
        $carrera->update($request->all());
        $nivels=Nivel::all();
        return redirect()->route('carrera.edit',compact('carrera','nivels'))->with('info','Se editó la carrera correctamente');
    }


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