<?php

namespace App\Controllers;

use App\Models\Penilaianeskul_model;


class PenilaianEskul extends BaseController
{
    public function __construct()
    {
        $this->Penilaianeskul_model = new Penilaianeskul_model();
    }
    private $Penilaianeskul_model;
    public function index()
    {
        // $ceklogin = cekLogin();
        // if ($ceklogin) return $ceklogin;
        // $user = getUserInfo();
        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Nilai Eskul'
        ];
        return view('Admin/Penilaian/nilaieskul', $data);
    }
    public function getPenilaian($idpembelajaran)
    {
        $exec = $this->Penilaianeskul_model->get_where_penilaian(['id_pembelajaran' => $idpembelajaran]);

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

    public function savePenilaian()
    {

        $request = $this->request;
        $nilaiArray = $request->getPost('nilai');
        $tujuanArray = $request->getPost('tujuan');
        $idpembelajaran = $request->getPost('idpembelajaran');
        // Lakukan validasi atau proses lain sesuai kebutuhan
        // Simpan data nilai dan switch ke database menggunakan model
        foreach ($nilaiArray as $idSiswa => $nilai) {
            $tujuanTercapai = isset($tujuanArray[$idSiswa]['tercapai']) ? implode(',', $tujuanArray[$idSiswa]['tercapai']) : '';
            $tujuanTidakTercapai = isset($tujuanArray[$idSiswa]['tidak_tercapai']) ? implode(',', $tujuanArray[$idSiswa]['tidak_tercapai']) : '';
            $where = ['id_siswa' => $idSiswa, 'id_pembelajaran' => $idpembelajaran];
            $ceksiswa = $this->Penilaianeskul_model->get_where_penilaian($where);
            if (!$ceksiswa) {
                $this->Penilaianeskul_model->penilaian_add([
                    'id_pembelajaran' => $idpembelajaran,
                    'id_siswa' => $idSiswa,
                    'nilai_akhir' => $nilai,
                    'tujuan_tercapai' => $tujuanTercapai,
                    'tujuan_belum' => $tujuanTidakTercapai
                ]);
            } else {
                $this->Penilaianeskul_model->penilaian_update($where, ['nilai_akhir' => $nilai, 'tujuan_tercapai' => $tujuanTercapai, 'tujuan_belum' => $tujuanTidakTercapai]);
            }
        }

        // Kirim respons ke client
        return $this->response->setJSON(['status' => 'success', 'message' => 'Data berhasil disimpan']);
    }

    public function saveCapaian()
    {

        $idpembelajaran = $this->request->getPost('idpembelajaran');
        $dataToUpdate = $this->request->getPost('dataToUpdate');

        foreach ($dataToUpdate as $data) {
            $id_siswa = $data['id_siswa'];
            $deskripsi_tercapai = $data['deskripsi_tercapai'];
            $deskripsi_belum = $data['deskripsi_belum'];

            // Update deskripsi tercapai dan belum tercapai dalam database
            $this->Penilaianeskul_model->penilaian_update(['id_siswa' => $id_siswa, 'id_pembelajaran' => $idpembelajaran], ['deskripsi_tercapai' => $deskripsi_tercapai, 'deskripsi_belum' => $deskripsi_belum]);
        }


        // Kirim respons ke client
        return $this->response->setJSON(['status' => 'success', 'message' => 'Data berhasil disimpan']);
    }

    public function addTujuan()
    {
        // Tangkap data dari form
        $tujuan = $this->request->getPost('tujuan');
        $guru = session()->get('user_id');
        $idmapel = $this->request->getPost('idmapel');
        $idrombel = $this->request->getPost('idrombel');
        $status = $this->request->getPost('status');
        $idpembelajaran = $this->request->getPost('idpembelajaran');
        // Validasi data jika diperlukan
        $id = $this->request->getPost('id');
        $result = [];
        for ($i = 0; $i < count($tujuan); $i++) {
            $data = [
                'id_pembelajaran' => $idpembelajaran,
                'tujuan' => $tujuan[$i],
                'id_guru' => $guru,
                'id_rombel' => $idrombel,
                'id_matapelajaran' => $idmapel,
                'status' => $status[$i]
            ];
            $dataupdate = [
                'tujuan' => $tujuan[$i],
                'status' => $status[$i]
            ];
            $where = ['id' => $id[$i]];
            if ($id[$i]) {
                $cektujuan = $this->Penilaianeskul_model->cek_where($where);
                if (!$cektujuan) {
                    $this->Penilaianeskul_model->tujuan_add($data);
                    $result[] = "Data berhasil ditambahkan";
                } else {
                    $this->Penilaianeskul_model->tujuan_update($where, $dataupdate);
                    $result[] = "Data berhasil diperbarui";
                }
            } else {
                $this->Penilaianeskul_model->tujuan_add($data);
                $result[] = "Data berhasil ditambahkan";
            }
        }

        // Kirim respons
        return $this->response->setJSON(['success' => true, 'message' => 'Berhasil disimpan']);
    }
    public function getPembelajaran()
    {
        $exec = $this->Penilaianeskul_model->get_all_pembelajaran();
        $data['data'] = $exec;
        return $this->response->setJSON($data);
    }
    public function getSiswa($rombel, $idp)
    {
        $siswa = $this->Penilaianeskul_model->get_penilaian_siswa($rombel, $idp, $this->appSetting()->tahunpelajaran);
        return $this->response->setJSON($siswa);
    }
    public function getCapaian($rombel, $idp)
    {
        $siswa = $this->Penilaianeskul_model->get_capaian_siswa($rombel, $idp);
        return $this->response->setJSON($siswa);
    }

    public function add()
    {
        $validationRules = [

            'nama' => 'required',
        ];

        if ($this->request->isAJAX()) {
            $response = ['success' => false, 'errors' => []];
            if ($this->validate($validationRules)) {
                $response['success'] = true;
                $data = array(

                    'nama_Tujuan' => $this->request->getPost('nama'),
                    'status' => $this->request->getPost('status'),
                );

                $insert = $this->Penilaianeskul_model->Tujuan_add($data);
                if ($insert) {
                    return $this->response->setJSON(['success' => true, 'message' => 'Mata Pelajaran Add successfully']);
                } else {
                    return $this->response->setStatusCode(404)->setJSON(['success' => false, 'message' => 'Mata Pelajaran not found']);
                }
            } else {
                $response['errors'] = $this->validator->getErrors();
            }
            return $this->response->setJSON($response);
        }
    }

    public function edit($id)
    {
        $data = $this->Penilaianeskul_model->get_by_id($id);
        echo json_encode($data);
    }

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

            // Validasi data yang diterima (optional)


            foreach ($data as $item) {
                // Lakukan update predikat dan deskripsi berdasarkan id_penilaian
                $id_penilaian = $item['id_penilaian'];
                $predikat = $item['predikat'];
                $deskripsi = $item['deskripsi'];

                // Lakukan update
                $this->Penilaianeskul_model->penilaian_update(['id' => $id_penilaian], [
                    'predikat' => $predikat,
                    'deskripsi' => $deskripsi
                ]);
            }

            // 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 resetCapaian()
    {
        $idpem = $this->request->getPost('idpem');
        $data = array(
            'deskripsi_tercapai' => "",
            'deskripsi_belum' => "",
        );

        $exec = $this->Penilaianeskul_model->penilaian_update(['id_pembelajaran' => $idpem], $data);
        // dd($error);
        if ($exec) {
            return $this->response->setJSON(['success' => true, 'message' => 'Reset successfully']);
        } else {
            //return $this->response->setJSON($error);
            return $this->response->setJSON(['success' => false, 'message' => 'Tidak ada data yang mengalami perubahan']);
        }
    }


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

        if ($user) {
            $this->Penilaianeskul_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']);
        }
    }
}
