<?php

namespace App\Http\Controllers;

use App\Models\Jabatan;
use Illuminate\Http\Request;

class JabatanController extends Controller
{
    public function index()
    {
        //
        $jabatan = Jabatan::all();
        return view('jabatan', compact('jabatan'));
    }
    public function store(Request $request)
    {
        //
        $request->validate([
            'Kategori_bidang_kerja' => 'required|string|max:255',
            'nilai' => 'required|integer',
        ]);

        // Simpan ke database
        Jabatan::create([
            'Kategori_bidang_kerja' => $request->Kategori_bidang_kerja,
            'nilai' => $request->nilai,
        ]);

        return redirect()->back()->with('success', 'Jabatan berhasil disimpan!');
    }
    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id_kategori)
    {
        //
        $request->validate([
            'Kategori_bidang_kerja' => 'required|string|max:255',
            'nilai' => 'required|integer',
        ]);

        $jabatan = Jabatan::findOrFail($id_kategori);

        // Update data
        $jabatan->Kategori_bidang_kerja = $request->Kategori_bidang_kerja;
        $jabatan->nilai = $request->nilai;
        $jabatan->save();

        return redirect()->back()->with('success', 'Jabatan berhasil diperbarui.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id_kategori)
    {
        //
        $jabatan = Jabatan::findOrFail($id_kategori);
        $jabatan->delete();
    
        return response()->json(['success' => true]);
    }
}