<?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; } }
	.mdAvatar .modal-dialog .modal-content .modal-header {
		background-color: #363B3F;
	}
	.mdAvatar .modal-dialog .modal-content .bootstrap-dialog-title{
		display: block;
	}
</style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('Contenido'); ?>
<div class="row">
	<div class="col-md-12">
		<h2><i class="fa fa-envelope-o"></i> E-Mail Marketing </h2>
	</div><!--/col-md-12-->
</div><!--/row-->

<div class="row">
	<?php /*
	<div class="col-md-12">
		<nav class="navbar navbar-inverse">
			<ul class="nav navbar-nav">
				<li><a href="<?php echo URL::to('templates'); ?>">Ver todos los Templates</a></li>
				<li><a href="<?php echo URL::to('templates/create'); ?>">Crear un Template</a>
			</ul>
		</nav>
	</div>
	*/ ?>
	<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">
					Listas
					&nbsp;&nbsp; <button type="button" class="btn btn-success btn-global" onclick="BuscarSuscriptor()"> <i class="fa fa-search"></i> Búsqueda global </button>
					<div data-toggle="buttons" class="button-group pull-right">
						<a class="tooltips" data-toggle="tooltip" data-original-title="Busqueda global de suscriptor" onclick="BuscarSuscriptor()" href="javascript:BuscarSuscriptor()" style="color: rgba(0, 0, 0, 0.3)">
							<i class="fa fa-search"> </i>
						</a>
						<a class="refresh tooltips" data-toggle="tooltip" data-original-title="Actualizar registros" onclick="Actualizar()" href="javascript:Actualizar()" style="color: rgba(0, 0, 0, 0.3)">
							<i class="fa fa-repeat"> </i>
						</a>
					</div>
				</h3>
			</div>
			<div class="porlets-content">
				<div class="tab-container">
					<ul class="nav nav-tabs">
						<li class="active"><a href="#genericas" data-toggle="tab"> De suscriptores </a></li>
						<li class=""><a href="#segmentadas" data-toggle="tab"> Segmentadas </a></li>
					</ul>
					<div class="tab-content">
						<div class="tab-pane cont active" id="genericas">
							<div class="table-responsive">
								<table class="table table-striped table-bordered table-hover" id="tblListasSuscriptores">
									<thead>
										<tr>
											<th class="all"> Nombre de la lista </th>
											<th style="width: 10%;" class="min-tablet-l"> Suscriptores </th>
											<th style="width: 10%;" class="min-tablet-l"> Desuscriptos </th>
											<th style="width: 38%;" class="min-tablet-l"> Acciones </th>
										</tr>
									</thead>
								</table>
							</div>
						</div>
						<div class="tab-pane" id="segmentadas">
							<div class="table-responsive">
								<table class="table table-striped table-bordered table-hover" id="tblListasSegmentados">
									<thead>
										<tr>
											<th class="all"> Nombre de la lista </th>
											<th style="width: 10%;" class="min-tablet-l"> Suscriptores </th>
											<th style="width: 10%;" class="min-tablet-l"> Desuscriptos </th>
											<th style="width: 30%;" class="min-tablet-l"> Acciones </th>
										</tr>
									</thead>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="bottom">
				<?php echo HTML::linkRoute('SysWebEmailMarketingListasSubscriptoresAgregar', 'Crear', array(), array('class' => 'btn btn-success btnAgregar', 'id' => 'btnSuscriptoresAgregar')); ?>

				<?php if(strpos(public_path(), 'prospector') !== false): ?>
				<?php else: ?>
				<button <?php echo $isdisable; ?>  onclick="getFaceBookAccessToken()" id="btngetaccesstoken" class="btn btn-primary" >Inicio de sesión con Facebook</button>
				<button <?php if( $isdisable == '' ): ?> disabled <?php endif; ?>  onclick="cerrarSesionFB()" id="" class="btn btn-danger" >Cerrar sesión con Facebook</button>
				<input type="hidden" id="hfaccesskeyid" value="<?php echo $accesskey; ?>">
				<input type="hidden" id="hfexpirydate" value="<?php echo $expirydate; ?>">
				<?php endif; ?>
			</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'); ?>

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

<?php echo HTML::script('jQueryValidate/additional-methods.min.js'); ?>

<?php echo HTML::script('jQueryValidate/jquery.validationEngine-es.js'); ?>

<script type="text/javascript">
	<?php
		$appId = (App::environment() == 'local' || strpos(public_path(), 'desarrollo'))?'216664162092407':'1304262509596042';
		//$appId = '1304262509596042';
	?>
	/* -------- SDK JAVASCRIPT DE FACEBOOK ------- */
	window.fbAsyncInit = function(){
		/*FB.init({
			appId: "<?= $appId ?>",
			xfbml: true,
			version: "v2.8"
		});*/
		FB.init({
			appId: "<?= $appId ?>",
			xfbml: true,
			version: "v3.2"
		});
	};
	(function(d, s, id) {
		var js, fjs = d.getElementsByTagName(s)[0];
		if (d.getElementById(id)) {
			return; }
		js = d.createElement(s);
		js.id = id;
		js.src = "//connect.facebook.net/en_US/sdk.js";
		fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));
	/* --------------------------------------------- */
	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='idL' 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('SysWebEmailMarketingListasSubscriptoresEliminar'); ?>",
						{idL:id},
						function(data){
							$.isLoading("hide"); NProgress.done();
							$("#btnEnviar").prop("disabled", false).html("Eliminar");
							if(data.success){
								new PNotify({ title: "Felicidades!", text: data.mensaje, type: "success" });
								dialogItself.close();
								var table = $("#tblListasSuscriptores").DataTable();
								var table2 = $("#tblListasSegmentados").DataTable();
								table.ajax.reload(null, false);
								table2.ajax.reload(null, false);
							}
							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"
					);
				}
			}]
		});
	}
	/* ---- CREACIÓN DE TOKEN DE ACCESO A LA APLICACIÓN DE FACEBOOK --- */
	function getFaceBookAccessToken() {
		FB.login(function(response){
			console.log( response );
			$.post(
				"<?php echo URL::route('SysWebEmailMarketingAddUserAccessToken'); ?>",
				{accesstoken:response.authResponse.accessToken},
				function(data){
					if(data.success){
						new PNotify({ title: "Felicidades!", text: "La plataforma ya a sido vinculada con tu cuenta de FB", type: "success" });
						//dialogItself.close();
						setTimeout( function(){ window.location.reload(); }, 3000 );
						/*$("#hfaccesskeyid").val(data.accesstoken)
						$("#btngetaccesstoken").prop('disabled', true);*/
					}
					else{
						alert(data.message);
					}
				},
				"json"
			);
		}, {scope: "manage_pages,leads_retrieval"});
	}
	function cerrarSesionFB(){
		$.isLoading({ text: "Cargando..." }); NProgress.start();
		$.get(
			"<?php echo URL::route('SysWebEmailMarketingRevocarPermisosFB'); ?>",
			function(data){
				if(data.success){
					$.isLoading("hide"); NProgress.done();
					new PNotify({ title: "Felicidades!", text: "La plataforma a sido desvinculada con tu cuenta de FB", type: "success" });
					//dialogItself.close();
					setTimeout( function(){ window.location.reload(); }, 3000 );
					/*$("#hfaccesskeyid").val(data.accesstoken)
					$("#btngetaccesstoken").prop('disabled', true);*/
				}
				else{
					alert(data.message);
				}
			},
			"json"
		);
	}
	/* -------------------------------------------------------------- */
	function Actualizar(){
		var table1 = $("#tblListasSuscriptores").DataTable();
		table1.ajax.reload(null, false);
		var table2 = $("#tblListasSegmentados").DataTable();
		table2.ajax.reload(null, false);
	}
	function BuscarSuscriptor(){
		var mensaje = '<div class="row"> \
			<div class="col-xs-12"> \
				<form id="frmBuscarSuscriptor"> \
					<div class="form-group"> \
						<label for="correo_electronico">Correo electrónico del suscriptor</label> \
						<div class="input-group"> \
							<input type="text" class="form-control" id="correo_electronico" name="correo_electronico" placeholder="correo@electronico.com"> \
							<span class="input-group-btn"> \
								<button class="btn btn-success" type="button" id="btnBuscarSGlobal"><i class="fa fa-search"></i></button> \
							</span> \
						</div> \
						<span id="errcorreo_electronico" class="text-danger"></span> \
					</div> \
				</form> \
				<div class="table-responsive"> \
					<table class="table table-striped table-bordered table-hover" id="tblListasSuscriptoresGlobal"> \
						<thead> \
							<tr> \
								<th class="text-center" style="width: 45%;">Nombre de la lista</th> \
								<th class="text-center" style="width: 25%;">Tipo de lista</th> \
								<th class="text-center" style="width: 25%;">Estado del suscriptor</th> \
								<th class="text-center" style="width: 15%;">Acciones</th> \
							</tr> \
						</thead> \
					</table> \
				</div> \
			</div> \
		</div>';
		BootstrapDialog.show({
			title: "<p class='text-center' style='margin: 0px; color:#ccc; font-size: 20px; '> Buscador global de suscriptores </p>",
			type: BootstrapDialog.TYPE_SUCCESS,
			size: BootstrapDialog.SIZE_WIDE,
			message: mensaje,
			draggable: true,
			closable: true,
			cssClass: "mdAvatar",
			onshown: function(dialogItself){
				var modalBody = dialogItself.getModalBody();
				modalBody.find("#correo_electronico").blur(function(e){
					modalBody.find("#correo_electronico").val( modalBody.find("#correo_electronico").val().trim() );
				});
				var formulario = modalBody.find("#frmBuscarSuscriptor").validate({
					lang: "es",
					errorPlacement: function(error, element){
						var p = error.attr("id").indexOf("-error");
						var elemento = "err"+error.attr("id").substr(0, p);
						$("#"+elemento).html( error.html() );
					},
					success: function(error){
						var p = error.attr("id").indexOf("-error");
						var elemento = "err"+error.attr("id").substr(0, p);
						$("#"+elemento).html("");
					},
					rules: {
						correo_electronico: {
							required: true,
							email: true
						}
					},
					onfocusout: false,
					onkeyup: false,
					onclick: false
				});
				modalBody.find("#btnBuscarSGlobal").click(function(e){
					if(formulario.valid()){
						var table = $("#tblListasSuscriptoresGlobal").DataTable();
						table.ajax.reload(null, false);
					}
				});
				modalBody.find("#tblListasSuscriptoresGlobal").DataTable({
					"processing": true,
					"serverSide": true,
					"ordering": false,
					"paging":   true,
					"pageLength": 5,
					"searching": false,
					"ajax": {
						url: "<?php echo URL::route('SysWebEmailMarketingListasSubscriptoresBusquedaGlobal'); ?>",
						data: function(d){
							d.correo_electronico = modalBody.find("#correo_electronico").val().trim();
						}
					},
					columns: [
						{data: "nombre", name: "nombre"},
						{data: "tipo", name: "tipo", "searchable": false, "orderable": false, "class": "text-center"},
						{data: "estado", name: "estado", "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,
					"order": [[ 0, "desc" ]],
					responsive: true,
				});
			}
		});
	}
</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();
	var table = $("#tblListasSuscriptores").DataTable();
	var table2 = $("#tblListasSegmentados").DataTable();
	table.ajax.reload(null, false);
	table2.ajax.reload(null, false);
	table.draw();
	table2.draw();
});
$("#tblListasSuscriptores").DataTable({
	"processing": true,
	"serverSide": true,
	"ajax": "<?php echo URL::route('SysWebEmailMarketingListasSubscriptores'); ?>",
	columns: [
		{data: 'nombre', name: 'nombre'},
		{data: 'suscriptores', name: 'suscriptores', "searchable": false},
		{data: 'desuscritos', name: 'desuscritos', "searchable": false},
		{data: 'acciones', name: 'acciones', "searchable": false, "orderable": false, "class": "text-center"}
	],
	"language": {
		"url": "<?php echo asset('DataTables/Spanish.json'); ?>"
	},
	"fnDrawCallback": function( oSettings ) {
		$(".tooltips").tooltip();
		if(oSettings._iRecordsTotal == 0){
			$(".btnAgregar").webuiPopover({
				trigger: "manual",
				title: "Instrucciones de Plataforma Sume Clientes",
				content: '<div class="alert alert-info" role="alert"> \
					<p style="font-size: 16;">Aún no tienes ningúna lista de suscripción, empieza a crear tus listas de suscripción haciendo clic en el botón de crear</p> \
				<div>',
				closeable: true,
				backdrop: true,
				style: "",
				width: 500,
				multi: true
			});
			$(".btnAgregar").webuiPopover("show");
		}
	},
	autoWidth: false,
	responsive: true
});
$("#tblListasSegmentados").DataTable({
	"processing": true,
	"serverSide": true,
	"ajax": "<?php echo URL::route('SysWebEmailMarketingListasSegmentadas'); ?>",
	columns: [
		{data: 'nombre', name: 'nombre'},
		{data: 'suscriptores', name: 'suscriptores', "searchable": false},
		{data: 'desuscritos', name: 'desuscritos', "searchable": false},
		{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,
	scrollCollapse: true,
	responsive: true
});
$('a[href="#genericas"]').click(function(e){
	$("#btnSuscriptoresAgregar").show();
});
$('a[href="#segmentadas"]').click(function(e){
	$("#btnSuscriptoresAgregar").hide();
});
<?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(); ?>