<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\StudentScore;
use Illuminate\Http\Request;

class StudentScoreController extends Controller
{
    public function index(Request $request)
    {
        $search = $request->input('search');

        $scores = StudentScore::when($search, function ($query, $search) {
            return $query->where('name', 'like', '%' . $search . '%')
                         ->orWhere('class', 'like', '%' . $search . '%');
        })
        ->orderBy('score', 'desc')
        ->orderBy('updated_at', 'asc')
        ->paginate(15);

        return view('admin.student-scores.index', compact('scores', 'search'));
    }

    public function destroy(StudentScore $studentScore)
    {
        $studentScore->delete();
        return redirect()->route('admin.student-scores.index')->with('success', 'Skor siswa berhasil dihapus.');
    }
}
