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

class Search_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()
	{	
		
		$data['product'] = array();
		$data['preproduct'] = 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;
		
		$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_brand',array(),'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') ;
		//$product = $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		



			
//echo '<pre>'; print_r($previous); echo '</pre>';


//foreach ($previous as $value) {
//  echo "$value <br>";
//  
//}
//	$product = $this->Db_model->where_in('shop_productr',array('aid'=>4,'aid'=>7),'order','asc') ;
//$ids = array(33, 34, 45);
//$dbStations->where_in('id', $ids);
// Produces: WHERE id IN (33, 34, 45)
		
		if($this->input->post('submit'))
		{
			
	        $categories5=$this->input->post('categories');
		    $sub_categories5=$this->input->post('sub_categories');
		   $brand5=$this->input->post('brand');
		   
		  
		   redirect('Search_product?cid='.$categories5.'&&sid='.$sub_categories5.'&&bid='.$brand5.'/');
			
		}
		else
		{
$email_id=$data['email'];
$sql ="SELECT * FROM shop_checkoutusers where email='$email_id' ";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
foreach($query->result() as $row) {
$user_id=$row->id;
}
}
		
$sql2 ="SELECT * FROM shop_order where user_id='$user_id' ";
$query2 = $this->db->query($sql2);
if ($query2->num_rows() > 0) {
foreach($query2->result() as $row2) {
$prod_id[]=$row2->prod_id;		
}
}


if($prod_id!=''){
$previous=array_unique($prod_id);
$preproduct=$product=$this->Db_model->information('shop_product',$previous) ;
}
}
		
if($_GET["cid"]) {  $cid=$_GET["cid"]; } 
if($_GET["sid"]) {  $sid=$_GET["sid"]; } 
if($_GET["bid"]) {  $bid=$_GET["bid"]; }  
if($cid!='') { $product = $this->Db_model->get_where_by_order('shop_product',array('category_id'=>$cid,'active'=>'Y'),'order','asc') ;}  
if($sid!='') { $product = $this->Db_model->get_where_by_order('shop_product',array('subcategory_id'=>$sid,'active'=>'Y'),'order','asc') ;}		
if($bid!='') { $product = $this->Db_model->get_where_by_order('shop_product',array('brand_id'=>$bid,'active'=>'Y'),'order','asc') ;}	


if($cid!='' && $sid!='' ) { $product = $this->Db_model->get_where_by_order('shop_product',array('category_id'=>$cid,'subcategory_id'=>$sid,'active'=>'Y'),'order','asc') ;}
if($cid!='' && $sid!='' &&  $bid!='' ) { $product = $this->Db_model->get_where_by_order('shop_product',array('category_id'=>$cid,'subcategory_id'=>$sid,'brand_id'=>$bid,'active'=>'Y'),'order','asc') ;}

	   
		
		
		if($product!==FALSE)
		{
		$data['product'] = $product;
	
		}
		
		
		
		
		if($this->input->post('addtocart'))
		{
			
			
			echo $pro_id=$this->input->post('pro_id');
			echo $price=$this->input->post('price');
			echo $unit_id=$this->input->post('unit_id');
			echo $url=$this->input->post('url');
			echo $title=$this->input->post('title');
			echo $qty=$this->input->post($pro_id);
			
			redirect('Cart/quckinsert/'.$pro_id.'/'.$qty.'/'.$price.'/'.$unit_id.'/'.$url.'/'.$cid.'/'.$sid.'/'.$bid);
			//'Cart/insert/'.$row_off['aid'].'/1/'.$row_off['price'].'/'.$row_off['unit_id'].'/'.$url.'/'.url_title($row_off->title,'-',true)
		}
		
		
		
		
		$this->load->view('website/header',$data);
        $this->load->view('website/menu',$data);
		$this->load->view('website/search_product',$data);
        $this->load->view('website/footer');
	}
	
	public function view($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;
		$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['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/search_product',$data);
        $this->load->view('website/footer');
	}
	
	
	
	public function subcategory()
	{	
		$category=$this->input->post('categories');
		$subcategory= $this->Db_model->get_where_by_order('shop_subcategory',array('category_id'=>$category),'id','asc') ;
		echo json_encode($subcategory);
	}
	
	
	
	
	
	
	public function search_product()
	{	
	
		$category=$this->input->post('categories');
		$sub_categories=$this->input->post('sub_categories');
		$brand=$this->input->post('brand');
		
		
		
		
		
		
		if($brand!='')
		{		
			$where_condition=array('category_id'=>$category,'subcategory_id'=>$sub_categories,'brand_id'=>$brand,'active'=>'Y');
		}
		else
		{
			$where_condition=array('category_id'=>$category,'subcategory_id'=>$sub_categories,'active'=>'Y');
		}	
		$data['product']=$this->Db_model->get_where_by_order('shop_product',$where_condition,'order','asc');
				
		$product_details=$this->load->view('website/search_product_details',$data);
        echo json_encode($product_details);
	}
	
	
	
	
	
	
	
	
	
	
	
	public function search_id()
	{	
if($this->input->post('search_id'))
{
 $id=$this->input->post('id');
 $url_re=$this->input->post('url_re');
$link=base64_decode(urldecode($url_re));

$sql ="SELECT * FROM `shop_product` WHERE `reg_id`='".$id."'";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
foreach($query->result() as $row) {
$category_id=$row->category_id;
$aid=$row->aid;
$title=$row->title;
}
$title2=url_title($title, '-', TRUE);
redirect('Pro/view/'.$aid.'/'.$category_id.'/1/'.$title2.'/');
}
else
{
	
	
	echo ("<script LANGUAGE='JavaScript'>
    window.alert('Property ID incorrect.Enter valid Property ID.');
    window.location.href='".$link."';
    </script>");
}


}
	
}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
