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

/**
 * Base_model — reusable CRUD for all master tables
 */
class Base_model extends CI_Model {

    protected $table = '';

    public function getAll($where = []) {
        if (!empty($where)) $this->db->where($where);
        return $this->db->get($this->table)->result();
    }

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

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

    public function update($id, $data) {
        return $this->db->update($this->table, $data, ['id' => $id]);
    }

    public function delete($id) {
        return $this->db->delete($this->table, ['id' => $id]);
    }

    public function count($where = []) {
        if (!empty($where)) $this->db->where($where);
        return $this->db->count_all_results($this->table);
    }

    public function getActive() {
        return $this->db->get_where($this->table, ['status' => 1])->result();
    }

    public function toggleStatus($id, $current_status) {
        $new_status = $current_status == 1 ? 0 : 1;
        return $this->db->update($this->table, ['status' => $new_status], ['id' => $id]);
    }
}
