<?php

namespace App\Controllers;

use App\Models\Projekpenilaian_model;

class ProjekPenilaian extends BaseController
{
    public function __construct()
    {
        $this->Projekpenilaian_model = new Projekpenilaian_model();
    }
    private $Projekpenilaian_model;
    public function index()
    {
        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Perencanaan Projek',

        ];
        return view('Admin/Projek/penilaian', $data);
    }

    public function getPenilaian($idrombel = null)
    {
        $data = $this->Projekpenilaian_model->get_penilaian($idrombel);
        return $this->response->setJSON($data);
    }
    public function savePenilaian()
    {
        $nilaiArray = $this->request->getPost('nilai');

        // Simpan data nilai dan switch ke database menggunakan model
        foreach ($nilaiArray as $nilai) {

            $where = ['id_siswa' => $nilai['id_siswa'], 'id_projek' => $nilai['id_projek'], 'id_dimensi' => $nilai['id_dimensi']];
            $ceksiswa = $this->Projekpenilaian_model->get_where_penilaian($where);
            if (!$ceksiswa) {
                $this->Projekpenilaian_model->penilaian_add([
                    'id_siswa' => $nilai['id_siswa'],
                    'nilai' => $nilai['nilai'],
                    'id_projek' => $nilai['id_projek'],
                    'id_dimensi' => $nilai['id_dimensi'],
                ]);
            } else {
                $this->Projekpenilaian_model->penilaian_update($where, ['nilai' => $nilai['nilai']]);
            }
        }

        // Kirim respons ke client
        return $this->response->setJSON(['status' => 'success', 'message' => 'Data berhasil disimpan']);
    }
    public function getRombel()
    {
        $data = $this->Projekpenilaian_model->get_rombel(cekRoleid(), $this->appSetting()->tahunpelajaran);
        return $this->response->setJSON($data);
    }
}
