<?php

namespace App\Controllers;

use App\Models\Absensi_model;


class Absensi extends BaseController
{
    public function __construct()
    {
        $this->Absensi_model = new Absensi_model();
    }
    private $Absensi_model;
    public function index()
    {
        // $ceklogin = cekLogin();
        // if ($ceklogin) return $ceklogin;
        // $user = getUserInfo();
        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Absensi'
        ];
        return view('Admin/Penilaian/absensi', $data);
    }

    public function getData()
    {
        $exec = $this->Absensi_model->get_all_Rombel($this->appSetting()->tahunpelajaran, cekRoleid());
        $data['data'] = $exec;
        return $this->response->setJSON($data);
    }
    public function getSiswa($idrombel)
    {
        $siswa = $this->Absensi_model->get_siswa($idrombel, $this->appSetting()->tahunpelajaran);

        return $this->response->setJSON($siswa);
    }
    public function simpanAbsensi()
    {
        // Periksa apakah permintaan adalah permintaan AJAX
        if ($this->request->isAJAX()) {
            // Ambil data yang dikirimkan dari JavaScript
            $data = $this->request->getPost('absensiData');


            // Validasi data yang diterima (optional)
            foreach ($data as $absensi) {
                // Lakukan update predikat dan deskripsi berdasarkan id_penilaian
                $id_siswa = $absensi['id_siswa'];
                $sakit = $absensi['sakit'];
                $ijin = $absensi['ijin'];
                $alfa = $absensi['alfa'];
                $id_absensi = $absensi['id_absensi'] !== null ? $absensi['id_absensi'] : 0;
                $cek = $this->Absensi_model->cek_where(['id' => $id_absensi]);
                // Lakukan update
                if (!$cek) {
                    $this->Absensi_model->absensi_add([
                        'id_siswa' => $id_siswa,
                        'sakit' => $sakit,
                        'ijin' => $ijin,
                        'alfa' => $alfa,
                        'tahunpelajaran' => $this->appSetting()->tahunpelajaran
                    ]);
                } else {
                    $this->Absensi_model->absensi_update(['id' => $id_absensi], [

                        'sakit' => $sakit,
                        'ijin' => $ijin,
                        'alfa' => $alfa,
                    ]);
                }
            }

            // Respon AJAX jika diperlukan
            return $this->response->setJSON(['status' => 'success', 'message' => 'Data berhasil diperbarui']);
        } else {
            // Respon jika bukan permintaan AJAX
            return $this->response->setStatusCode(403)->setBody('Forbidden');
        }
    }

    public function simpanCatatan()
    {
        // Periksa apakah permintaan adalah permintaan AJAX
        if ($this->request->isAJAX()) {
            // Ambil data yang dikirimkan dari JavaScript
            $data = $this->request->getPost('absensiData');


            // Validasi data yang diterima (optional)
            foreach ($data as $absensi) {
                // Lakukan update predikat dan deskripsi berdasarkan id_penilaian
                $id_siswa = $absensi['id_siswa'];
                $catatan = $absensi['catatan'];

                $id_absensi = $absensi['id_absensi'] !== null ? $absensi['id_absensi'] : 0;
                $cek = $this->Absensi_model->cek_where(['id' => $id_absensi]);
                // Lakukan update

                $this->Absensi_model->absensi_update(['id' => $id_absensi], [

                    'catatan' => $catatan,

                ]);
            }

            // Respon AJAX jika diperlukan
            return $this->response->setJSON(['status' => 'success', 'message' => 'Data berhasil diperbarui']);
        } else {
            // Respon jika bukan permintaan AJAX
            return $this->response->setStatusCode(403)->setBody('Forbidden');
        }
    }

    public function delete($id)
    {
        $user = $this->Absensi_model->find($id);

        if ($user) {
            $this->Absensi_model->delete($id);
            return $this->response->setJSON(['success' => true, 'message' => 'User deleted successfully']);
        } else {
            return $this->response->setStatusCode(404)->setJSON(['success' => false, 'message' => 'User not found']);
        }
    }
}
