<?php

namespace App\Http\Controllers;

use App\Models\Plantel;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class PlantelController extends Controller
{

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

    public function index()
    {
        $plantels = Plantel::all();
        return view('plantel.index',compact('plantels'));
    }

    public function create()
    {
        return view('plantel.create');
    }


    public function store(Request $request)
    {
        $request->validate([
            'name'=>'required'
        ]);

        $plantel = Plantel::create($request->all());

        return redirect()->route('plantel.edit',$plantel)->with('info','El plantel se creo correctamente');
    }


    public function show(Plantel $plantel)
    {
        return view('plantel.show',compact('plantel'));
    }


    public function edit(Plantel $plantel)
    {
        return view('plantel.edit',compact('plantel'));
    }


    public function update(Request $request, Plantel $plantel)
    {
        $plantel->update($request->all());

        return redirect()->route('plantel.edit',$plantel)->with('info','El plantel se actualizo correctamente');
    }

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