(function(){
	var ShopPurchase = function() {
    return {

    	init: function() {
    		var basketTable = $('.basket-table'),
    			emptyBasket = $('.empty-basket'),
    			shopProcessBtn = $('.shopProcessBtn'),
    			tmp = '<tr class="text-center" data-id="" data-rowid="">' +
	                    '<td class="p-img"> <img src="" /> </td>' +
	                    '<td class="p-name"> <h4 class="bold"></h4><p></p> </td>' +
	                    '<td class="p-price"> $ </td>' +
	                    '<td>' +
	                    '	1' +
	                    '</td>' +
	                    '<td>' +
	                        '<a class="btn yellow btn-removeFromCart"><i class="fa fa-trash"></i></a>' +
	                    '</td>' +
	                    '<td class="p-total">' +
	                        '' +
	                    '</td>' +
	                '</tr>';

	        var removeBtn = function() {
	        	var self = $(this),
					parent = self.parents('tr'),
					id = parent.data('rowid');

				App.ajax({
					url: ajax_url_shop_remove,
					data: {
						id: id
					}
				}, function( data ) {
					if( data && data.data.result == true ) {
						parent.remove();

						emptyBasket.removeClass('hidden');
						basketTable.addClass('hidden');
						shopProcessBtn.addClass('hidden');
					}
				});
	        };

			$('.btn-addToCart').on('click', function(e){
				var self = $(this),
					parent = self.parents('tr'),
					id = parent.data('id');

				App.ajax({
					url: ajax_url_shop_addTo,
					data: {
						id: id
					}
				}, function( data ) {
					if( data && data.data.result == true ) {
						var row = $(tmp);

						emptyBasket.addClass('hidden');
						row.data('id', data.data.items.id);
						row.data('rowid', data.data.items.rowid);
						row.find('.p-img img').attr('src', '/storage/_static/products/' + data.data.items.img);
						row.find('.p-name h4').html(data.data.items.name);
						row.find('.p-name p').html(data.data.items.description);
						row.find('.p-price').html('$ ' + data.data.items.price);
						row.find('.p-total').html('$ ' + data.data.items.price);
						row.find('.btn-removeFromCart').on('click', removeBtn);
						basketTable.find('tbody').empty();
						basketTable.find('tbody').append(row);
						basketTable.removeClass('hidden');
						shopProcessBtn.removeClass('hidden');
					}
				});
			});

			$('.btn-removeFromCart').on('click', removeBtn);
		}
    };
  }();

	$(document).ready(function() {
		ShopPurchase.init();
	});

})();
