<?php

namespace App\Http\Controllers;

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

class MataPelajaranController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $mapel = MataPelajaran::latest()->get();

        return view(
            'mata-pelajaran.index',
            compact('mapel')
        );
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
         MataPelajaran::create([

            'kode_mapel' => $request->kode_mapel,
            'nama_mapel' => $request->nama_mapel

        ]);

        return back()->with(
            'success',
            'Mata Pelajaran berhasil ditambahkan'
        );
    
    }

    /**
     * Display the specified resource.
     */
    public function show(MataPelajaran $mataPelajaran)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        $mapel = MataPelajaran::findOrFail($id);

        return view(
            'mata-pelajaran.edit',
            compact('mapel')
        );
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
        $mapel = MataPelajaran::findOrFail($id);

        $mapel->update([

            'kode_mapel' => $request->kode_mapel,

            'nama_mapel' => $request->nama_mapel

        ]);

        return redirect('/mata-pelajaran')
            ->with(
                'success',
                'Mapel berhasil diupdate'
            );
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(MataPelajaran $mataPelajaran)
    {
        $mataPelajaran->delete();

        return redirect('/mata-pelajaran')
            ->with('success', 'Mapel berhasil dihapus');
    }
}
