<?php

namespace App\Http\Controllers;

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

class AreaCodeController extends Controller
{

    public function __construct() {
    $this->middleware('can:areacode.index')->only('index','show');
    $this->middleware('can:areacode.create')->only('create','store');
    $this->middleware('can:areacode.edit')->only('edit','update');
    $this->middleware('can:areacode.destroy')->only('destroy');
    }
    public function index()
    {
        $areacodes = AreaCode::all();
        return view('areacode.index',compact('areacodes'));
    }

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

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

        $areacode = AreaCode::create($request->all());

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


    public function show(AreaCode $areacode)
    {
        return view('areacode.show',compact('areacode'));
    }


    public function edit(AreaCode $areacode)
    {
        return view('areacode.edit',compact('areacode'));
    }


    public function update(Request $request, AreaCode $areacode)
    {
        $request->validate([
            'name'=>'required'
        ]);

        $areacode->update($request->all());
        return redirect()->route('areacode.edit',$areacode)->with('info','El código se actualizo correctamente');
    }


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