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

class Cart extends CI_Controller {
	
	public function __construct()
    {
	    parent::__construct();
	     
	}
	
	public function add_to_cart()
	{
		$product_id	= $this->input->post('product_id');
		if(!empty($product_id))
		{
			$res ='';
			$cart_product=$this->Data_access_model->select_data('*','hs_products',array('prod_id'=>$this->input->post('product_id'), 'prod_status' =>1,));
		 
			if($cart_product->num_rows() > 0)
			{
				$data['cartitems'] = $cart_product->result();
				if($data['cartitems'][0]->prod_quantity == "")
				{
					$cart_array=array(
					'id'      => $data['cartitems'][0]->prod_id,
				    'qty'     => $this->input->post('product_quantity'),
				    'price'   => $data['cartitems'][0]->prod_price,
				    'name'    => $data['cartitems'][0]->prod_name,
				    'options' => array('image' => $data['cartitems'][0]->prod_image,)		
				    );
				}else{
					
					if($data['cartitems'][0]->prod_quantity >= $this->input->post('product_quantity'))
					{
						$cart_array=array(
									'id'      => $data['cartitems'][0]->prod_id,
									'qty'     => $this->input->post('product_quantity'),
									'price'   => $data['cartitems'][0]->prod_price,
									'name'    => $data['cartitems'][0]->prod_name,
									'options' => array('image' =>$data['cartitems'][0]->prod_image,),		
									);
					}else{
						echo 0;
					}							
				}
				if(isset($cart_array)){
					$res = $this->cart->insert($cart_array);
				}
			}   
			if($res == true)
			{
				$this->showcart();    
			}  			
		}
		
	}	
	
	public function showcart()
	{
		$cart = $this->cart->contents();
		if(!empty($cart))
		{ 
?>
			<img src="<?php echo base_url(); ?>themeassets/images/svg/cart.svg" alt=""><span class="cart_count"><?php echo $this->cart->total_items() ; ?></span>
			<ul class="fd_cart_contents">
<?php    	$i=0;
			foreach($cart as $items)
			{  
				$i++; 
?>				<li><div class="cart_img">
				<a href="javascript:;"><img src="<?php echo base_url().$items['options']['image']; ?>" alt=""></a>
				</div>
				<div class="cart_data">
				<h2><a href="javascript:;"><?php echo $items['name']; ?></a></h2>
				<span class="fd_quantity"> Quantity : <?php echo $items['qty']; ?></span>
				<span class="fd_p_price"><span>X</span> <?php echo $this->hs->getwebsettings('currency_symbol'); ?><?php echo $items['price']; ?></span>
				</div>
				<span class="cart_close"><a href="javascript:;" onclick="remove_cartitem('<?php echo $items['rowid']; ?>',<?php echo $items['id']; ?>);"><i class="fa fa-times"></a></i></span>
				</li> 
<?php
				if($i == 2){ break; }
			}  
?>
			<li>
			<span class="cart_total">total</span>
			<span class="cart_ttl_amnt"><?php echo $this->hs->getwebsettings('currency_symbol'); ?><?php echo $this->cart->total(); ?></span>
			</li><li><a href="<?php echo base_url().'cart/viewcart'; ?>"   class="cart_btn">view cart</a>
			<?php if(isset($_SESSION['userid'])){   ?>
					
					<a href="<?php echo base_url().'checkout'; ?>" class="cart_btn">checkout</a>
			<?php }else{ ?>
					<a href="#myModal1" class="cart_btn" data-toggle="modal">checkout</a>
			<?php }	?>
			</li>
			</ul>
<?php	}else{  
?>
			<img src="<?php echo base_url(); ?>themeassets/images/svg/cart.svg" alt=""><span class="cart_count"><?php echo $this->cart->total_items() ; ?></span>
			<ul class="fd_cart_contents">
			<li><span class="cart_total">Cart is empty</span></li>
			</ul>
<?php
		}			 
	}		
		
	public function quantity_update()
	{
		$prodrow_id =$this->input->post('p_rowid');
		$prod_id =$this->input->post('prod_id');
		$p_qty =$this->input->post('prod_qty');
		$cart = $this->cart->contents();
		$prod_info = $this->Data_access_model->select_data('*','hs_products',array('prod_id'=>$prod_id, 'prod_status' =>1,));
		if($prod_info->num_rows() > 0)
		{
			$check_qty = $prod_info->row();
			if($check_qty->prod_quantity >= $p_qty || $check_qty->prod_quantity =='')
			{
				if(!empty($prodrow_id))
				{
					if(!empty($cart))
					{
						foreach($cart as $items)
						{
							if($items['rowid'] === $prodrow_id)
							{
										$rowid =$items['rowid'];
							}
						}
						
						$data = array(
						'rowid' => $rowid,
						'qty'   => $p_qty,
						);
											 
						$this->cart->update($data); 
					}
					echo 1;	
				}
			}else{
				echo 2;
			}				
		}
	}	
			
	public function remove_cartitem()
	{
		$prodrow_id =$this->input->post('prodrowid');
		$prod_id = $this->input->post('prodid');
		$cart = $this->cart->contents();
		if(!empty($prodrow_id))
		{
			if(!empty($cart))
			{
				foreach($cart as $items)
				{
					if($items['rowid'] === $prodrow_id){
								$rowid =$items['rowid'];
					}
				}
					$data = array(
							'rowid' => $rowid,
							'qty'   => 0
							);
					$this->cart->update($data);
					$discount_session = 0;
					if(isset($_SESSION['discount_prodid'])){
						if($prod_id == $_SESSION['discount_prodid'] ){
							unset($_SESSION['discount_prodid']);
							unset($_SESSION['discount_amount']);
							unset($_SESSION['copen_code']);
							 $discount_session = 1;
						}
					}
					
			}
		}
		 $resp = array('staus'=>1, 'session_status'=>$discount_session);
		echo json_encode($resp);
	} 

	public function clear_cart()
	{
		$cart = $this->cart->contents();
		if(!empty($cart))
		{
			$this->cart->destroy();
			if(isset($_SESSION['discount_amount'])){
				 unset($_SESSION['discount_prodid']);
				 unset($_SESSION['discount_amount']);
				 unset($_SESSION['copen_code']);
			}
			echo 1;
		}
	}

		
	//*********view cart start************// 

	public function loadview_cart()
	{
		$cart = $this->cart->contents();
		$cart_str ='';		
		if(!empty($cart))
		{
			foreach($cart as $cart_items)
			{
				$cart_str .='<tr><td><div class="cart_block">
							<div class="product_block">
							<div class="product_img">
							<img src="'.base_url().$cart_items['options']['image'].'" alt="product image" />
							</div><div class="product_details"><h3 class="product_name">'.$cart_items['name'].'</h3>
							</div></div></div></td><td><div class="cart_block"><h5 class="product_price"><span>'.$this->hs->getwebsettings('currency_symbol').' </span>'.$cart_items['price'].'</h5>
							</div></td><td><div class="cart_block"><div class="product_quantity">
							<a href="javascript:;" class="quantity_btn" onclick="quantity_minus(\''.$cart_items['rowid'].'\',\''.$cart_items['id'].'\')"><i class="fa fa-minus" aria-hidden="true"></i></a>
							<input type="text" class="cart_input" value="'.$cart_items['qty'].'" id="quantity_'.$cart_items['rowid'].'" />
							<a href="javascript:;" class="quantity_btn" onclick="quantity_plus(\''.$cart_items['rowid'].'\',\''.$cart_items['id'].'\')"><i class="fa fa-plus" aria-hidden="true"></i></a>
							</div></div></td><td><div class="cart_block"><h5 class="product_price"><span>'.$this->hs->getwebsettings('currency_symbol').'</span> '.$cart_items['subtotal'].'</h5>
							</div></td><td><div class="cart_block"><span class="remove_product" onclick="remove_cartitem(\''.$cart_items['rowid'].'\','.$cart_items['id'].');"><i class="fa fa-times" aria-hidden="true"></i></span></div></td></tr>';
			}
			$cart_empty = 0;
		}else{
			$cart_str = '<tr><td colspan="5">Cart is empty</td></tr>';
			$cart_empty =1;
			if(isset($_SESSION['discount_amount'])){
				 unset($_SESSION['discount_prodid']);
				 unset($_SESSION['discount_amount']);
				 unset($_SESSION['copen_code']);
			}
		}								
			$total_cart = array('output'=>$cart_str,'total' =>$this->cart->total(),'cart_res' =>$cart_empty);
			echo json_encode($total_cart);
	}
	
	public function viewcart()
	{	
		$cart = $this->cart->contents();
		if(empty($cart)){
			redirect(base_url());
		}
		$offer_query=$this->Data_access_model->select_data('*','hs_offers',array('offer_status' =>1,));
		if($offer_query->num_rows() > 0)
		{
			$data['all_offer'] =$offer_query->result();
		}else{ 
			$data['all_offer'] = "";
		}
		$partner_query=$this->Data_access_model->select_data('*','hs_partners',array( 'partner_status' =>1,));
		if($partner_query->num_rows() > 0)
		{
			$setting_data['all_partners'] =$partner_query->result();
		}else{ 
			$setting_data['all_partners'] = "";
		}
			
		$opening_time_query =$this->Data_access_model->select_all('hs_opening_schedule');
		if($opening_time_query->num_rows() > 0)
		{
			$setting_data['opening_time'] =$opening_time_query->result();
		}else{ 
			$setting_data['opening_time'] = "";
		}
		$data['page_title'] ='Viewcart';
		$data['page_descri'] ='View Your Cart';
		$this->load->view('theme/common/header',$data);
		$this->load->view('theme/view_cart');
		$this->load->view('theme/common/footer',$setting_data); 
	}
//**********view cart end**********//	
	
}	
 