<?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; } }
	.modal .bootstrap-dialog-close-button{
		display: block !important;
	}
	<?php
		if( !isset($_GET['import']) ){
	?>
	.oculto {
		display: none !important;
	}
	<?php
		}
	?>
</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>
<?php
	$cantidadCampanhas = Campanha::join('configuraciones_emails AS ce', 'ce.id', '=', 'campanhas.configuraciones_emails_id')
	->where('ce.clientes_id', '=', Cliente::select('id')->where('users_id', '=', Auth::user()->id)->first()->id)->count();
?>
<?php if( $cantidadCampanhas == 0 ): ?>
<div class="row">
	<div class="alert alert-danger" style="margin: 15px;">
		<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
		<div class="row">
			<div class="col-md-1" style="text-align: center;">
				<i class="fa fa-exclamation-triangle" style="font-size: 55px;"></i>
			</div>
			<div class="col-md-8">
				<p style="font-size: 18px; font-family: arial;">
					Por favor antes de crear tu primer campaña asegurate de ver el video "¿Campaña o secuencia de seguimiento?" haciendo <a href="javascript:verVideoDCSS()" style="font-weight: bold;">clic acá</a>
				</p>
			</div>
		</div>
	</div>
</div>
<?php endif; ?>
<div class="row">
	<div class="col-md-12">
		<div class="block-web">
			<div class="header">
				<h3 class="content-header">
					Listado de campañas
					<div data-toggle="buttons" class="button-group pull-right">
						<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="row">
					<div class="table-responsive">
					<table class="table table-striped table-bordered table-hover display" id="tblCampanhas" width="100%" cellspacing="0">
						<thead>
							<tr>
								<th class=""> id </th>
								<th class="col-md-2 all"> Nombre </th>
								<th class="col-md-2 min-tablet-l"> Asunto </th>
								<th class="col-md-2 min-tablet-l"> Listas de suscriptores </th>
								<th class="col-md-1 min-tablet-l"> Enviar a </th>
								<th class="col-md-2 min-tablet-l"> Estado </th>
								<th class="col-md-3 min-tablet-l text-center"> Acciones </th>
							</tr>
						</thead>
					</table>
				</div>
				</div>
			</div>
			<div class="bottom">
				<?php /* HTML::linkRoute('SysWebEmailMarketingCampanhasAgregar', 'Agregar', array(), array('class' => 'btn btn-primary')) */ ?>
				<?php echo HTML::linkRoute('SysWebEmailMarketingCampanhasAgregarV2', 'Agregar', array(), array('class' => 'btn btn-primary')); ?>

				<?php echo HTML::linkRoute('SysWebEmailMarketingListasSubscriptores', 'Regresar', array(), array('class' => 'btn btn-default')); ?>

			</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 PruebaEnvio(idC, idPl){
		var idC = idC;
		var tipo = (idPl != "0")?'PCE':'CEN';
		$.post(
			"<?php echo URL::route('SysWebEmailMarketingPruebaEnvioCObtencionCampos'); ?>",
			{idC:idC, tipo: tipo},
			function(data){
				if(data.success){
					var fielset = "";
					if(data.campos.length > 0){
						var campos = "";
						for(i=0; i<data.campos.length; i++){
							campos += "<div class='form-group'> \
											<label> "+data.campos[i].nombre+" </label> <br> \
											<input type='text' name='"+data.campos[i].slug+"' class='form-control'> \
											<input type='hidden' name='Campos[]' value='"+data.campos[i].slug+"'> \
											<spam id='err"+data.campos[i].slug+"' class='text-danger'> </spam> \
										</div>";
						}
						var fielset = "<fielset class='camposP'> \
											<legend> Campos personalizados del mensaje </legend> \
											"+campos+" \
										</fielset>";
					}
					var mensaje = " \
						<div class='row'> \
							<div class='col-xs-12'> \
								<form id='frmPruebaEnvio'> \
									<div class='form-group '> \
										<label> Correo electronico a enviar </label> <br> \
										<input type='text' name='correo_electronico0' class='form-control'> \
										<spam id='errcorreo_electronico0' class='text-danger'> </spam> \
									</div> \
									"+fielset+" \
									<input type='hidden' name='idC' value='"+idC+"'> \
								</form> \
							</div> \
						</div> \
					";
					BootstrapDialog.show({
						title: "Correo de prueba de campaña",
						type: BootstrapDialog.TYPE_DEFAULT,
						message: mensaje,
						draggable: true,
						closable: false,
						buttons: [{
							id: "btnEnviar",
							label: "Enviar",
							cssClass: "btn-primary",
							action: function(dialogItself){
								$("#btnEnviar").prop("disabled", true).html('<i class="fa fa-spinner fa-spin"></i> Enviando');
								if(idPl != "0"){
									var idP = idPl;
									var ruta = "<?php echo URL::route('SysWebEmailMarketingEnvioCorreoPlantillaxCliente'); ?>";
									var camposP = [];
									var envio = {
										codigoHTML: '',
										tipo: "enviar",
										tipoCliente: "campanha",
										idP: idPl,
										idS: idC,
										Campos: camposP,
										correo: $('[name="correo_electronico0"]').val(),
									}
									$("#frmPruebaEnvio").find(".camposP").find("input").each(function(){
										var nombre = $(this).attr("name");
										var valor =  $(this).val();
										camposP.push(valor);
										envio[nombre] = valor
									});
								}
								else{
									var ruta = "<?php echo URL::route('SysWebEmailMarketingPruebaEnvioCEnviar'); ?>";
									var envio = $("#frmPruebaEnvio").serialize();
								}
								$.post(
									ruta,
									envio,
									function(data){
										$("#btnEnviar").prop("disabled", false).html("Enviar");
										if(data.success){
											new PNotify({ title: "Felicidades!", text: data.mensaje, type: "success" });
											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"
								);
							}
						}]
					});
				}
				else{
					if(data.mensaje != ''){ new PNotify({ title: "Error", text: data.mensaje, type: "error" }); }
				}
			},
			"json"
		);
	}
	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='idC' 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('SysWebEmailMarketingCampanhasEliminar'); ?>",
						{idC: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 = $("#tblCampanhas").DataTable();
								table.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"
					);
				}
			}]
		});
	}
	function verVideoDCSS(){
		$("#btnPopoverAyuda").trigger("click");
	}
	function Actualizar(){
		var tabla = $("#tblCampanhas").DataTable();
		tabla.ajax.reload(null, false);
	}
	function Clonar(id){
		var id = id;
		$.isLoading({ text: "Cargando..." }); NProgress.start();
		$.post(
			"<?php echo URL::route('SysWebEmailMarketingCampanhasClonar'); ?>",
			{idC:id},
			function(data){
				$.isLoading("hide"); NProgress.done();
				if(data.success){
					new PNotify({ title: "Felicidades!", text: data.mensaje, type: "success" });
					Actualizar();
					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"
		);
	}
	<?php
		if( isset($_GET['import']) ){
	?>
	function ReemplazarCodigo(tipo, idP){
		var id = id;
		var mensaje = "<div class='row'> \
			<div class='col-xs-12'> \
				<form id='frmImportarPlantilla' enctype='multipart/form-data'> \
					<div class='form-group'> \
						<label for='flArchivo'>Archivo HTML</label> \
						<input type='file' id='flArchivo' name='flArchivo'> \
						<span class='text-danger' id='errflArchivo'></span> \
					</div> \
					<input type='hidden' name='hdnIdP' value='"+idP+"'> \
					<input type='hidden' name='hdnTipo' value='"+tipo+"'> \
				</form> \
			</div> \
		</div>";
		BootstrapDialog.show({
			title: "Reemplazar Plantilla de archivo HTML",
			type: BootstrapDialog.TYPE_DEFAULT,
			message: mensaje,
			draggable: true,
			cssClass: "ModalImportarPlantilla",
			buttons: [{
				id: "btnImportarPlantilla",
				label: "Reemplazar",
				cssClass: "btn-default",
				action: function(dialogItself){
					$.isLoading({ text: "Cargando..." }); NProgress.start();
					$.isLoading("hide"); NProgress.done();
					$("#btnImportarPlantilla").prop("disabled", true).html('<i class="fa fa-spinner fa-spin"></i> Reemplazando...');
					var formulario = $("#frmImportarPlantilla");
					var formData = new FormData(document.getElementById("frmImportarPlantilla"));
					$.ajax({
						type: "POST",
						url: "<?php echo URL::route('SysWebEmailMarketingPlantillasImportar', array()); ?>",
						data: formData,
						cache: false,
						contentType: false,
						processData: false,
						dataType: "json",
						timeout: 1800000,
						success: function(data){
							$("#btnImportarPlantilla").prop("disabled", false).html("Reemplazar");
							if(data.success){
								new PNotify({ title: "Realizado", text: data.mensaje, type: "success" });
								$(".porlets-content").find("table").each(function(e){
									var table = $(this).DataTable();
									table.ajax.reload(null, false);
								});
								dialogItself.close();
							}
							else{
								if(typeof data.MensajeError != "undefined"){ $("#Errs").html(data.MensajeError); $("#MensajeError").show(); }else{ $("#MensajeError").hide(); $("#Errs").html(""); }
								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){
							$("#btnImportarPlantilla").prop("disabled", false).html("Reemplazar");
							new PNotify({ title: "Error", text: textStatus + " " + errorThrown, type: "error" });
						}
					});
				}
			}]
		});
	}
	<?php
		}
	?>
</script>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('JSOr'); ?>
$("#tblCampanhas").DataTable({
	"processing": true,
	"serverSide": true,
	"ajax": "<?php echo URL::route('SysWebEmailMarketingCampanhas'); ?>",
	columns: [
		{data: "id", name: "id", "searchable": false, "visible": false},
		{data: "nombre", name: "campanhas.nombre"},
		{data: "asunto", name: "campanhas.asunto"},
		{data: "listas", name: "listas", "searchable": false, "orderable": false},
		{data: "etiquetas", name: "etiquetas", "searchable": false, "orderable": false},
		{data: "estado", name: "estado", "searchable": false, "orderable": false},
		{data: "acciones", name: "acciones", "searchable": false, "orderable": false, "class": "text-center"}
	],
	"language": {
		"url": "<?php echo asset('DataTables/Spanish.json'); ?>"
	},
	responsive: true,
	"fnDrawCallback": function( oSettings ) {
		$(".tooltips").tooltip();
	},
	"order": [[ 0, "desc" ]]
});
<?php if(Session::has('MensajeS')): ?>
new PNotify({ title: "Realizado", text: "<?php echo Session::get('MensajeS'); ?>", type: "success" });
<?php endif; ?>
<?php if(Session::has('MensajeE')): ?>
new PNotify({ title: "Realizado", text: "<?php echo Session::get('MensajeE'); ?>", type: "error" });
<?php endif; ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('BaseSistemaWeb', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>