<?php

namespace App\Http\Controllers\tables;

use App\Http\Controllers\Controller;
use App\Models\Classification;
use App\Models\Siswa;
use Illuminate\Http\Request;

class ClassificationController extends Controller
{
    public function index()
    {
        $classification = Classification::join("siswa", "siswa.user_id", "=", "classification.user_id")->get();
        return view('content.classification.index', compact('classification'));
    }

    public function create()
    {
        $siswa = Siswa::all();
        return view('content.classification.create', compact('siswa'));
    }

    public function edit(Request $request, $classificationId)
    {
        $classification = Classification::join("siswa", "siswa.user_id", "=", "classification.user_id")->where("classification.id", "=", $classificationId)->first();
        return view('content.classification.edit', compact('classification'));
    }

    public function insert(Request $request)
    {
        Classification::create([
            "user_id" => $request->id,
            "nilai" => $request->nilai,
            "hasil_kelas" => $request->hasil_kelas,
        ]);

        return redirect()->route("classification")->with("success", "Data Berhasil Disimpan!");
    }
    public function update(Request $request, $classificationId)
    {
        $classification = Classification::find($classificationId);
        $classification->update([
            "nilai" => $request->nilai,
            "hasil_kelas" => $request->hasil_kelas,
        ]);

        return redirect()->route("classification")->with("success", "Data Berhasil Diperbarui!");
    }

    public function destroy(Request $request, $classificationId)
    {
        $id = $classificationId;
        $classification = Classification::find($id);

        $classification->delete();

        return redirect()->route("classification")->with("success", "Data Berhasil Dihapus!");
    }
}
