<?php

namespace App\Http\Controllers;

use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RoleController extends Controller
{
    public function index()
    {
        $data = Role::all();
    
        return view('admin.role')->with('data', $data);
    }

    public function store(Request $request)
    {
        $data = $request->except(['_method','_token']);
    
        Role::updateOrCreate($data);
          
        return back()
           ->with('success','Tambah Kategori Berhasil.');
    }

    public function edit($id)
    {
       $kategori = Role::find($id);
       
       return response()->json($kategori);
    }

    public function update(Request $request, $id)
    {
       $kategori = Role::find($id);
       $kategori->update(['nama'=>$request->nama]);
       
       return back()
            ->with('success','Edit Kategori Berhasil.');
    }

    public function destroy(Request $request, $id)
    {
        Role::find($id)->delete();
       
        return redirect()->back()
        ->with('success','Hapus Data Berhasil');;        
    }
}
