<?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['contactus'] = $this->db_model->get_where_by_order('contact',array('status'=>'1'),'aid','desc') ;
		
		
		//$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('category','slct','asc') ;
		
		$data['contactus'] = $this->db_model->get_where_by_order('contact',array('status'=>'1'),'aid','desc') ;
		
		$data['category'] = $this->db_model->get_all_by_order('category','slct','asc') ;
		
		if($offer_products!==FALSE)
		{
			$data['offer'] = $offer_products;
		}
		else
		{
			$data['offer'] = array() ;
		}
		
		
		
		
		
			
		$this->load->view('website/header',$data);
		$this->load->view('website/topbar',$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($pid='000',$qty=0,$price=0,$name='',$option_array=array())
	{
		$this->load->model('db_model') ;
		$product = $this->db_model->get_by_id('product','pid',$pid) ;
		

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

		$this->cart->insert($data); 
		
		//$this->view();		
		
		redirect('Product/view/'.$pid.'/'.$name) ;
	}
	
	
	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('product','pid',$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)>15 ? substr($product->title,0,15).'..' : $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('product','pid',$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('product','pid',$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()
	{
		/*$data = array(
               'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
               'qty'   => 3
            );*/
		
		/*echo '<pre>' ;
		print_r($this->input->post());
		echo '</pre>' ;*/
		

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

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

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

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

	
	

	
}//end class

