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

class Product extends CI_Controller {


	private $cart_qty   = '' ;
	private $logged 	= 0  ;	
	private $user 		= '' ;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('Db_model');
		
		$this->load->library('cart');
		
		$this->load->helper('basic_helper');
		$ic=0;
		foreach ($this->cart->contents() as $items):
		$ic++;
		
		endforeach;
		$this->cart_qty = ''.$ic.'';
		
		if($this->session->userdata('shop_user2'))
		{
			$this->load->library('encrypt') ;
			$this->user = $this->encrypt->decode($this->session->userdata('shop_user2'));
			$this->logged = 1;
		}
		
	}
	
	public function index()
	{
		$this->category() ;
	}
	
	public function view($pid='',$cid='',$uid='',$title='')
	{
	
	if($pid=="undefined") {
		
		redirect('Search_product') ;
		}
 
		$data['product'] = array();
		$data['result']= array();
		$data['categoryes']=array();
		$data['cart_qty'] = $this->cart_qty ;
		$data['brand_id'] = $this->brand_id ;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		
		
		
		//echo $pid;
		$sql ="SELECT * FROM shop_product where aid=$pid";

$query = $this->db->query($sql);

if ($query->num_rows() > 0) {
foreach($query->result() as $row) {
$subcategory_id=$row->subcategory_id;
}
}
			
		
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		//$data['result'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		$data['result'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>1),'order','asc') ;
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;

		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
//		$data['related'] = $this->Db_model->get_where_by_order_limit('shop_product',array('category_id'=>'$cid'),'order','asc','10') ;
		$data['related2'] = $this->Db_model->get_where_by_order_limit2('shop_product',array('subcategory_id'=>$subcategory_id),'order','asc','8') ;
			
			
        $data['type'] = $this->Db_model->get_all_by_order('shop_type','order','asc') ;
		$data['categoryname'] = $this->Db_model->get_one('shop_category',array('id'=>$cid)) ;
	
		$data['unit'] = $this->Db_model->get_one('shop_unit',array('id'=>$uid)) ;
		
		
        $product = $this->Db_model->get_one('shop_product',array('aid'=>$pid)) ;
        
		$data['categoryes'] = $this->Db_model->get_all_by_order('shop_product','order','asc');
		
		if($product!==FALSE)
		{
		$data['product'] = $product;
		}
		
		
	
	
		$this->load->view('website/product_header',$data);
		$this->load->view('website/menu',$data);
		$this->load->view('website/prd_content1',$data);
		$this->load->view('website/footer');
	}
	
	
	
	public function categorys()
	{
		
		
		$data['result'] = array() ;
		$data['cart_qty'] = $this->cart_qty;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['page'] = 'about' ;
		$data['result'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>1),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		$data['category'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'order','desc') ;
		$data['product'] = $this->Db_model->get_all_by_order('shop_product','order','asc') ;
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		
		
	
		$this->load->view('website/header',$data);
		$this->load->view('website/menu',$data);
		$this->load->view('website/category',$data);
		$this->load->view('website/footer');
	}
	
	
	
	
	public function todaysoffers()
	{
		
		
		$data['result'] = array() ;
		$data['cart_qty'] = $this->cart_qty;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['page'] = 'about' ;
		$data['result'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>1),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		$data['category'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		//$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'order','desc') ;
		$data['product'] = $this->Db_model->get_where_by_order('shop_product',array('code'=>1),'order','asc') ;
		//$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		
		
	
		$this->load->view('website/header',$data);
		$this->load->view('website/menu',$data);
		$this->load->view('website/todays',$data);
		$this->load->view('website/footer');
	}
	
	
	
	
	
	
public function category_list($category_id='',$title='')
	{
		$data['result'] = array() ;
		$data['books'] = array() ;
		$data['category']=array() ;
		$data['cart_qty'] = $this->cart_qty ;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['title']=$title;
		
	
	 $data['category_id'] =$category_id;

	
	$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
        $data['brand'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'brand'),'order','asc') ;
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;
		//$data['category']=$data['result'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		
		$data['subcat']= $this->Db_model->get_where_by_order('shop_subcategory',array('category_id'=>$category_id),'id','asc') ;
		
	
		
		
		
		$product =  $this->Db_model->get_where_by_order('shop_product',array('category_id'=>$category_id,'active'=>'Y'),'order','asc');
		
		if($product!==FALSE)
		{
			$data['product'] = $product; 
		}
		
		$this->load->view('website/header',$data);
        $this->load->view('website/menu',$data);
		$this->load->view('website/category_content',$data);
        $this->load->view('website/footer');
	}
	
	
	
	
	
public function subcategory_list($subcategory_id='',$title='')
	{
		$data['result'] = array() ;
		$data['books'] = array() ;
		$data['category']=array() ;
		$data['cart_qty'] = $this->cart_qty ;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['title']=$title;
	
	    $data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
        $data['brand'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'brand'),'order','asc') ;
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;
		//$data['category']=$data['result'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		
        $data['catselect']= $this->Db_model->get_where_by_order('shop_subcategory',array('id'=>$subcategory_id),'id','asc') ;
        $data['subcat']= $this->Db_model->get_where_by_order('shop_subcategory',array('id'=>$subcategory_id),'id','asc') ;
    
	
    	$cat_det = $this->Db_model->get_one('shop_subcategory',array('id'=>$subcategory_id)) ;
		if($cat_det!==FALSE)
		{
		$data['category_id']=$cat_det->category_id;
		}

        $data['subcatlist']= $this->Db_model->get_where_by_order('shop_subcategory',array('id'=>$subcategory_id),'id','asc') ;
	    $product =  $this->Db_model->get_where_by_order('shop_product',array('subcategory_id'=>$subcategory_id,'active'=>'Y'),'order','asc');
		
		
		
		$data['subcategory_id'] =$subcategory_id;
		
		
		if($product!==FALSE)
		{
			$data['product'] = $product; 
		}
		
		$this->load->view('website/header',$data);
        $this->load->view('website/menu',$data);
		$this->load->view('website/category_content',$data);
        $this->load->view('website/footer');
	}
	public function search_product($pid='',$cid='',$uid='',$title='')	{ 		$data['product'] = array();		$data['result']= array();		$data['categoryes']=array();		$data['cart_qty'] = $this->cart_qty ;		$data['brand_id'] = $this->brand_id ;		$data['email'] = $this->user;		$data['logged'] = $this->logged;		$cid= "" ;		$uid= "";		$aid= "" ;		$productdetail="";		$product_name=$this->input->post('search_value');		$product_details = $this->Db_model->get_one('shop_product',array('title'=>$product_name));		if($product_details!==FALSE)		{					$data['aid']=$product_details->aid;			$data['category_id']=$product_details->category_id;			$data['unit_id']=$product_details->unit_id;			$data['title']=url_title($product_name, '-', TRUE);		}		echo json_encode($data);	}
	
	
}
