<?php

class Model_distribuisaunmateria extends CI_Model
{
    public function hamosDadus()
    {
        return $this->db->order_by('id_distribuisaun', 'ASC')
                        ->get('tb_distribuisaunmateria')
                        ->result();
    }
    public function get_materia_by_kode($kode) {
        return $this->db->get_where('tb_materia', ['kode_materia' => $kode])->row_array();
    }

     public function get_dosente() {
        return $this->db->get('tb_dosente')->result(); 
    }
    public function cariDadus($keyword)
{
    $this->db->group_start(); // Memulai pengelompokan kondisi pencarian
    $this->db->like('kode_materia', $keyword);
    $this->db->or_like('materia', $keyword); // Mengganti 'naran_materia' menjadi 'materia'
    $this->db->group_end(); // Mengakhiri pengelompokan

    return $this->db->get('tb_distribuisaunmateria')->result();
}

    public function insert($data)
    {
        return $this->db->insert('tb_distribuisaunmateria', $data);
    }

    public function getById($id)
    {
        return $this->db->get_where(
            'tb_distribuisaunmateria',
            ['id_distribuisaun' => $id]
        )->row();
    }

    public function update($id, $data)
    {
        return $this->db->where('id_distribuisaun', $id)
                        ->update('tb_distribuisaunmateria', $data);
    }

    public function delete($id)
    {
        return $this->db->where('id_distribuisaun', $id)
                        ->delete('tb_distribuisaunmateria');
    }

    // ===========================
    // JOIN DENGAN tb_materia
    // ===========================
    public function hamosDadusJoin()
    {
        $this->db->select('
            tb_distribuisaunmateria.*,
            tb_materia.naran_materia,
            tb_materia.departamentu
        ');

        $this->db->from('tb_distribuisaunmateria');

        $this->db->join(
            'tb_materia',
            'tb_materia.kode_materia = tb_distribuisaunmateria.kode_materia',
            'left'
        );

        $this->db->order_by('tb_distribuisaunmateria.id_distribuisaun', 'ASC');

        return $this->db->get()->result();
    }
}