<?php

namespace App\Controllers;

use App\Models\ModelKelas;
use App\Models\ModelPelanggaran;
use App\Models\ModelRekap;
use App\Models\ModelSiswa;
use App\Models\ModelGuru;

class InputPelanggaran extends BaseController
{

    public function index()
    {
        if (!session()->get('akses_input')) {
            return redirect()->to('/token');
        }
        $kelasModel = new ModelKelas();
        $pelanggaranModel = new ModelPelanggaran();
        $guruModel = new ModelGuru();

        $data = [
            'kelas'        => $kelasModel->findAll(),
            'pelanggaran'  => $pelanggaranModel->findAll(),
            'guru'         => $guruModel->orderBy('nama')->findAll()
        ];



        return view('InputPelanggaran', $data);
    }

    public function simpan()
    {
        if (!session()->get('akses_input')) {
            return redirect()->to('/token');
        }
        $rekap = new ModelRekap();

        $rekap->insert([
            'id_siswa'            => $this->request->getPost('id_siswa'),
            'id_pelanggaran'      => $this->request->getPost('id_pelanggaran'),
            'id_guru'             => $this->request->getPost('id_guru'),
            'tanggal'             => date('Y-m-d H:i:s'),
            'keterangan_tambahan' => $this->request->getPost('keterangan_tambahan')
        ]);

        return redirect()->to('/InputPelanggaran')
            ->with('success', 'Data Berhasil Tersimpan!');
    }

    public function getSiswa($id_kelas)
    {
        $model = new ModelSiswa();

        $siswa = $model
            ->where('id_kelas', $id_kelas)
            ->orderBy('nama', 'ASC')
            ->findAll();

        return $this->response->setJSON($siswa);
    }
}
