<?php

namespace App\Models;

use CodeIgniter\Model;

class Kelulusan_model extends Model
{

    var $table = 'kelulusan';

    public function __construct()
    {
        parent::__construct();
    }

    public function get_all_kelulusan()
    {
        //       $query = $this->db->table('kelulusans');
        $query = $this->db->query('select * from kelulusans');
        //      print_r($query->getResult());
        // $query = $this->db->get();
        return $query->getResult();
    }

    public function get_by_id($id)
    {

        $query =  $this->db->table($this->table)

            ->where('kelulusan.id', $id)
            ->get();

        return $query->getRow();
    }
    public function get_penilaian($nis)
    {
        $query =  $this->db->table('penilaian')
            ->select('penilaian.id as id_penilaian,penilaian.nilai,matapelajaran.nama_matapelajaran, matapelajaran.kelompok, matapelajaran.jurusan')
            ->join('matapelajaran', 'matapelajaran.kode=penilaian.kode_mapel')
            ->where('penilaian.nis', $nis)
            ->where('matapelajaran.status', 'aktif')
            ->orderBy('matapelajaran.kelompok', 'ASC')
            ->orderBy('matapelajaran.no_urut', 'ASC')
            ->get();

        $result = $query->getResult();

        // Membuat array untuk menyimpan hasil kelompokkan
        $groupedResult = [];

        // Mengelompokkan data berdasarkan kelompok mata pelajaran
        foreach ($result as $row) {
            $groupedResult[$row->kelompok][] = $row;
        }

        // Mengembalikan hasil kelompokkan
        return $groupedResult;
    }

    public function get_setting_skl($id = 1)
    {
        $query =  $this->db->table('kelulusan')
            ->where('id', $id)
            ->get();

        return $query->getRow();
    }

    public function kelulusan_add($data)
    {

        $query = $this->db->table($this->table)->insert($data);

        return $this->db->insertID();
    }

    public function kelulusan_update($where, $data)
    {
        $this->db->table($this->table)->update($data, $where);
        //        print_r($this->db->getLastQuery());
        return $this->db->affectedRows();
    }

    public function delete_by_id($id)
    {
        $this->db->table($this->table)->delete(array('id' => $id));
    }
}
