$(function () {
	$(".clearjs").on("click", function () {
		return confirm('确定要清空购物车吗？');
	})

	// 加和减数量
	$('#minus').click(function () {
		var count = Number($('#J_IptAmount').val())
		if (count > 1) {
			$("#J_IptAmount").val(count - 1)
		}
	})
	$('#add').click(function () {
		var count = Number($('#J_IptAmount').val())
		var inventory = Number($('#inventory').html())
		if (count < inventory) {
			$("#J_IptAmount").val(count + 1)
		} else {
			alert('超出库存')
		}
	})
	$('#J_IptAmount').change(function () {
		var inventory = Number($('#inventory').html())
		if (Number($(this).val()) < 1) {
			$("#J_IptAmount").val(1)
		}
		if (Number($(this).val()) > inventory) {
			alert('超出最大库存量')
			$("#J_IptAmount").val(inventory)
		}
	})

	$('.quantity').change(function () {
		var set_quantity = $(this)
		var formData = new FormData();
		formData.append("action", 'edit');
		formData.append("quantity", Number($(this).val()));
		formData.append("id", Number($(this).prev().val()));
		$.ajax({
			scriptCharset: 'UTF-8',
			type: 'post',
			url: '',
			data: formData,
			cache: false,
			processData: false,
			contentType: false,
			dataType: "json",
			success: function (data) {
				if (data.status == 0) {
					alert(data.msg)
					if (data.inventory) {
						set_quantity.val(data.inventory)
					}
				}
			}
		})
	})
})
