<?php

namespace App\Http\Controllers;

use App\Http\Requests\JenisDokILRequest;
use App\Models\JenisDokIL;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;

class JenisDokILController extends Controller
{
    public function index()
    {
        try {
            $jenisdokil = JenisDokIL::latest()->get();
            return Inertia::render('admin/jenisdokil/index_jenisdokil', ['jenisdokil' => $jenisdokil]);
        } catch (\Exception $e) {
            Log::error('Error fetching Jenis Dokumen Ilmiah: ' . $e->getMessage());
            return back()->with('error', 'Failed to fetch Jenis Dokumen Ilmiah.');
        }
    }

    public function store(JenisDokILRequest $request)
    {
        try {
            JenisDokIL::create($request->validated());
            return redirect()->route('admin.jenisdokil.index')->with('success', 'Jenis Dokumen Ilmiah created successfully.');
        } catch (\Exception $e) {
            Log::error('Error creating Jenis Dokumen Ilmiah: ' . $e->getMessage());
            return back()->with('error', 'Failed to create Jenis Dokumen Ilmiah.');
        }
    }

    public function update(JenisDokILRequest $request, JenisDokIL $jenisdokil)
    {
        try {
            $jenisdokil->update($request->validated());
            return redirect()->route('admin.jenisdokil.index')->with('success', 'Jenis Dokumen Ilmiah updated successfully.');
        } catch (\Exception $e) {
            Log::error('Error updating Jenis Dokumen Ilmiah: ' . $e->getMessage());
            return back()->with('error', 'Failed to update Jenis Dokumen Ilmiah.');
        }
    }

    public function destroy(JenisDokIL $jenisdokil)
    {
        try {
            $jenisdokil->delete();
            return redirect()->route('admin.jenisdokil.index')->with('success', 'Jenis Dokumen Ilmiah deleted successfully.');
        } catch (\Exception $e) {
            Log::error('Error deleting Jenis Dokumen Ilmiah: ' . $e->getMessage());
            return back()->with('error', 'Failed to delete Jenis Dokumen Ilmiah.');
        }
    }
}
