<?php
	$server_name='localhost';
    $username='candxszz_candy777';
    $password='Victory37$688';
    $database='candxszz_candycleanse_me';
	$con = mysqli_connect($server_name, $username, $password, $database);
	session_start();
	
	if(isset($_POST['add_cart'])){
		//$user_ip = get_client_ip();
		$product_id = $_POST['id'];
		$quantity = $_POST['quantity'];
		$date_added = date('Y-m-d');
		$ip_address = $_SESSION['ip_address'];
		
		$check_existance = mysqli_query($con,"select * from cart where cust_ip = '$ip_address' and prod_id = '$product_id' and date_added = '$date_added'");
		if(mysqli_num_rows($check_existance)>0){
			$prod_added = 'duplicate';
			//mysqli_query($con,"update cart set quantity = quantity + $quantity where cust_ip = '$ip_address' and prod_id = '$product_id' and date_added = '$date_added'");
		}else{
			$prod_added = 'Yes';
			mysqli_query($con,"insert into cart set quantity = $quantity , cust_ip = '$ip_address' , prod_id = '$product_id' , date_added = '$date_added'");
		}
		$cart_items = mysqli_num_rows(mysqli_query($con,"select * from cart where cust_ip = '$ip_address' and date_added = '$date_added'"));
		$response = array('prod_added'=>$prod_added,'cart_count'=>$cart_items);
		print_r(json_encode($response,true));
	}
	
	if(isset($_POST['update_cart'])){
		$count = 0;
		$date_added = date('Y-m-d');
		$ip_address = $_SESSION['ip_address'];
		foreach($_POST['prod_id'] as $key => $val){
			$prod_id 	= $_POST['prod_id'][$count];
			$quantity 	= $_POST['quantity'][$count];
			if($quantity > 0){
				mysqli_query($con,"update cart set quantity = $quantity where cust_ip = '$ip_address' and prod_id = '$prod_id' and date_added = '$date_added'");
			}else{
				mysqli_query($con,"delete from cart where cust_ip = '$ip_address' and prod_id = '$prod_id' and date_added = '$date_added'");
			}
			$count++;
		}
		header('location:../cart.php');
	}
	
	if(isset($_POST['del_cart'])){
		$date_added = date('Y-m-d');
		$ip_address = $_SESSION['ip_address'];
		mysqli_query($con,"delete from cart where cust_ip = '$ip_address' and date_added = '$date_added'");
		header('location:../index.php');
	}
	
	if(isset($_POST['remove_item_cart'])){
		print_r($_POST);
		$prod_id = $_POST['id'];
		$date_added = date('Y-m-d');
		$ip_address = $_SESSION['ip_address'];
		mysqli_query($con,"delete from cart where cust_ip = '$ip_address' and prod_id = '$prod_id' and date_added = '$date_added'");
	}
	
	if(isset($_POST['get_mini_cart'])){
		$date_added = date('Y-m-d');
		$ip_address = $_SESSION['ip_address'];
		$check_existance = mysqli_query($con,"select cart.*,p.* from cart inner join product p on cart.prod_id = p.product_id where cust_ip = '$ip_address' and date_added = '$date_added'");
		$cart_response = '
				<div class="minicart__header ">
					<div class="minicart__header--top d-flex justify-content-between align-items-center">
						<h3 class="minicart__title"> Shopping Cart</h3>
						<button class="minicart__close--btn" onclick="close_minicart()" aria-label="minicart close btn">
							<svg class="minicart__close--icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32" d="M368 368L144 144M368 144L144 368"></path></svg>
						</button>
					</div>
				</div>
				<form method="post">
		';
		if(mysqli_num_rows($check_existance)>0){
			$mini_total_price = 0;
			while($cart_row = mysqli_fetch_array($check_existance)){
				$mini_total_price += $cart_row['product_price']*$cart_row['quantity'];
				$cart_response.= '
					<div class="minicart__product cart_row_'.$cart_row['product_id'].'">
						<div class="minicart__product--items d-flex">
							<div class="minicart__thumbnail">
								<a href="product-details.php?id='.$cart_row['product_id'].'">
									<img src="images/product_images/'.$cart_row['product_image'].'" alt="prduct-img">
								</a>
							</div>
							<div class="minicart__text">
								<h4 class="minicart__subtitle"><a href="product-details.php?id='.$cart_row['product_id'].'">'.$cart_row['product_title'].'</a></h4>
								<div class="minicart__price">
									<span class="current__price">$'.number_format($cart_row['product_price'],2).'</span>
								</div>
								<div class="minicart__text--footer d-flex align-items-center">
									<input type="hidden" class="" id="prod_id_'.$cart_row['product_id'].'" value="'.$cart_row['prod_id'].'" name="prod_id[]">
									<div class="quantity__box minicart__quantity">
										<button type="button" class="quantity__value decrease" aria-label="quantity value" value="Decrease Value">-</button>
										<label>
											<input type="number" class="quantity__number" id="quantity_'.$cart_row['product_id'].'" name="quantity[]" value="'.$cart_row['quantity'].'">
										</label>
										<button type="button" class="quantity__value increase" aria-label="quantity value" value="Increase Value">+</button>
									</div>
									<button class="minicart__product--remove remove_cart_item" total_price="'.$cart_row['product_price']*$cart_row['quantity'].'" id="remove_cart_item_'.$cart_row['product_id'].'" aria-label="minicart remove btn">Remove</button>
								</div>
							</div>
						</div>
					</div>
				';
			}
			$cart_response.= '
					<div class="minicart__amount">
						<div class="minicart__amount_list d-flex justify-content-between">
							<span>Sub Total:</span>
							<span><b>$'.number_format($mini_total_price,2).'</b></span>
						</div>
						<div class="minicart__amount_list d-flex justify-content-between">
							<span>Total:</span>
							<span><b>$'.number_format($mini_total_price,2).'</b></span>
						</div>
					</div>
					<div class="minicart__button d-flex justify-content-center mb-15">
						<a class="variant__buy--now__btn primary__btn" href="checkout.php"><center>PROCEED TO CHECKOUT</center></a>
					</div>
					<div class="minicart__button d-flex justify-content-center">
						<a class="variant__buy--now__btn primary__btn" href="cart.php" style="background:black !important;"><center>VIEW CART</center></a>
					</div>
				</form>
			';
		}else{
			$cart_response.= '<p style="margin-top: 100px;text-align: center;color: red;">No item in the cart</p>';
		}
		echo $cart_response;
	}
	
?>