<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Mengajar;
use App\Models\Guru;
use App\Models\Mapel;
use App\Models\Kelas;
use Illuminate\Http\Request;

class MengajarController extends Controller
{
    public function index()
    {
        $mengajars = Mengajar::with('guru','mapel','kelas')->get();
        $gurus = Guru::all();
        $mapels = Mapel::all();
        $kelas = Kelas::all();

        return view('admin.mengajar.index', compact(
            'mengajars','gurus','mapels','kelas'
        ));
    }

    public function store(Request $request)
    {
        $request->validate([
            'id_guru' => 'required',
            'id_mapel' => 'required',
            'id_kelas' => 'required',
        ]);

        Mengajar::create([
            'id_guru' => $request->id_guru,
            'id_mapel' => $request->id_mapel,
            'id_kelas' => $request->id_kelas,
        ]);

        return redirect()->back()->with('success','Data mengajar berhasil ditambahkan');
    }

    public function update(Request $request, $id)
    {
        $mengajar = Mengajar::findOrFail($id);

        $mengajar->update([
            'id_guru' => $request->id_guru,
            'id_mapel' => $request->id_mapel,
            'id_kelas' => $request->id_kelas,
        ]);

        return redirect()->back()->with('success','Data mengajar berhasil diupdate');
    }

    public function destroy($id)
    {
        Mengajar::findOrFail($id)->delete();

        return redirect()->back()->with('success','Data mengajar berhasil dihapus');
    }
}
