<?php

namespace App\Http\Controllers\Instansi;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Jabatan;
use Illuminate\Support\Facades\Auth;

class JabatanController extends Controller
{
    public function index()
    {
        $jabatans = Jabatan::where('instansi_id', Auth::user()->instansi_id)->get();
        return view('instansi.jabatan.index', compact('jabatans'));
    }

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

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

        Jabatan::create([
            'instansi_id' => Auth::user()->instansi_id,
            'nama_jabatan' => $request->nama_jabatan
        ]);

        return redirect()->route('instansi.jabatan.index')
            ->with('success','Jabatan berhasil ditambahkan');
    }

    public function edit(Jabatan $jabatan)
    {
        return view('instansi.jabatan.edit', compact('jabatan'));
    }

    public function update(Request $request, Jabatan $jabatan)
    {
        $request->validate([
            'nama_jabatan' => 'required'
        ]);

        $jabatan->update([
            'nama_jabatan' => $request->nama_jabatan
        ]);

        return redirect()->route('instansi.jabatan.index')
            ->with('success','Jabatan berhasil diupdate');
    }

    public function destroy(Jabatan $jabatan)
    {
        $jabatan->delete();
        return redirect()->route('instansi.jabatan.index')
            ->with('success','Jabatan berhasil dihapus');
    }
}
