<script type="text/javascript">
	$(".quick_view").on("click", function() { 
		var prod_title = $(this).attr('prod_title');
		var prod_image = $(this).attr('prod_image');
		var prod_desc  = $(this).attr('prod_desc');
		var prod_price = $(this).attr('prod_price');
		var prod_id    = $(this).attr('prod_id');
		$("#prod_id").val(prod_id);
		$("#prod_title").html(prod_title);
		$("#prod_desc").html(prod_desc);
		$("#prod_image").attr('href',"images/product_images/"+prod_image);
		$("#prod_image img").attr('src',"images/product_images/"+prod_image);
		$(".glightbox").attr('src',"images/product_images/"+prod_image);
		$("#prod_price").html(prod_price);
		
		//$(".modal-dialog").css('opacity',1);
		//$(".modal").css('opacity',1);
		//$(".modal").css('visibility','visible');
	});
	function add_cart(prod_id = '',prod_quantity = 1, btn_id){
		if(prod_id == ''){
			var prod_id    		= $("#prod_id").val();
			var prod_quantity   = $("#prod_quantity").val();
		}
		$('#btn_'+btn_id+' .cart_btn').attr("disabled", true);
		$('#btn_'+btn_id+' .cart_btn').html('<i class="fa fa-refresh fa-spin" id="spinner"></i>');
		$.ajax({
			url: "includes/cart_operations.php",
			type: "post",
			data: {'add_cart':'yes',id:prod_id,quantity:prod_quantity} ,
			success: function (response) {
				var response_array = $.parseJSON(response);
				if(response_array['prod_added'] == 'duplicate'){
					toastr.info('Item Already Added');
					$('#btn_'+btn_id+' .cart_btn').attr("disabled", false);
					$('#btn_'+btn_id+' .cart_btn').html('Add To Cart');	
				}else{
					$('.cart_count').html(response_array['cart_count']);
					$('#btn_'+btn_id+' .cart_btn').attr("disabled", false);
					$('#btn_'+btn_id+' .cart_btn').html('Add To Cart');	
					toastr.success('Item Added Successfully');
					var mini_cart = get_mini_cart();
					$("#modal1").removeClass('is-visible');
					$("body").addClass('offCanvas__minicart_active');
					$(".offCanvas__minicart").addClass('active');
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
			   console.log(textStatus, errorThrown);
			}
		});
		
	}
	
	$(document).on('click', '.remove_cart_item', function(e){
		var pathArray = window.location.pathname.split( '/' );
		var pagename = pathArray[pathArray.length-1];
		//e.preventDefault();
		var item_id = $(this).attr('id');
		if(pagename == 'cart.php'){
			var item_price = $(this).attr('total_price');
			//var cart_total = $(".cart__summary--amount span").html();
			var cart_total = $("#total_price_table").html();
			var updated_total = parseFloat(cart_total) - parseFloat(item_price);
			console.log(item_price+" "+cart_total+" "+updated_total);
			//$(".cart__summary--amount span").html(updated_total);
			$("#total_price_table").html(updated_total.toFixed(2));
			if(updated_total <= 0){
				window.location.href = "index.php";
			}
		}
		item_id = item_id.split('_');
		item_id = item_id[3];
		$('.cart_row_'+item_id).remove();
		var cart_items = $('.cart_count').html();
		cart_items = parseInt(cart_items - 1);
		$('.cart_count').html(cart_items);
		var href = document.location.href;
		var lastPathSegment = href.substr(href.lastIndexOf('/') + 1);
		
		if(lastPathSegment == 'checkout.php'){
			var prod_price = $("#prod_price_"+item_id).val();
			var grand_total = $("#grand_total").html();
			console.log(prod_price+" "+grand_total);
			if((parseFloat(grand_total) - parseFloat(prod_price)) <= 0){
				window.location.href = "index.php";
			}
			$("#grand_total").html((parseFloat(grand_total) - parseFloat(prod_price)).toFixed(2));
			$(".check_row_"+item_id).remove();
		}
		$.ajax({
			url: "includes/cart_operations.php",
			type: "post",
			data: {'remove_item_cart':'yes',id:item_id} ,
			success: function (response) {
				var mini_cart = get_mini_cart();
				//location.reload();
			},
			error: function(jqXHR, textStatus, errorThrown) {
			   console.log(textStatus, errorThrown);
			}
		});
	});
	
	
	function update_qty(id,type){
		if(type == 'win'){
			var quantity = $("#quantity_"+id).val();
			if(quantity > 0){
				$(".minicart__text .decrease").prop('disabled',false);
			}else{
				$(".minicart__text .decrease").prop('disabled',true);
			}
		}else{
			var quantity = $("#prod_quantity").val();
			console.log(quantity);
			if(quantity > 0){
				$(".quantity__box .decrease").prop('disabled',false);
			}else{
				$(".quantity__box .decrease").prop('disabled',true);
			}
		}
	}
	$('.minicart__open--btn').click(function(e){
		e.preventDefault();
		$("body").addClass('offCanvas__minicart_active');
		$(".offCanvas__minicart").addClass('active');
	});
	function close_minicart(){
		console.log('here');
		$("body").removeClass('offCanvas__minicart_active');
		$(".offCanvas__minicart").removeClass('active');
	}
	
	function get_mini_cart(){
		$.ajax({
			url: "includes/cart_operations.php",
			type: "post",
			data: {'get_mini_cart':'yes'} ,
			success: function (response) {
				$('.offCanvas__minicart').html(response);
			},
			error: function(jqXHR, textStatus, errorThrown) {
			   console.log(textStatus, errorThrown);
			}
		});
	}
	
</script>