<?php

namespace App\Http\Controllers;

use App\Models\Campan;
use Illuminate\View\View;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

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

    public function index(): View
    {
        $campans = Campan::all();
        return view('campan.index',compact('campans'));
    }

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

    public function store(Request $request)
    {
        $campan = Campan::create($request->all());
        return redirect()->route('campan.edit',compact('campan'))->with('info','Se creó la campaña correctamente');
    }

    public function edit(Campan $campan)
    {
        return view('campan.edit',compact('campan'));
    }

    public function update(Request $request,Campan $campan)
    {
        $campan ->update($request->all());
        return redirect()->route('campan.edit',compact('campan'))->with('info','Se modificó la campaña correctamente');
    }

    public function show(Campan $campan)
    {
        return view('campan.show',compact('campan'));
    }

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