<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class item_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->item = TBL_ITEM;
        $this->category = TBL_CATEGORY;
    }
    
    public function list_all() {
        $this->db->select('i.*, cat.category_name',FALSE);
        $this->db->join($this->category . ' as cat','cat.id = i.category_id','left');
        $this->db->where('i.status !=', 2);
        $this->db->order_by('i.id', 'desc');
        return $this->db->get($this->item . ' as i')->result();
    }
    
    public function active_item_count() {
        $this->db->where('status', 1);
        return $this->db->get($this->item)->num_rows();
    }
    
    public function category_list() {
        $this->db->where('status', 1);
        return $this->db->get($this->category)->result();
    }
    
    public  function insert($data){  
        $this->db->insert($this->item, $data);  
        return $this->db->insert_id();
    }   
    
    public function update($data, $id){
        $this->db->where('id', $id);
        $this->db->update($this->item, $data);
        return $this->db->affected_rows();
    }
    
    public function get_item_by_id($id) {
        $this->db->select('i.*, cat.category_name, CASE i.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->join($this->category . ' as cat','cat.id = i.category_id','left');
        $this->db->where('i.id',$id);
        return $this->db->get($this->item . ' as i')->row();
    }
   
    public function item_name_exists($category_id = '', $id = '', $item_name) {
        if(!empty($category_id))
            $this->db->where('category_id', $category_id);
    
        $this->db->where('name', $item_name);
        if($id != '')
            $this->db->where('id !=', $id);
       
        $this->db->where('status !=', 2);
        if($this->db->get($this->item)->num_rows())
            return 1;
        else 
            return 0;
   }
   
   public function item_url_key_exists($url_key, $id) {
        if (!empty($id)) {
            $this->db->where('id !=', $id);
        }
        $this->db->where('url_key', $url_key);
        return $this->db->get($this->item)->num_rows();
   }
   
   function ajax(){
       $post = $this->input->post();
       $return = array();
       
       switch( $post['type'] ) :
           
           case 'check' :
               
               $item_slug = get_var_name( $post['value'] );
               
               $this->db->select('ii.category_id, ic.name AS cat_name, ii.id AS item_id');
               $this->db->join( TBL_INV_CATEGORY . ' AS ic', 'ic.id = ii.category_id' );
               $q = $this->db->get_where( TBL_INV_ITEM . ' AS ii', array( 'ii.status' => 1, 'ii.item_slug' => $item_slug ) );
               $return = $q->row_array();
               
           break;  
       
           case 'get_inv_cat':
               
               $q = $this->db->get_where( TBL_INV_CATEGORY, array( 'id' => $post['value'] ) );
               $return = $q->row_array();
           break;    
           
       endswitch;
       
       return $return;
   }
}
?>