$(document).ready(function() {
	$('#modalPuntos').on('show.bs.modal', function (event) {
		$('.modal-wait').hide();
		var button = $(event.relatedTarget)
		var modal = $(this);
		var id = button.data('id');

		if (id == 0) {
			//modal.find('.btn-eliminar').hide();
			modal.find('.puntos-info-clientes').show();
			modal.find('.puntos-info-ventas').hide();
			modal.find('.puntos-info-estado').hide();
			modal.find('.puntos-select-id-loyalty-state').val(2);
			modal.find('.puntos-btn-editar').html('Cargar');
			modal.find('.modal-wait').show();
		}

		else{
			modal.find('.puntos-input-id-loyalty').val(id);
			modal.find('.puntos-input-loyalty-id-venta').val(0);
			modal.find('.puntos-input-loyalty-id-order').val(0);
			modal.find('.puntos-input-loyalty-description').val('');
			modal.find('.puntos-input-loyalty-points').val(0);
			modal.find('.puntos-select-id-loyalty-state').val(0);
			modal.find('.puntos-btn-editar').html('Editar');

			var parametros = {
				id: id
			};

			$.ajax({
				data: parametros,
				url:  '/puntos/info',
				type: 'post',
				beforeSend: function () {
				},
				success:  function (response) {
					loyalty = JSON.parse(response);
					modal.find('.puntos-input-email').val(loyalty.email);
					modal.find('.puntos-input-nombre').val(loyalty.nombre);
					modal.find('.puntos-input-id-customer').val(loyalty.id_customer);
					modal.find('.puntos-input-loyalty-id-venta').val(loyalty.id_venta);
					modal.find('.puntos-input-loyalty-id-order').val(loyalty.id_order);
					modal.find('.puntos-input-loyalty-description').val(loyalty.description);
					modal.find('.puntos-input-loyalty-points').val(loyalty.points);
					modal.find('.puntos-select-id-loyalty-state').val(loyalty.id_loyalty_state);

					$('.modal-wait').show();
				}
			});
		}
	});

	$("#puntosEditar" ).click(function(event) {
		var button = $(event.relatedTarget)
		button.attr('disabled', 'disabled');
		var parametros = $('#formPuntos').serialize();
		$.ajax({
			data: parametros,
			url:  '/puntos/save',
			type: 'post',
			beforeSend: function () {
			},
			success:  function (response) {
				var idLoyalty = JSON.parse(response);

				var loyalty = $('#loyalty' + idLoyalty);

				loyalty.find('.loyalty-description').html($('#formPuntos .puntos-input-loyalty-description').val());
				loyalty.find('.loyalty-points').html($('#formPuntos .puntos-input-loyalty-points').val());
				loyalty.find('.loyalty-state').html($('#formPuntos .puntos-select-id-loyalty-state option:selected').text());

				$('#modalPuntos').modal('hide');
			}
		});
	});

	$('.puntos-input-email').autocomplete({
		source: function( request, response ) {
			// Limpia campos relacionados a cliente
			$('.puntos-input-cliente').val('');
			$('.puntos-input-id-customer').val('');

			$.ajax( {
				url: 'clientes/ac_emails',
				dataType: "jsonp",
				data: {
					term: request.term
				},
				success: function( data ) {
					response( data );
				}
			} );
		},
		minLength: 2,
		select: function( event, ui ) {
			//console.log( "Selected: " + ui.item.value + " aka " + ui.item.id );
			$('.puntos-input-id-customer').val(ui.item.ps_id);
			$('.puntos-input-nombre').val(ui.item.nombre);
			$(this).blur();
		}
	});
});