<?php

namespace App\Http\Controllers;

use App\Models\JenisSanksi;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;

class JenisSanksiController extends Controller
{
    public function index()
    {
        try {
            $jenissanksi = JenisSanksi::latest()->get();
            return Inertia::render('admin/jenissanksi/index_jenis_sanksi', ['jenissanksi' => $jenissanksi]);
        } catch (\Exception $e) {
            Log::error('Error fetching Jenis Sanksi: ' . $e->getMessage());
            return back()->with('error', 'Something went wrong.');
        }
    }

    public function store(Request $request)
    {
        try {
            JenisSanksi::create($request->all());
            return redirect()->route('admin.jenissanksi.index')->with('success', 'Jenis Sanksi berhasil dibuat.');
        } catch (\Exception $e) {
            Log::error('Error creating Jenis Sanksi: ' . $e->getMessage());
            return back()->with('error', 'Something went wrong.');
        }
    }

    public function update(Request $request, JenisSanksi $jenissanksi)
    {
        try {
            $jenissanksi->update($request->all());
            return redirect()->route('admin.jenissanksi.index')->with('success', 'Jenis Sanksi berhasil diperbarui.');
        } catch (\Exception $e) {
            Log::error('Error updating Jenis Sanksi: ' . $e->getMessage());
            return back()->with('error', 'Something went wrong.');
        }
    }

    public function destroy(JenisSanksi $jenissanksi)
    {
        try {
            $jenissanksi->delete();
            return redirect()->route('admin.jenissanksi.index')->with('success', 'Jenis Sanksi berhasil dihapus.');
        } catch (\Exception $e) {
            Log::error('Error deleting Jenis Sanksi: ' . $e->getMessage());
            return back()->with('error', 'Something went wrong.');
        }
    }
}
