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

class Jenis_pinjaman_model extends CI_Model {

  public function get_all() {
    $this->db->select('*, nama_pinjaman as nama_jenis, tenor_max as tenor_maksimal');
    $this->db->where('status', 'aktif');
    return $this->db->get('jenis_pinjaman')->result();
}

   public function get_by_id($id) {
    $this->db->select('*, nama_pinjaman as nama_jenis, tenor_max as tenor_maksimal');
    return $this->db->get_where('jenis_pinjaman', array('id' => $id))->row();
}

public function get_all_admin() {
    $this->db->select('*, nama_pinjaman as nama_jenis, tenor_max as tenor_maksimal, denda_persen');
    return $this->db->get('jenis_pinjaman')->result();
}

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

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

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

public function cek_nama_exists($nama, $exclude_id = null) {
    $this->db->where('nama_pinjaman', $nama); // ganti nama_jenis → nama_pinjaman
    if ($exclude_id) {
        $this->db->where('id !=', $exclude_id);
    }
    $query = $this->db->get('jenis_pinjaman');
    return $query->num_rows() > 0;
}
}