<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class price_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->price = TBL_PRICE;
        $this->category = TBL_CATEGORY;
        $this->item = TBL_ITEM;
    }
    
    public function list_all() {
        $this->db->select('p.*, cat.category_name, i.name, CASE p.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->join($this->category . ' as cat','cat.id = p.category_id');
        $this->db->join($this->item . ' as i','i.id = p.item_id');
        $this->db->where('p.status !=', 2);
        $this->db->order_by('p.id', 'desc');
        return $this->db->get($this->price . ' as p')->result();
    }
    
    public function category_list() {
        $this->db->where('status',1);
        return $this->db->get($this->category)->result();
    }
    
    public function item_list() {
        $this->db->where('status',1);
        return $this->db->get($this->item)->result();
    }
    
    public  function insert($data){  
        $this->db->insert($this->price, $data);  
        return $this->db->insert_id();
    }
    
    public function update($data, $id){
        $this->db->where('id', $id);
        $this->db->update($this->price, $data);
        return $this->db->affected_rows();
    }
    
    public function get_by_id($id) {
        $this->db->select('p.*, cat.category_name, CASE p.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->join($this->category . ' as cat','cat.id = p.category_id');
        #$this->db->join($this->item . ' as i','FIND_IN_SET(p.item_id,i.id)', 'left');
        $this->db->where('p.id',$id);
        return $this->db->get($this->price . ' as p')->row();
    }
    
    public function get_items($item_id) {
        $this->db->select('name as item_name');
        $this->db->where('id',$item_id);
        return $this->db->get($this->item)->row();
    }
   
    public function price_name_exists($cat_name,$item_name,$start_date,$end_date,$id='') {
        $this->db->where('category_id', $cat_name);
        $this->db->where('item_id', $item_name);
        $this->db->where('start_date', date('Y-m-d',strtotime($start_date)));
        $this->db->where('end_date', date('Y-m-d',strtotime($end_date)));
        if($id != '')
            $this->db->where('id !=', $id);
       
        $this->db->where('status !=', 2);
        if($this->db->get($this->price)->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->price)->num_rows();
   }
   
   function get_cat_items(){
       $post = $this->input->post();
       $cat_id = $post['catID'];
       
       $args = array(
           'self.status'         => 1,
       );
       
       if( !empty( $cat_id ) ) :
           $args['self.category_id'] = $cat_id;
       
            if( !empty( $post['pageID'] ) ) :
                //$args['prce.id'] = $post['pageID'];

                $this->db->select('self.id AS id, self.name, prce.id AS price_id, prce.item_id AS price_items_id');
                $this->db->join( $this->price . ' AS prce', 'prce.category_id = self.category_id AND prce.id = '. $post['pageID'] .' AND `self`.`id` IN (`prce`.`item_id`)', 'left' );
                
                
            endif;
       endif;
       
       
       $q = $this->db->get_where( $this->item . ' AS self', $args );
       

       return $q->result_array();
   }
}
?>