<?php

namespace App\Http\Controllers;

use App\Models\Classes;
use App\Models\AttendanceHistoryDaily;
use Illuminate\Http\Request;

class ClassController extends Controller
{
    public function harian()
    {
        $classes = Classes::all();
        return view('pages.kelas.kelas_absensi', compact('classes'));
    }


    public function mapel()
    {
        $classes = Classes::all();
        return view('pages.kelas.kelas_mapel', compact('classes'));
    }

    public function kelas_laporkan()
    {
        $classes = Classes::all();
        return view('pages.kelas.kelas_laporkan', compact('classes'));
    }

    // Proses tambah kelas
    public function store(Request $request)
    {
        $request->validate([
            'name'  => 'required|string|max:255',
            'major' => 'required|string|max:255',
            'grade' => 'required|integer|in:10,11,12',
            'code'  => 'required|string|max:50',
        ], [
            'grade.required' => 'Tingkat kelas harus diisi.',
            'grade.integer' => 'Tingkat kelas harus berupa angka.',
            'grade.in' => 'Tingkat kelas hanya boleh 10, 11, atau 12.',
        ]);

        Classes::create([
            'name'  => $request->name,
            'major' => $request->major,
            'grade' => $request->grade,
            'code'  => $request->code,
        ]);

        return redirect()->route('kelas.absensi')->with('success', 'Kelas berhasil ditambahkan.');
        return redirect()->route('kelas.mapel')->with('success', 'Kelas berhasil ditambahkan.');
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'name'  => 'required|string|max:255',
            'major' => 'required|string|max:255',
            'grade' => 'required|integer|in:10,11,12',
            'code'  => 'required|string|max:50',
        ], [
            'grade.required' => 'Tingkat kelas harus diisi.',
            'grade.integer' => 'Tingkat kelas harus berupa angka.',
            'grade.in' => 'Tingkat kelas hanya boleh 10, 11, atau 12.',
        ]);

        $kelas = Classes::findOrFail($id);
        $kelas->update([
            'name'  => $request->name,
            'major' => $request->major,
            'grade' => $request->grade,
            'code'  => $request->code,
        ]);

        return redirect()->back()->with('success', 'Kelas berhasil diperbarui.');
    }

    public function destroy($id)
    {
        $kelas = Classes::findOrFail($id);
        $kelas->delete();

        return redirect()->route('kelas.absensi')->with('success', 'Kelas berhasil dihapus.');
        return redirect()->route('kelas.mapel')->with('success', 'Kelas berhasil dihapus.');
    }
}
