<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Materia extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Model_materia');
    }

    // LIST
    public function index()
    {
        $keyword = $this->input->get('keyword', true);

        if (!empty($keyword)) {
            $data['materia'] = $this->Model_materia->cariDadus($keyword);
        } else {
            $data['materia'] = $this->Model_materia->hamosDadus();
        }

        $data['titulu'] = 'Dadus Materia';
        $data['isi'] = 'materia/dadus_materia';

        $this->load->view('layout/all', $data);
    }

    public function get_materia_ajax() {
    $kode = $this->input->post('kode');
    // Sesuaikan nama tabel 'materia' dengan database Anda
    $this->db->where('kode_materia', $kode);
    $data = $this->db->get('materia')->row();
    echo json_encode($data);
}

    // TAMBAH
    public function aumenta()
    {
        $data['titulu'] = 'Aumenta Dadus Materia';
        $data['isi'] = 'materia/aumenta_materia';

        $this->load->view('layout/all', $data);
    }

    // SIMPAN
    public function saluva()
    {
        $data = [
            'kode_materia'  => $this->input->post('kode_materia'),
            'naran_materia' => $this->input->post('naran_materia'),
            'credito'       => $this->input->post('credito'),
            'semestre'      => $this->input->post('semestre'),
            'total_credito' => $this->input->post('total_credito'),
            'total_materia' => $this->input->post('total_materia'),
            'tipu' => $this->input->post('tipu'),
            'departamentu'  => $this->input->post('departamentu')
        ];

        $this->Model_materia->insert($data);

        redirect('materia');
    }

    // EDIT PAGE
    public function hadia($id)
    {
        $data['materia'] = $this->Model_materia->getById($id);

        if (!$data['materia']) {
            redirect('materia');
        }

        $data['titulu'] = 'Hadia Dadus Materia';
        $data['isi'] = 'materia/hadia_materia';

        $this->load->view('layout/all', $data);
    }

    // UPDATE
    public function atualiza($id)
    {
        $data = [
            'kode_materia'  => $this->input->post('kode_materia'),
            'naran_materia' => $this->input->post('naran_materia'),
            'credito'       => $this->input->post('credito'),
            'semestre'      => $this->input->post('semestre'),
            'total_credito' => $this->input->post('total_credito'),
            'total_materia' => $this->input->post('total_materia'),
            'tipu' => $this->input->post('tipu'),
            'departamentu'  => $this->input->post('departamentu')
        ];

        $this->Model_materia->update($id, $data);

        redirect('materia');
    }

    // DELETE
    public function hamos($id)
    {
        $this->Model_materia->delete($id);
        redirect('materia');
    }

// REPORT
public function report()
{
    // Menggunakan tb_materia agar tidak terjadi error table not found
    $this->db->from('tb_materia'); 
    
    $semestre = $this->input->get('semestre') ?: 'ALL';
    
    // Logika Filter
    if ($semestre == 'GANJIL') {
        $this->db->where_in('semestre', ['I', 'III', 'V', 'VII']);
    } elseif ($semestre == 'GENAP') {
        $this->db->where_in('semestre', ['II', 'IV', 'VI', 'VIII']);
    } elseif ($semestre != 'ALL') {
        $this->db->where('semestre', $semestre);
    }
    
    // Eksekusi query
    $data['materia'] = $this->db->get()->result(); 
    $data['semestre_aktif'] = $semestre;
    
    // Pastikan file view berada di folder yang benar: application/views/materia/report.php
    $this->load->view('materia/report', $data);
}
}