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

class Category_model extends CI_Model
{
    // public function insert($model)
    // {
    //     $this->db->insert('category_master', $model);
    //     return $this->db->insert_id();
    // }
    public function insert($data)
{
    $this->db->insert('category_master', $data);
    return $this->db->insert_id();
}
    
   
// {
//     $fields = [
//         'category_name' => $this->input->post('category_name'),
//         'status' => $this->input->post('status')
//     ];

//     print_r($fields);
//     exit;
// }
    
    

    public function selectall()
    {
        $this->db->select('id, category_name, status');
        $this->db->where('is_deleted', 0);
        $this->db->order_by('id', 'DESC');
        return $this->db->get('category_master')->result();
    }

    public function getbyid($id)
    {
        $this->db->where('id', $id);
        return $this->db->get('category_master')->result();
    }

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

    public function delete($id)
    {
        // Hard delete. category FK वर ON DELETE CASCADE नाही,
        // म्हणून हा record कुठे वापरला असेल (product_master) तर DB exception टाकेल.
        $this->db->where('id', $id);
        $this->db->delete('category_master');
        return $this->db->affected_rows() > 0;
    }

    public function getActive()
    {
        $this->db->where(['status' => 1]);
        return $this->db->get('category_master')->result();
    }

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