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

<?php echo HTML::style('DataTables/responsive.dataTables.min.css'); ?>

<style type="text/css">
	@media (max-width: 450px) { .ModalEliminar .modal-dialog { width: auto; } }
	@media (min-width: 451px) { .ModalEliminar .modal-dialog { width: 450px; } }
	.img-producto { width: 80px; height: 80px; margin: 0px auto; display: block; object-fit: contain;}
</style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('Contenido'); ?>
<div class="row">
	<div class="col-md-12">
		<h2><i class="fa fa-users"></i> Gestión de prospectos </h2>
	</div><!--/col-md-12-->
</div><!--/row-->

<div class="row">
	<div class="col-md-12">
		<?php if(Session::has('message')): ?>
		<div class="alert alert-info"><?php echo Session::get('message'); ?></div>
		<?php endif; ?>
	</div>
</div>

<div class="row">
	<div class="col-md-12">
		<div class="block-web">
			<div class="header"> <h3 class="content-header"> Listado de productos o servicios de la empresa </h3> </div>
			<div class="porlets-content">
				<div class="table-responsive">
					<table class="table table-striped table-bordered table-hover" id="tblEmpleados">
						<thead>
							<tr>
								<th class="all" style="width: 8%;"> Imagen </th>
								<th class="all"> Nombre del producto/servicio </th>
								<!--<th class=""> Precio </th>-->
								<th class="all" style="width: 20%;"> Relacionados </th>
								<th style="width: 20%;" class="min-tablet-l text-center"> Acciones </th>
							</tr>
						</thead>
						</table>
				</div>
			</div>
			<div class="bottom">
				<a href="<?php echo URL::route('SysWebGClientesProductosAgregar'); ?>" class="btn btn-primary"> Agregar </a>
			</div>
		</div>
	</div>
</div><!--/row end-->
<?php $__env->stopSection(); ?>

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

<?php echo HTML::script('DataTables/dataTables.bootstrap.min.js'); ?>

<?php echo HTML::script('DataTables/dataTables.responsive.min.js'); ?>

<script type="text/javascript">
	function Eliminar(id){
		var id = id;
		var mensaje = "<div class='row'> \
							<div class='col-xs-12'> \
								<form id='frmEliminar'> \
									<h2> ¿Seguro que desea eliminar? </h2> \
									<input type='hidden' name='idP' value='"+id+"'> \
								</form> \
							</div> \
						</div>";
		BootstrapDialog.show({
			title: "Eliminar registro",
			type: BootstrapDialog.TYPE_DANGER,
			message: mensaje,
			draggable: true,
			cssClass: "ModalEliminar",
			buttons: [{
				id: "btnEliminar",
				label: "Eliminar",
				cssClass: "btn-danger",
				action: function(dialogItself){
					$.isLoading({ text: "Cargando..." }); NProgress.start();
					$("#btnEliminar").prop("disabled", true).html('<i class="fa fa-spinner fa-spin"></i> Eliminando');
					$.post(
						"<?php echo URL::route('SysWebGClientesProductosEliminar'); ?>",
						{idP: id},
						function(data){
							$.isLoading("hide"); NProgress.done();
							$("#btnAgregar").html("Guardar").prop("disabled", false);
							if(data.success){
								dialogItself.close();
								var table = $("#tblEmpleados").DataTable();
								table.ajax.reload(null, false);
								new PNotify({ title: "Realizado", text: data.mensaje, type: "success" });
							}
							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(""); } });
							}
						},
						"json"
					);
				}
			}]
		});
	}
	function AdministrarInventario(id){
		var id = id;
		var producto = $(".inventarioP"+id);
		var ruta = "<?php echo URL::route('SysWebGClientesProductosEditarInventario', array('idP')); ?>";
		ruta = ruta.replace("idP", id);
		var conPrecioC = (producto.attr("data-tipo")!='P')?"disabled":"";
		var conPeso = (producto.attr("data-tipo")!='P')?"disabled":"";
		var conInventario = (parseInt(producto.attr("data-cantidad-ilimitada"))==1)?"disabled":"";
		var mensaje = "<div class='row'> \
			<div class='col-xs-12'> \
				<table class='table table-bordered' id='idTblAdministrarInventario'> \
					<thead> <tr> <th> Precio </th> <th> Precio comparativo </th> <th> Peso envío (g) </th> <th> Inventario </th> </tr> </thead> \
					<tbody> \
						<tr> \
							<td> \
								<input type='number' class='form-control nmPrecio' value='"+producto.attr("data-precio")+"' min='' max='' step='' data-id=''> \
							</td> \
							<td> \
								<input type='number' class='form-control nmPrecioTachado' value='"+producto.attr("data-precio-tachado")+"' min='' max='' step='' data-id='' "+conPrecioC+"> \
							</td> \
							<td> \
								<input type='number' class='form-control nmPeso' value='"+producto.attr("data-peso")+"' min='' max='' step='' data-id='' "+conPeso+"> \
							</td> \
							<td> \
								<input type='number' class='form-control nmInventario' value='"+producto.attr("data-inventario")+"' min='' max='' step='' data-id='' "+conInventario+"> \
							</td> \
						</tr> \
					</tbody> \
				</table> \
			</div> \
		</div>";
		BootstrapDialog.show({
			title: "Administración de inventario",
			type: BootstrapDialog.TYPE_INFO,
			message: mensaje,
			draggable: true,
			cssClass: "ModalEliminar2",
			buttons: [{
				id: "btnActualizar",
				label: "Editar",
				cssClass: "btn-info",
				action: function(dialogItself){
					$.isLoading({ text: "Cargando..." }); NProgress.start();
					$("#btnEliminar").prop("disabled", true).html('<i class="fa fa-spinner fa-spin"></i> Eliminando');
					$.post(
						ruta,
						{idP: id, precio: $(".nmPrecio").val(), precioTachado: $(".nmPrecioTachado").val(), peso: $(".nmPeso").val(), inventario: $(".nmInventario").val()},
						function(data){
							$.isLoading("hide"); NProgress.done();
							$("#btnAgregar").html("Guardar").prop("disabled", false);
							if(data.success){
								dialogItself.close();
								var table = $("#tblEmpleados").DataTable();
								table.ajax.reload(null, false);
								new PNotify({ title: "Realizado", text: data.mensaje, type: "success" });
							}
							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(""); } });
							}
						},
						"json"
					);
				}
			}]
		});
	}
	function CambiarEstadoProducto(id, estado){
		var id = id;
		var strEstado = ( estado == 1 )?"activar":"desactivar";
		var mensaje = '<div class="row"> \
			<div class="col-xs-12"> \
				<form id="frmCambiarEstado"> \
					<h2> ¿Seguro que deseas '+strEstado+' el producto? </h2> \
					<input type="hidden" name="idP" value="'+id+'"> \
					<input type="hidden" name="estado" value="'+estado+'"> \
				</form> \
			</div> \
		</div>';
		BootstrapDialog.show({
			title: "Cambiar estado del producto",
			type: BootstrapDialog.TYPE_PRIMARY,
			message: mensaje,
			draggable: true,
			cssClass: "ModalEliminar",
			buttons: [{
				id: "btnCambiarEstado",
				label: "Cambiar",
				cssClass: "btn-primary",
				action: function(dialogItself){
					$.isLoading({ text: "Cargando..." }); NProgress.start();
					$("#btnCambiarEstado").prop("disabled", true).html('<i class="fa fa-spinner fa-spin"></i> Cambiando estado...');
					$.post(
						"<?php echo URL::route('SysWebGClientesProductosCambiarEstado'); ?>",
						$("#frmCambiarEstado").serialize(),
						function(data){
							$.isLoading("hide"); NProgress.done();
							$("#btnCambiarEstado").html("Cambiar").prop("disabled", false);
							if(data.success){
								dialogItself.close();
								var table = $("#tblEmpleados").DataTable();
								table.ajax.reload(null, false);
								new PNotify({ title: "Realizado", text: data.mensaje, type: "success" });
							}
							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(""); } });
							}
						},
						"json"
					);
				}
			}]
		});
	}
</script>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('JSOr'); ?>
$("a[data-toggle=\"tab\"]").on("shown.bs.tab", function (e) { $.fn.dataTable.tables( {visible: true, api: true} ).columns.adjust(); });
$("#tblEmpleados").DataTable({
	"processing": true,
	"serverSide": true,
	"ajax": "<?php echo URL::route('SysWebGClientesProductos'); ?>",
	columns: [
		{data: "imagen", name: "imagen", "searchable": false,
			"fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
				var ruta = (oData.imagen != null && oData.imagen != "")?oData.imagen:"https://via.placeholder.com/80x80?text=N/A";
				$(nTd).html('<img src="'+ruta+'" class="img-producto" alt="">');
			}
		},
		{data: "nombre", name: "nombre"},
		/*{data: "precio", name: "precio"},*/
		{data: "relacionados", name: "relacionados", "searchable": false, "orderable": false, "class": "text-center"},
		{data: "acciones", name: "acciones", "searchable": false, "orderable": false, "class": "text-center"}
	],
	"language": {
		"url": "<?php echo asset('DataTables/Spanish.json'); ?>"
	},
	"fnDrawCallback": function( oSettings ) {
		$(".tooltips").tooltip();
	},
	autoWidth: false,
	responsive: true
});
<?php if(Session::has('EliminarMensajeS')): ?>
new PNotify({ title: "Realizado", text: "<?php echo Session::get('EliminarMensajeS'); ?>", type: "success" });
<?php endif; ?>
<?php if(Session::has('EliminarMensajeE')): ?>
new PNotify({ title: "Realizado", text: "<?php echo Session::get('EliminarMensajeE'); ?>", type: "error" });
<?php endif; ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('BaseSistemaWeb', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>