<?php $__env->startSection('CSS'); ?>
<?php echo HTML::style('bootstrap-select/css/bootstrap-select.min.css'); ?>

<style type="text/css">
	.tBodyValores input[type="text"] {
		display: block;
	}
	.tBodyValores .form-group {
		padding-left: 15px;
	}
	.tBodyValores .input-group-btn {
		width: unset;
	}
</style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('Contenido'); ?>
<div class="row">
	<div class="col-md-12">
		<h2><i class="fa fa-shopping-cart"></i> Gestión de ventas en línea </h2>
	</div>
</div>

<div class="row">
	<div class="col-md-12">
		<div class="block-web">
			<div class="header"> <h3 class="content-header"> Editar tabla de rangos de precios de envío por <?php echo strtolower($tipo); ?> total </h3> </div>
			<div class="porlets-content">
				<?php if( $tipo == 'PESO' ): ?>
				<div class="alert alert-info">
					El peso debe de ser ingresado en gramos y recuerda que 1 Kilogramo equivale a 1000 Gramos
				</div>
				<?php endif; ?>
				<?php echo Form::open(array('route' => 'SysWebVentasConfEnviosProveedoresRangosEditar', 'method' => 'post', 'class' => 'form-horizontal row-border', 'id' => 'frmAgregar', 'files' => true, 'target' => 'upload_iframe')); ?>

					<table class="table table-hover">
						<thead>
							<tr>
								<th> De </th>
								<th> Hasta </th>
								<th> Precio </th>
								<th class="text-center"> Acciones </th>
							</tr>
						</thead>
						<tbody class="tBodyValores">
							<?php $__empty_2 = true; foreach($RangosPreciosEnvios as $indice => $Rango): $__empty_2 = false; ?>
							<tr class="trRango<?php echo $indice+1; ?>">
								<td>
									<div class="form-group">
										<input type="number" name="rangos[<?php echo $indice+1; ?>][inicio]" value="<?php echo $Rango->inicio; ?>" class="txtInicio<?php echo $indice+1; ?>" step="0.01" required number>
									</div>
								</td>
								<td>
									<div class="form-group">
										<input type="number" name="rangos[<?php echo $indice+1; ?>][fin]" value="<?php echo $Rango->fin; ?>" class="txtFin<?php echo $indice+1; ?>" step="0.01" required number>
									</div>
								</td>
								<td>
									<div class="form-group">
										<input type="number" name="rangos[<?php echo $indice+1; ?>][precio]" value="<?php echo $Rango->precio; ?>" class="txtPrecio<?php echo $indice+1; ?>" step="0.01" required number>
									</div>
								</td>
								<td class="text-center">
									<input type="hidden" name="rangos[<?php echo $indice+1; ?>][id]" value="<?php echo $Rango->id; ?>" class="hdnId<?php echo $indice+1; ?>">
									<button type="button" class="btn btn-danger tooltips" onclick="EliminarValor(<?php echo $indice+1; ?>)" data-toggle="tooltip" data-original-title="Eliminar">
										<i class="fa fa-trash-o"></i>
									</button>
								</td>
							</tr>
							<?php endforeach; if ($__empty_2): ?>
							<tr class="trRangoVacio">
								<td colspan="4"> <p class="text-center"> No existe ningún registro actualmente... </p> </td>
							</tr>
							<?php endif; ?>
						</tbody>
					</table>
					<span id="errrangos" class="text-danger"></span>
					<div class="bottom">
						<button type="button" class="btn btn-info tooltips" data-toggle="tooltip" data-original-title="Agregar nuevo valor de rangos a la tabla" onclick="AgregarValor()">
							<i class="fa fa-plus"></i>
						</button>
						<?php echo Form::button('Guardar', array('class' => 'btn btn-primary', 'id' => 'btnAgregar', 'type' => 'submit')); ?>

						<?php echo HTML::linkRoute('SysWebVentasConfEnviosProveedores', 'Cancelar', array($Pais->id), array('class' => 'btn btn-default')); ?>

					</div>
				<?php echo Form::close(); ?>

		</div>
	</div>
</div><!--/row end-->
<?php $__env->stopSection(); ?>

<?php $__env->startSection('JS'); ?>
<?php echo HTML::script('bootstrap-select/js/bootstrap-select.min.js'); ?>

<?php echo HTML::script('country-region-selector/crs.min.js'); ?>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/additional-methods.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/localization/messages_es.min.js"></script>
<?php echo HTML::script('bootstrap-number-input/bootstrap-number-input-Mod.min.js'); ?>

<?php echo HTML::script('Alphanum/jquery.alphanum.min.js'); ?>

<script type="text/javascript">
	function AgregarValor(){
		if( $("#frmAgregar").valid() ){
			$(".trRangoVacio").remove();
			var N = $(".tBodyValores").find("tr").length+1;
			if( N == 1 ){
				var valorInicio = 0;
				var valorFin = valorInicio+1;
			}
			else{
				var valorInicio = parseFloat( $(".tBodyValores").find("tr:last").find('input[class^="txtFin"]').val() )+1;
				var valorFin = valorInicio+1;
			}
			$(".tBodyValores").append(
				'<tr class="trRango'+N+'"> \
					<td> \
						<div class="form-group"> \
							<input type="number" name="rangos['+N+'][inicio]" value="'+valorInicio+'" class="txtInicio'+N+'" step="0.01" required number> \
						</div> \
					</td> \
					<td> \
						<div class="form-group"> \
							<input type="number" name="rangos['+N+'][fin]" value="'+valorFin+'" class="txtFin'+N+'" step="0.01" required number> \
						</div> \
					</td> \
					<td> \
						<div class="form-group"> \
							<input type="number" name="rangos['+N+'][precio]" value="" class="txtPrecio'+N+'" min="1" max="999999999" step="0.01" required number> \
						</div> \
					</td> \
					<td class="text-center"> \
						<input type="hidden" name="rangos['+N+'][id]" value="0" class="hdnId'+N+'"> \
						<button type="button" class="btn btn-danger tooltips" onclick="EliminarValor('+N+')" data-toggle="tooltip" data-original-title="Eliminar"> <i class="fa fa-trash-o"></i> </button> \
					</td> \
				</tr>'
			);
			$(".tBodyValores").find(".tooltips").tooltip();
			$(".tBodyValores").find('button[data-original-title="Eliminar"]').addClass("disabled");
			$(".tBodyValores").find('button[data-original-title="Eliminar"]:last').removeClass("disabled");
			$(".tBodyValores").find('input[type="text"]').numeric();
		}
	}
	function EliminarValor(id){
		var id = id;
		var idR = $(".hdnId"+id).val();
		if( idR != 0 ){
			$.post(
				"<?php echo URL::route('SysWebVentasConfEnviosProveedoresRangosEliminar', array($Pais->id, $ProveedorEnvioxPais->id, $tipo)); ?>",
				{idR: idR},
				function(data){
					if(data.success){
						new PNotify({ title: "Realizado", text: data.mensaje, type: "success" });
					}
					else{
						if(data.mensaje != ''){ new PNotify({ title: "Error", text: data.mensaje, type: "error" }); }
					}
				},
				"json"
			);
		}
		$(".trRango"+id).remove();
		$(".tBodyValores").find('button[data-original-title="Eliminar"]').addClass("disabled");
		$(".tBodyValores").find('button[data-original-title="Eliminar"]:last').removeClass("disabled");
	}
</script>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('JSOr'); ?>
$(".tBodyValores").find('button[data-original-title="Eliminar"]').addClass("disabled");
$(".tBodyValores").find('button[data-original-title="Eliminar"]:last').removeClass("disabled");
$(".tBodyValores").find('input[type="text"]').numeric();
$("#frmAgregar").validate({
	errorClass: "text-danger",
	errorElement: "span",
	errorPlacement: function(error, element){
		$(element).closest(".form-group").append(error);
	},
	ignore: []
});
$("#frmAgregar").submit(function(e){
	$.isLoading({ text: "Cargando..." }); NProgress.start();
	$("#btnAgregar").html('<i class="fa fa-spinner fa-spin"></i> Cargando').prop("disabled", true);
	e.preventDefault();
	var frmAgregar = new FormData(document.getElementById("frmAgregar"));
	$.ajax({
		type: "POST",
		url: "<?php echo URL::route('SysWebVentasConfEnviosProveedoresRangosEditar', array($Pais->id, $ProveedorEnvioxPais->id, $tipo)); ?>",
		data: frmAgregar,
		cache: false,
		contentType: false,
		processData: false,
		dataType: "json",
		success: function(data){
			$.isLoading("hide"); NProgress.done();
			$("#btnAgregar").html("Guardar").prop("disabled", false);
			if(data.success){
				new PNotify({ title: "Realizado", text: data.mensaje, type: "success" });
				setTimeout( function(){ window.location.href = "<?php echo URL::route('SysWebVentasConfEnviosProveedores', array($Pais->id)); ?>"; }, 3000 );
			}
			else{
				if(data.mensaje != ''){ new PNotify({ title: "Error", text: data.mensaje, type: "error" }); }
				$.each(data.errMensajes, function(key, value){ if(value != ''){ $("#err"+key).html(value); } else{ $("#err"+key).html(""); } });
			}
		},
		error: function(jqXHR, textStatus, errorThrown){
			new PNotify({ title: "Error", text: textStatus + " " + errorThrown, type: "error" });
		}
	});
});
<?php $__env->stopSection(); ?>
<?php echo $__env->make('BaseSistemaWeb', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>