<?php

namespace App\Http\Controllers;

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

class JurnalController extends Controller
{
    public function index()
    {
        $data = JurnalModel::allJurnal();
        return view('dashboard.jurnal.jurnal', ['data' => $data]);
    }

    public function store(Request $request)
    {
        $data = [
            'link_jurnal' => $request->input('link_jurnal'),
            'keterangan' => $request->input('keterangan'),
        ];

        $result = JurnalModel::insert($data);

        if ($result) {
            return redirect()->route('jurnal')->with('success', 'Jurnal berhasil ditambahkan');
        } else {
            return back()->with('error', 'Jurnal gagal ditambahkan');
        }
    }
    public function update(Request $request, $id)
    {
        $data = [
            'link_jurnal' => $request->input('link_jurnal'),
            'keterangan' => $request->input('keterangan'),
        ];

        $result = JurnalModel::where('id_jurnal', $id)->update($data);

        if ($result) {
            return redirect()->route('jurnal')->with('success', 'Jurnal berhasil diupdate');
        } else {
            return back()->with('error', 'Jurnal gagal diupdate');
        }
    }

    public function destroy(Request $request)
    {
        $id = $request->input('id_jurnal');
        $result = JurnalModel::where('id_jurnal', $id)->delete();

        if ($result) {
            return redirect()->route('jurnal')->with('success', 'Jurnal berhasil dihapus');
        } else {
            return back()->with('error', 'Jurnal gagal dihapus');
        }
    }
}
