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

class Search extends CI_Controller {


	private $cart_qty   = '' ;
	private $logged 	= 0  ;	
	private $user 		= '' ;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('loginfunctions');
		
		$this->load->model('Db_model');
		
		$this->load->library(array('form_validation'));
		
		$this->load->library('cart');
		
		$this->load->library("pagination");
		
		$this->load->helper('basic_helper');
		
		$this->load->library('user_loginfunctions');
		
		$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;
		}
		
		
		if($_COOKIE['username_m'] !='' && $_COOKIE['password_m']!='' )
		{	
		if($this->user_loginfunctions->mobile_log($_COOKIE['username_m'],$_COOKIE['password_m']))
		{
			// echo $_COOKIE['username_m'];
			$this->load->library('encrypt') ;
			$this->user = $_COOKIE['username_m'];
			$this->logged = 1;
		}	
		}
		
		
		
	}
	
	
	
	
	
	
	public function index()
	{
		
		

		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		
		
	   if($this->input->post('type'))
		{
		
		
	
		 $buy=0;
		 $type=0;
		  $stype=0;
		 $district=0;
		 $town=0;	
		 $cat='';
		 $subcat='';
		 $dname='';
		 $dcity='';
		
		 //$buy = $this->input->post('buy'); 
		  $type = $this->input->post('type'); 
		  $stype = $this->input->post('stype'); 
		  $district = $this->input->post('district'); 
		  $town = $this->input->post('town'); 


if($type!='') {

$sql ="SELECT * FROM shop_category  where id=$type ";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
foreach($query->result() as $row) {
$cat=$row->title;
}
}
}



if($stype!='') {
$sql1 ="SELECT * FROM shop_subcategory where id=$stype ";
$query1 = $this->db->query($sql1);
if ($query1->num_rows() > 0) {
foreach($query1->result() as $row1) {
$subcat=$row1->title;
}
}
}

		 	 	
if($district!='') {
$sql2 ="SELECT * FROM districts where id=$district ";
$query2 = $this->db->query($sql2);
if ($query2->num_rows() > 0) {
foreach($query2->result() as $row2) {
$dname=$row2->name;
}
}
}


if($town!='') {
$sql3 ="SELECT * FROM city where id=$town ";
$query3 = $this->db->query($sql3);
if ($query3->num_rows() > 0) {
foreach($query3->result() as $row3) {
$dcity=$row3->title;
}
}
}



/*if($subcat!='') { $subcat='-'.$subcat.'';}
if($dname!='') { $dname='-'.$dname.'';}
if($dcity!='') { $dcity='-'.$dcity.'';}
*/

if($cat!='') { $cat=''.url_title($cat,'-', TRUE).'';}
if($subcat!='') { $subcat=''.url_title($subcat,'-', TRUE).'';}
if($dname!='' && $subcat!='') { $dname='-'.url_title($dname,'-', TRUE).'';}
if($dname!='' && $subcat=='') { $dname=''.url_title($dname,'-', TRUE).'';}
if($dcity!='') { $dcity='-'.url_title($dcity,'-', TRUE).'';}


		 

		redirect('Search/rn/1/'.$type.'/'.$stype.'/'.$district.'/'.$town.'/'.$cat.'-'.$subcat.''.$dname.''.$dcity.'');
		
		}
		
		$config = array();
        $config["base_url"] = base_url() . "Search";
        $config["total_rows"] = $this->Db_model->get_count_pagi('shop_product');
        $config["per_page"] = 12;
        $config["uri_segment"] = 2;


        $this->pagination->initialize($config);

        $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;

        $data["links"] = $this->pagination->create_links();

        $data['allproduct'] = $this->Db_model->get_authors('shop_product',$config["per_page"], $page,'La_modi_date','desc');
		//$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y','featured'=>'1'),'La_modi_date','desc') ;
		
		 $data['subcat']= $this->Db_model->get_where_by_order('shop_subcategory',array(),'id','asc') ;
		 $data['district'] = $this->Db_model->get_where_by_order('districts',array('state_id'=>'18'),'name','asc') ;
		  $data['category'] = $this->Db_model->get_all_by_order('shop_category',array(),'order','asc') ;

       // $this->load->view('authors/index', $data);
		
		
		
//$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		
		
		
		$this->load->view('website/header.php',$data);
        $this->load->view('website/menu',$data);
		$this->load->view('website/search',$data);
        $this->load->view('website/footer');
	}


   public function rn($page='',$buy='',$type='',$district='',$town='')
	{
	
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
	     $buy=0;
		 $data['type'] =  $type=0;
		 $data['stype'] = $stype=0;
		 $data['district'] = $district=0;
		 $data['town'] = $town=0;	
		 $cat='';
		 $subcat='';
		 $dname='';
		 $dcity='';
		


		if($this->input->post('type'))
		{
		echo "form submied";
		
		
		 //$buy = $this->input->post('buy'); 
		$type = $this->input->post('type'); 
		  $stype = $this->input->post('stype'); 
		  $district = $this->input->post('district'); 
		  $town = $this->input->post('town'); 


if($type!='') {

$sql ="SELECT * FROM shop_category  where id=$type ";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
foreach($query->result() as $row) {
$cat=$row->title;
}
}
}



if($stype!='') {
$sql1 ="SELECT * FROM shop_subcategory where id=$stype ";
$query1 = $this->db->query($sql1);
if ($query1->num_rows() > 0) {
foreach($query1->result() as $row1) {
$subcat=$row1->title;
}
}
}

		 	 	
if($district!='') {
$sql2 ="SELECT * FROM districts where id=$district ";
$query2 = $this->db->query($sql2);
if ($query2->num_rows() > 0) {
foreach($query2->result() as $row2) {
$dname=$row2->name;
}
}
}


if($town!='') {
$sql3 ="SELECT * FROM city where id=$town ";
$query3 = $this->db->query($sql3);
if ($query3->num_rows() > 0) {
foreach($query3->result() as $row3) {
$dcity=$row3->title;
}
}
}



/*if($subcat!='') { $subcat='-'.$subcat.'';}
if($dname!='') { $dname='-'.$dname.'';}
if($dcity!='') { $dcity='-'.$dcity.'';}
*/

if($cat!='') { $cat=''.url_title($cat,'-', TRUE).'';}
if($subcat!='') { $subcat=''.url_title($subcat,'-', TRUE).'';}
if($dname!='' && $subcat!='') { $dname='-'.url_title($dname,'-', TRUE).'';}
if($dname!='' && $subcat=='') { $dname=''.url_title($dname,'-', TRUE).'';}
if($dcity!='') { $dcity='-'.url_title($dcity,'-', TRUE).'';}


		 

		redirect('Search/rn/1/'.$type.'/'.$stype.'/'.$district.'/'.$town.'/'.$cat.'-'.$subcat.''.$dname.''.$dcity.'');
		
		}
		
		
		
 $data['type'] = $this->s_cat = $this->uri->segment(4,'');
 $data['stype'] = $this->s_subcat = $this->uri->segment(5,'');
 $data['district_id'] =  $this->s_dis_id = $this->uri->segment(6,'');
 $data['town_id'] = $this->s_city = $this->uri->segment(7,'');
 $this->s_text = $this->uri->segment(8,'');
		
		
		 $data['category'] = $this->Db_model->get_all_by_order('shop_category',array(),'order','asc') ;
		 $data['subcat']= $this->Db_model->get_where_by_order('shop_subcategory',array(),'id','asc') ;
		 $data['district'] = $this->Db_model->get_where_by_order('districts',array('state_id'=>'18'),'name','asc') ;
		 $data['town'] = $this->Db_model->get_all_by_order('city',array(),'order','asc') ;
		 
		 
		//echo "cat)id"; echo $this->s_dis_id;
		 
		 
		 if($this->s_cat!=0) { $data['subcat']= $this->Db_model->get_where_by_order('shop_subcategory',array('category_id'=>$this->s_cat),'id','asc') ; }
		 if($this->s_dis_id!=0) { $data['town']= $this->Db_model->get_where_by_order('city',array('dist_id'=>$this->s_dis_id),'order','asc') ; }
		 
		 
		
	 

       if($this->s_cat!=0 && $this->s_dis_id!=0  && $this->s_subcat==0 && $this->s_city==0)
       {	
	    $allproduct = $data['allproduct'] = $this->Db_model->get_where_by_order_seearch('shop_product',array('category_id'=>$this->s_cat,'dist_id'=>$this->s_dis_id),'La_modi_date','desc');
	   }
	   
	  if($this->s_cat!=0 && $this->s_dis_id!=0  && $this->s_subcat==0 && $this->s_city!=0)
       {	
	    $allproduct = $data['allproduct'] = $this->Db_model->get_where_by_order_seearch('shop_product',array('category_id'=>$this->s_cat,'dist_id'=>$this->s_dis_id,'city_mainid'=>$this->s_city),'La_modi_date','desc');
	   }
	   
	  
	   
	  if($this->s_cat!=0 && $this->s_dis_id!=0  && $this->s_subcat!=0 && $this->s_city==0)
       {	
	    $allproduct = $data['allproduct'] = $this->Db_model->get_where_by_order_seearch('shop_product',array('category_id'=>$this->s_cat,'dist_id'=>$this->s_dis_id,'subcategory_id'=>$this->s_subcat),'La_modi_date','desc');
	   }
	 

       if($this->s_cat!=0 && $this->s_dis_id!=0  && $this->s_subcat!=0 && $this->s_city!=0)
       {	
	    $allproduct = $data['allproduct'] = $this->Db_model->get_where_by_order_seearch('shop_product',array('category_id'=>$this->s_cat,'subcategory_id'=>$this->s_subcat,'dist_id'=>$this->s_dis_id,'city_mainid'=>$this->s_city),'La_modi_date','desc');
	   }
	 

	   
	   
	   
	
	
	
	   
	   
	   
	/*	}
		 if($buy!=0 && $type==0 && $district!=0)
        {	
        $allproduct = $data['allproduct'] = $this->Db_model->get_where_by_order_seearch('shop_product',array('dist_id'=>$district,'code'=>$buy),'La_modi_date','desc');
		}
		
		 if($buy==0 && $type!=0 && $district!=0)
        {	
        $allproduct = $data['allproduct'] = $this->Db_model->get_where_by_order_seearch('shop_product',array('dist_id'=>$district,'category_id'=>$type),'La_modi_date','desc');
		}
		
		 if($buy!=0 && $type!=0 && $district!=0)
        {	
        $allproduct = $data['allproduct'] = $this->Db_model->get_where_by_order_seearch('shop_product',array('dist_id'=>$district,'code'=>$buy,'category_id'=>$type),'La_modi_date','desc');
		}*/
		




		
		
		$this->load->view('website/header_search.php',$data);
        $this->load->view('website/menu',$data);
		$this->load->view('website/search2',$data);
        $this->load->view('website/footer');
	}




  public function search_id($id='')
	{ 
	
	
	 $data['email'] = $this->user;
		$data['logged'] = $this->logged;
		
		
		
			
		$this->load->view('website/header_search.php',$data);
        $this->load->view('website/menu',$data);
		$this->load->view('website/search_id',$data);
        $this->load->view('website/footer');
	}
 






	
	

}
