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

class Jenis_iuran_model extends CI_Model {

    // ============================================
    // METHOD LAMA — tidak diubah sama sekali
    // ============================================

    public function get_all() {
        return $this->db->get_where('jenis_iuran', array('status' => 'aktif'))->result();
    }

    public function get_by_id($id) {
        return $this->db->get_where('jenis_iuran', array('id' => $id))->row();
    }

    // ============================================
    // METHOD BARU — untuk halaman CRUD jenis iuran
    // ============================================

    // Semua jenis iuran termasuk nonaktif (untuk tabel admin)
    public function get_all_admin() {
        $this->db->order_by('nama_iuran', 'ASC');
        return $this->db->get('jenis_iuran')->result();
    }

    // Cek duplikat kode, $exclude_id dipakai saat edit
    public function kode_exists($kode, $exclude_id = null) {
        $this->db->where('kode', strtoupper($kode));
        if ($exclude_id) {
            $this->db->where('id !=', $exclude_id);
        }
        return $this->db->count_all_results('jenis_iuran') > 0;
    }

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

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

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

    // Hitung tagihan yang pakai jenis ini (proteksi sebelum hapus)
    public function count_tagihan($id) {
        $this->db->where('jenis_iuran_id', $id);
        return $this->db->count_all_results('pembayaran_iuran');
    }
}