<?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; } }
</style>
<?php $__env->stopSection(); ?>


<?php $__env->startSection('Contenido'); ?>
<div class="row">
	<div class="col-md-12">
		<h2><i class="fa fa-users"></i> Administración de cuentas </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 cuentas </h3> </div>
			<div class="porlets-content">
				<div class="table-responsive">
					<table class="table table-striped table-bordered table-hover" id="tblCuentas">
						<thead>
							<tr>
								<th class=""> Empresa </th>
								<th class=""> Usuario principal </th>
								<th style="" class=""> Acciones </th>
							</tr>
						</thead>
						</table>
				</div>
			</div>
			<div class="bottom">
				<?php
					$cantidadCuentas = Cliente::where('cliente_padre', '=', $Cliente->id)->where('vinculado', '=', 1)->count();
					$ocultarBotonNC = ( (int)$cantidadCuentas < (int)$Cliente->cuentas )?'':'style="display: none;"';
				?>
				<input type="hidden" name="cantidadCuentas" value="<?php echo $cantidadCuentas; ?>">
				<input type="hidden" name="cuentas" value="<?php echo $Cliente->cuentas; ?>">
				<a href="<?php echo URL::route('SysWebPerfilCuentasAgregar', array($Usuario->id)); ?>" class="btn btn-primary" id="btnAgregarCuenta" <?php echo $ocultarBotonNC; ?>>
					Agregar cuenta
				</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 la cuenta? </h2> \
					<p> Si desvinculas esta cuenta de tu cuenta principal se mantendrá con los datos como la dejes y podrás vincularla nuevamente en un futuro si lo deseas... pero mientras esté desvinculada todo lo que está dentro de ella dejará de funcionar, páginas de captura, secuencias de seguimiento, campañas, etc. y el usuario no podrá acceder más a esa cuenta porqué quedará inactiva </p> \
					<input type="hidden" name="idC" value="'+id+'"> \
				</form> \
			</div> \
		</div>';
		BootstrapDialog.show({
			title: "Desvincular de tu cuenta",
			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('SysWebPerfilCuentasEliminar', array($Usuario->id)); ?>",
						{idC: id},
						function(data){
							$.isLoading("hide"); NProgress.done();
							$("#btnEliminar").prop("disabled", false).html("Eliminar");
							if(data.success){
								location.reload();
								var table = $("#tblCuentas").DataTable();
								table.ajax.reload(null, false);
								if( parseInt(data.cantidadCuentas) < parseInt(<?php echo $Cliente->cuentas; ?>) ){
									$("#btnAgregarCuenta").show();
								}
								else{
									$("#btnAgregarCuenta").hide();
								}
								dialogItself.close();
							}
							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 Vincular(id){
		var id = id;
		var mensaje = '<div class="row"> \
			<div class="col-xs-12"> \
				<form id="frmEliminar"> \
					<h2> ¿Seguro que desea volver a vincular la cuenta? </h2> \
					<p> Si la vinculas nuevamente, la cuenta se activará automáticamente y todo seguirá funcionando como lo dejaste al momento de desvincularla de tu cuenta (Páginas de captura, secuencias de seguimiento, campañas, prospectos, etc...) </p> \
					<input type="hidden" name="idC" value="'+id+'"> \
				</form> \
			</div> \
		</div>';
		BootstrapDialog.show({
			title: "Vincular a tu cuenta",
			type: BootstrapDialog.TYPE_SUCCESS,
			message: mensaje,
			draggable: true,
			cssClass: "ModalEliminar",
			buttons: [{
				id: "btnVincular",
				label: "Vincular",
				cssClass: "btn-success",
				action: function(dialogItself){
					$.isLoading({ text: "Cargando..." }); NProgress.start();
					$("#btnVincular").prop("disabled", true).html('<i class="fa fa-spinner fa-spin"></i> Eliminando');
					$.post(
						"<?php echo URL::route('SysWebPerfilCuentasVincular', array($Usuario->id)); ?>",
						{idC: id},
						function(data){
							$.isLoading("hide"); NProgress.done();
							$("#btnVincular").prop("disabled", false).html("Eliminar");
							if(data.success){
								location.reload();
								var table = $("#tblCuentas").DataTable();
								table.ajax.reload(null, false);
								if( parseInt(data.cantidadCuentas) < parseInt(<?php echo $Cliente->cuentas; ?>) ){
									$("#btnAgregarCuenta").show();
								}
								else{
									$("#btnAgregarCuenta").hide();
								}
								dialogItself.close();
							}
							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 IniciarSesion(id){
	var idC = id;
	console.log(idC)
	$.post(
		"<?php echo URL::route('SysWebCambiarSesion'); ?>",
		{idC:id, tipo:"user"},
		function(data){
			if(data.success){
				new PNotify({ title: "Realizado", text: data.mensaje, type: "success" });
				setInterval(function(){ window.location.href = "<?php echo URL::route('SysWebIndex'); ?>" }, 1000);
			}
			else{
				if(data.mensaje != ""){ new PNotify({ title: "Error", text: data.mensaje, type: "error" }); }
			}
		},
		"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(); });
$("#tblCuentas").DataTable({
	"processing": true,
	"serverSide": true,
	"ajax": "<?php echo URL::route('SysWebPerfilCuentas', array($Usuario->id)); ?>",
	columns: [
		{data: "empresa", name: "empresa"},
		{data: "username", name: "username"},
		{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(); ?>