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


class Cart 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.'';
		//$this->cart_qty = ''.$this->cart->total_items().'';
		
		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->view();		
	}
	
	function view()
	{
		$data = array(
				'cart_qty'=>$this->cart_qty,	  
				'email' => $this->user,
				'logged' => $this->logged
				);
		
		
		//$data['result'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'about','active'=>'Y'),'order','desc') ;
		
		$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['banneresult'] = $this->Db_model->get_where_by_order('shop_banner',array('status'=>'1'),'id','desc') ;
		
		$offer_products = $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'),'id','desc') ;
		
	//	$data['category'] = $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') ;
		
		
		
		if($offer_products!==FALSE)
		{
			$data['offer'] = $offer_products;
		}
		else
		{
			$data['offer'] = array() ;
		}
		
		
		
		
		
			
		$this->load->view('website/header',$data);
		$this->load->view('website/menu',$data);
		$this->load->view('website/cart_view',$data);
		$this->load->view('website/footer');
		/*echo '<pre>' ;
		print_r($this->cart->contents());
		echo '</pre>' ;*/
	}
	
	function insert($id='000',$qty=0,$price=0,$unit_id=0,$link='',$name='',$option_array=array())
	{
 $link=base64_decode(urldecode($link));
	
	 echo "<br>"; echo $name; echo "<br>";  echo $qty;  echo "<br>";  echo $price;  echo "<br>";  echo $id; 
	
	
		$this->load->model('Db_model') ;
		$product = $this->Db_model->get_by_id('shop_product','aid',$id) ;
		 
		

		//$qty = $this->input->post('update');
		
		$data = array(
               //'id'      => 'sku_'.$id,
			   'id'      => $id ,
               'qty'     => $qty,
               'price'   => $price,
			   'unit_id' => $unit_id,
               'name'    => $product->title,
			   'image'   => $product->img,
               'options' => $option_array
            );
	

		$this->cart->insert($data); 
		
		//$this->view();		

		redirect($link); //redirect('Cart') ;
	}
	
	
	
	
	function quckinsert($id='000',$qty=0,$price=0,$unit_id=0,$link='',$cid='',$sid='',$bid='')
	{
      $link=base64_decode(urldecode($link));
	  
	echo $cid;  echo "<br>"; 
	echo $sid;  echo "<br>"; 
	echo $bid;  echo "<br>"; 
	echo $price; echo "<br>";
	echo $id; echo "<br>"; 
	echo $name; echo "<br>";  
	echo $qty;  echo "<br>";  
	
	
		$this->load->model('Db_model') ;
		$product = $this->Db_model->get_by_id('shop_product','aid',$id) ;
		 
		

		//$qty = $this->input->post('update');
		
		$data = array(
               //'id'      => 'sku_'.$id,
			   'id'      => $id ,
               'qty'     => $qty,
               'price'   => $price,
			   'unit_id' => $unit_id,
               'name'    => $product->title,
			   'image'   => $product->img,
               'options' => $option_array
            );
	

		$this->cart->insert($data); 
		
		//$this->view();		

		//redirect($link); //redirect('Cart') ;
		
		  redirect('Search_product?cid='.$cid.'&&sid='.$sid.'&&bid='.$bid);
	}
	
	
	
	
	
		function insert_home($id='000',$qty=0,$price=0,$unit_id=0,$name='',$option_array=array())
	{
		$this->load->model('Db_model') ;
		$product = $this->Db_model->get_by_id('shop_product','aid',$id) ;
		

		//$qty = $this->input->post('update');
		
		$data = array(
               //'id'      => 'sku_'.$id,
			   'id'      => $id ,
               'qty'     => $qty,
               'price'   => $price,
			   'unit_id'   => $unit_id,
               'name'    => strlen($product->title)>100 ? substr($product->title,0,100).'..' : $product->title ,
               'options' => $option_array
            );

		$this->cart->insert($data); 
		
		//$this->view();		

		redirect(''); //redirect('Cart') ;
	}
	
	
	function insert1($id='000',$qty=0,$price=0,$name='',$option_array=array())
	{
		$this->load->model('Db_model') ;
		$qty = $this->input->post('qty');
		$did = $this->input->post('did');
		$proid=$this->input->post('proid');
		$product = $this->Db_model->get_by_id('shop_product','aid',$id) ;
		if($qty=='')
		{
		$qty=1;
		}

		//$qty = $this->input->post('update');
		
		$data = array(
               //'id'      => 'sku_'.$id,
			   'id'      => $id ,
               'qty'     => $qty,
               'price'   => $price,
			   'name'    => $name,
               'name'    => strlen($product->title)>100 ? substr($product->title,0,100).'..' : $product->title ,
               'options' => $option_array
            );
		if($this->cart->contents()){
		foreach ($this->cart->contents() as $items):
		
		
		if($items['id']==$proid)
		{
		$this->cart->insert($data); 
		redirect('Cart') ;
		}
		else
		{
		redirect('Product/view/'.$proid) ;
		}
		
		endforeach;
		}
		else
		{
		redirect('Product/view/'.$proid) ;
		}
		
	}
	
	function insert_post()
	{
		$this->load->model('Db_model') ;
		
		$product = $this->Db_model->get_by_id('shop_product','aid',$id) ;
		
		$data = array(
               'id'      => 'sku_'.$this->input->post('rowid'),
               'qty'     => $this->input->post('qty'),
               'price'   => $this->input->post('price'),
               'name'    => $this->input->post('name'),
               'options' => $option_array
            );

		$this->cart->insert($data); 
		
		//$this->view();		
		
		redirect('Cart') ;
	}
	
	function change_qty()
	{   
	/*$data = array(
				'cart_qty'=>$this->cart_qty,	  
				'email' => $this->user,
				'logged' => $this->logged
				);*/
	
	$pqty=$this->input->post('pqty');
 $id = $this->input->post('pid');
 
		
		 $this->load->model('Db_model') ;
		
		$product = $this->Db_model->get_by_id('shop_product','aid',$id) ;
		
			//  print_r($product); 
			  $prices=($product->price)*($pqty);
			  $name=$product->title;
			 // $this->cart->update(); 
			  echo  $data['prices']=$prices;
			 // redirect('userpage/shipaddress') ;
			  
			 
			
	}
	
	private function _cart_inc($data=array())
	{
		//$this->load->view('website/header');
		//$this->load->view('website/menu',$data);
		//$this->load->view('website/cart_view',$data);	
		//$this->load->view('website/right');
		//$this->load->view('website/footer');
	}

	
	function update($link='')
	{
	
	$link=base64_decode(urldecode($link));
		/*$data = array(
               'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
               'qty'   => 3
            );*/
		
		/*echo '<pre>' ;
		print_r($this->input->post());
		echo '</pre>' ;*/
		
       //print_r($this->input->post());
		//$this->cart->update($this->input->post());
		
	 	$dtype=$this->input->post('dtype');
		
		/*echo '<pre>' ;
		print_r($this->input->post());
		echo '</pre>' ;*/
		
		
		
		
		
		
		
		
		
		
		
$formValues = $this->input->post();
foreach($formValues as $key => $username) 
{


$rowids=$username['rowid'];
$new_cart_quty=$username['qty'];
$pro_id= $username['id'];
 $product = $this->Db_model->get_by_id('shop_product','aid',$pro_id) ;
 $product->stock;
  
 
$data[] = array(
        'rowid'  => $rowids,
        'qty'    => $new_cart_quty,
        'id'  => $pro_id,
        'options' => array('cupon' => $dtype));
  
}
		
$this->cart->update($data);		


		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	
		
		if($this->input->post('update'))
		{
		redirect($link);
		
		}	
		if($this->input->post('cupdate'))
		{
		redirect('Product/categorylist');
			
		}
			
		if($this->input->post('placeorder'))
		{
		redirect('Userpage/shipaddress');
		}
		

		//$this->view();		
		
		//redirect('cart') ;
	}
	
	function delete_item($row_id='',$link='')
	{ 
	
	echo $link=base64_decode(urldecode($link));
		$data = array(
               'rowid' => $row_id,
               'qty'   => 0
            );

		$this->cart->update($data);
		
		redirect($link) ;
	}
	
	public function delete($link='')
	{
	$link=base64_decode(urldecode($link));
		$this->cart->destroy();
		
		//$this->view();
		
		redirect($link) ;
	}

	public function shoping()
	{
		$this->load->view('shop');
	}

	
	

	
}//end class

