<?php

namespace App\Http\Controllers\Guru;

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

class SiswaController extends Controller
{
    public function index(Request $request)
    {
        $query = Siswa::with('kelas');

        // SEARCH
        if ($request->has('search')) {
            $search = $request->search;
            $query->where('nama', 'like', "%$search%")
                  ->orWhere('nisn', 'like', "%$search%");
        }

        // FILTER BY KELAS
        if ($request->has('filter_kelas') && $request->filter_kelas != '') {
            $query->where('id_kelas', $request->filter_kelas);
        }

        // SHOW PER PAGE
        $perPage = $request->get('perPage', 10);

        $siswas = $query->paginate($perPage)->appends($request->all());

        // Ambil semua kelas untuk dropdown filter
        $kelass = Kelas::orderBy('tingkat')->orderBy('nama_kelas')->get();

        return view('guru.siswa.index', compact('siswas', 'kelass'));
    }
}
