<?php include('includes/header.php');?>
<?php include('includes/login/auth.php');?>
<?php include('includes/list/main.php');?>
<?php include('includes/helpers/short.php');?>
<?php include('includes/helpers/preferences.php');?>
<?php
	if(get_app_info('is_sub_user')) 
	{
		if(get_app_info('app')!=get_app_info('restricted_to_app'))
		{
			echo '<script type="text/javascript">window.location="'.addslashes(get_app_info('path')).'/list?i='.get_app_info('restricted_to_app').'"</script>';
			exit;
		}
		else if(get_app_info('campaigns_only')==1 && get_app_info('templates_only')==1 && get_app_info('lists_only')==1 && get_app_info('reports_only')==1)
		{
			echo '<script type="text/javascript">window.location="'.addslashes(get_app_info('path')).'/logout"</script>';
			exit;
		}
		else if(get_app_info('lists_only')==1)
		{
			go_to_next_allowed_section();
		}
	}
?>

<?php 
	//Get sorting preference and whether to show hidden lists
	$q = 'SELECT templates_lists_sorting, hide_lists, opt_in FROM apps WHERE id = '.get_app_info('app');
	$r = mysqli_query($mysqli, $q);
	if ($r && mysqli_num_rows($r) > 0) 
	{
		while($row = mysqli_fetch_array($r)) 
		{
			$templates_lists_sorting = $row['templates_lists_sorting'];
			$hide_lists = $row['hide_lists'];
			$opt_in = $row['opt_in'];
		}
	}
	$sortby = $templates_lists_sorting=='date' ? 'id DESC' : 'name ASC';
	$show_hidden_lists = $hide_lists == 0 ? '' : 'AND hide = 0';

	//Manage subscription modal always uses every unhidden list, independent of pagination.
	$manage_subscription_lists = array();
	$q = 'SELECT id, name, preference_visible, preference_name, preference_description, preference_sort FROM lists WHERE app = '.get_app_info('app').' AND userID = '.get_app_info('main_userID').' AND hide = 0 ORDER BY preference_sort ASC, '.$sortby;
	$r = mysqli_query($mysqli, $q);
	if($r)
	{
		while($row = mysqli_fetch_assoc($r))
		{
			$manage_subscription_lists[(int)$row['id']] = array(
				'id' => (int)$row['id'],
				'name' => stripslashes($row['name']),
				'visible' => (int)$row['preference_visible'],
				'public_name' => stripslashes(isset($row['preference_name']) ? $row['preference_name'] : ''),
				'description' => stripslashes(isset($row['preference_description']) ? $row['preference_description'] : ''),
				'sort' => (int)$row['preference_sort']
			);
		}
	}
	$manage_subscription_preview_url = sendy_preferences_url(get_app_info('path'), get_app_info('app'), 'subscriber@example.com').'&preview=1';
?>

<link href="<?php echo get_app_info('path');?>/css<?php echo get_app_info('dark_mode') ? '/dark' : '';?>/tablesorter.css?30" rel="stylesheet">
<script type="text/javascript" src="<?php echo get_app_info('path');?>/js/tablesorter/jquery.tablesorter.min.js"></script>
<script type="text/javascript" src="<?php echo get_app_info('path');?>/js/tablesorter/jquery.tablesorter.widgets.min.js"></script>
<script type="text/javascript" src="<?php echo get_app_info('path');?>/js/validate.js"></script>
<style type="text/css">
	.lists-actions-toolbar {
		margin-bottom: 12px;
	}
	.lists-actions-primary {
		display: flex;
		align-items: center;
		gap: 8px;
	}
	.lists-actions-primary .btn {
		white-space: nowrap;
	}
	#list-form + br {
		display: none;
	}
	#list-form {
		margin-bottom: 12px;
	}
	#manage-subscription-modal {
		width: 780px;
		margin-left: -390px;
	}
	.manage-subscription-layout {
		display: flex;
		gap: 22px;
		min-height: 330px;
		padding-top: 18px;
		border-top: 1px solid rgba(128,128,128,.25);
	}
	.manage-subscription-lists {
		width: 42%;
		padding-right: 22px;
		border-right: 1px solid rgba(128,128,128,.25);
	}
	#manage-subscription-sortable {
		list-style: none;
		margin: 0;
	}
	.manage-subscription-item {
		display: flex;
		align-items: center;
		box-sizing: border-box;
		margin-bottom: 5px;
		padding: 9px 10px;
		border: 1px solid rgba(128,128,128,.25);
		border-radius: 4px;
		cursor: pointer;
	}
	.manage-subscription-item.active {
		border-color: #8b5cf6;
		background: rgba(139,92,246,.11);
		box-shadow: 0 0 0 1px rgba(139,92,246,.12);
	}
	.manage-subscription-handle {
		margin-right: 9px;
		cursor: move;
		opacity: .65;
	}
	.manage-subscription-checkbox {
		margin: 0 9px 0 0 !important;
	}
	.manage-subscription-list-name {
		min-width: 0;
		line-height: 18px;
		word-wrap: break-word;
	}
	.manage-subscription-editor {
		flex: 1;
		min-width: 0;
	}
	#manage-subscription-selected-name {
		margin: 0 0 18px;
		font-size: 15px;
		font-weight: 700;
		line-height: 18px;
	}
	.manage-subscription-editor input,
	.manage-subscription-editor textarea {
		box-sizing: border-box;
		width: 100%;
		min-height: 30px;
	}
	.manage-subscription-editor textarea {
		min-height: 125px;
		resize: vertical;
	}
	#manage-subscription-status {
		display: none;
		margin: 0 0 15px;
	}
	@media (max-width: 767px) {
		.lists-actions-primary {
			align-items: stretch;
			flex-direction: column;
		}
		.lists-actions-primary .btn {
			box-sizing: border-box;
			width: 100%;
		}
		#manage-subscription-modal {
			position: fixed;
			top: 10px !important;
			bottom: 10px;
			left: 10px;
			right: 10px;
			width: auto;
			margin-left: 0;
			overflow-y: auto;
		}
		#manage-subscription-modal .modal-body {
			max-height: none;
			overflow: visible;
		}
		.manage-subscription-layout {
			display: block;
		}
		.manage-subscription-lists {
			box-sizing: border-box;
			width: 100%;
			margin-bottom: 20px;
			padding-right: 0;
			padding-bottom: 15px;
			border-right: 0;
			border-bottom: 1px solid rgba(128,128,128,.25);
		}
		#list-form {
			margin: 0;
		}
		#list-form[style*="display: none"] {
			padding: 0;
			border: 0;
		}
		#list-form + br {
			display: none;
		}
		#list-form + br + .table-wrapper,
		#list-form + br + table,
		#list-form + br + .table-wrapper table {
			margin-top: 0 !important;
		}
	}
</style>
<script type="text/javascript">
	$(document).ready(function() {
		$('table').tablesorter({
			widgets        : ['saveSort'],
			usNumberFormat : true,
			sortReset      : true,
			sortRestart    : true,
			headers: { 0: { sorter: false}, 8: {sorter: false}, 9: {sorter: false}, 10: {sorter: false} }	
		});
		$("#new-list-btn, #add-one-btn").click(function(){
			$("#list-form").slideDown("fast");
			$("#list_name").focus();
			$("#list-form").validate({
				rules: {
					list_name: {
						required: true	
					}
				},
				messages: {
					list_name: "<?php echo addslashes(_('List name is required'));?>"
				}
			});
		});
		$("#close-add-list-btn").click(function(){
			$("#list-form").slideUp("fast");
		});

		var manageSubscriptionSettings = <?php echo json_encode($manage_subscription_lists, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);?>;
		var selectedManageSubscriptionList = null;
		var manageSubscriptionOrderChanged = false;

		function saveCurrentManageSubscriptionFields()
		{
			if(selectedManageSubscriptionList === null || !manageSubscriptionSettings[selectedManageSubscriptionList]) return;
			manageSubscriptionSettings[selectedManageSubscriptionList].public_name = $("#manage-subscription-public-name").val();
			manageSubscriptionSettings[selectedManageSubscriptionList].description = $("#manage-subscription-description").val();
		}

		function selectManageSubscriptionList(id)
		{
			id = String(id);
			if(!manageSubscriptionSettings[id]) return;
			saveCurrentManageSubscriptionFields();
			selectedManageSubscriptionList = id;
			$(".manage-subscription-item").removeClass("active");
			$(".manage-subscription-item[data-id='"+id+"']").addClass("active");
			$("#manage-subscription-selected-name").text(manageSubscriptionSettings[id].name);
			$("#manage-subscription-public-name").val(manageSubscriptionSettings[id].public_name);
			$("#manage-subscription-description").val(manageSubscriptionSettings[id].description);
			$("#manage-subscription-fields").show();
			$("#manage-subscription-empty-editor").hide();
		}

		$("#manage-subscription-sortable").sortable({
			handle: ".manage-subscription-handle",
			axis: "y",
			containment: "parent",
			start: saveCurrentManageSubscriptionFields,
			update: function(){
				manageSubscriptionOrderChanged = true;
				$(".manage-subscription-item").each(function(index){
					manageSubscriptionSettings[String($(this).attr("data-id"))].sort = index;
				});
			}
		});

		$(".manage-subscription-item").click(function(e){
			if($(e.target).hasClass("manage-subscription-handle") || $(e.target).parent().hasClass("manage-subscription-handle")) return;
			selectManageSubscriptionList($(this).attr("data-id"));
		});
		$(".manage-subscription-checkbox").change(function(){
			var id = String($(this).closest(".manage-subscription-item").attr("data-id"));
			manageSubscriptionSettings[id].visible = $(this).is(":checked") ? 1 : 0;
			selectManageSubscriptionList(id);
		});
		$("#manage-subscription-modal").on("shown", function(){
			if(selectedManageSubscriptionList === null)
			{
				var first = $(".manage-subscription-item:first");
				if(first.length) selectManageSubscriptionList(first.attr("data-id"));
			}
		});

		$("#save-manage-subscription").click(function(e){
			e.preventDefault();
			saveCurrentManageSubscriptionFields();
			var payload = [];
			$(".manage-subscription-item").each(function(index){
				var id = String($(this).attr("data-id"));
				var item = manageSubscriptionSettings[id];
				payload.push({id: parseInt(id, 10), visible: item.visible, name: item.public_name, description: item.description, sort: item.sort});
			});

			var button = $(this);
			button.attr("disabled", "disabled");
			$("#manage-subscription-status").removeClass("alert-success alert-error").hide();
			$.ajax({
				url: "includes/list/save-preferences.php",
				type: "POST",
				dataType: "json",
				data: {app: <?php echo (int)get_app_info('app');?>, lists: JSON.stringify(payload)},
				success: function(response){
					if(response && response.success)
					{
						manageSubscriptionOrderChanged = false;
						$("#manage-subscription-status").addClass("alert-success").text("<?php echo addslashes(_('Settings saved.'));?>").show();
					}
					else
						$("#manage-subscription-status").addClass("alert-error").text(response && response.message ? response.message : "<?php echo addslashes(_('Unable to save settings.'));?>").show();
				},
				error: function(xhr){
					var response = xhr.responseJSON;
					$("#manage-subscription-status").addClass("alert-error").text(response && response.message ? response.message : "<?php echo addslashes(_('Unable to save settings. Please try again.'));?>").show();
				},
				complete: function(){ button.removeAttr("disabled"); }
			});
		});
	});
</script>
<div class="row-fluid">
    <div class="span2">
        <?php include('includes/sidebar.php');?>
    </div> 
    <div class="span10">
    	<div>
	    	<p class="lead">
	    	<?php echo sendy_brand_logo_img();?>
		    	<?php if(get_app_info('is_sub_user')):?>
			    	<?php echo get_app_data('app_name');?>
		    	<?php else:?>
			    	<a href="<?php echo get_app_info('path'); ?>/edit-brand?i=<?php echo get_app_info('app');?>" data-placement="right" title="<?php echo _('Edit brand settings');?>"><?php echo get_app_data('app_name');?> <span class="icon icon-pencil top-brand-pencil"></span></a>
		    	<?php endif;?>
		    </p>
    	</div>
    	<h2><?php echo _('Subscriber lists');?></h2><br/>
    	
		<div class="page-actions-toolbar lists-actions-toolbar">
			<div class="page-actions-primary lists-actions-primary">
				<button class="btn" id="new-list-btn"><i class="icon-plus-sign"></i> <?php echo _('Add a new list');?></button>
				<?php if(count($manage_subscription_lists)):?>
				<button type="button" class="btn" data-target="#manage-subscription-modal" data-toggle="modal"><i class="icon-envelope"></i> <?php echo _('Manage subscription');?></button>
				<?php endif;?>
			</div>
	    	
			<form class="form-search page-actions-search" action="<?php echo get_app_info('path');?>/search-all-lists" method="GET">
	    		<input type="hidden" name="i" value="<?php echo get_app_info('app');?>">
				<input type="text" class="input-medium search-query" name="s" style="width: 200px;">
				<button type="submit" class="btn"><i class="icon-search"></i> <?php echo _('Search all lists');?></button>
			</form>
		</div>
		
		<form action="<?php echo get_app_info('path')?>/includes/subscribers/import-add.php" method="POST" accept-charset="utf-8" class="form-vertical well" enctype="multipart/form-data" id="list-form">
	    	
	    	<label class="control-label" for="list_name"><?php echo _('List name');?></label>
	    	<div class="control-group">
		    	<div class="controls">
	              <input type="text" class="input-xlarge" id="list_name" name="list_name" placeholder="<?php echo _('The name of your new list');?>">
	            </div>
				<div class="btn-group" data-toggle="buttons-radio" style="margin-top: 5px;">
				  <a href="javascript:void(0)" title="" class="btn" id="single"><i class="icon icon-angle-right"></i> <?php echo _('Single Opt-In');?></a>
				  <a href="javascript:void(0)" title="" class="btn" id="double"><i class="icon icon-double-angle-right"></i> <?php echo _('Double Opt-In');?></a>
				</div>
				<script type="text/javascript">
					$(document).ready(function() {
						<?php 
							if($opt_in==0):
						?>
						$("#single").button('toggle');
						$("#opt_in").val("0");
						<?php else:?>
						$("#double").button('toggle');
						$("#opt_in").val("1");
						<?php endif;?>
						
						$("#single").click(function(){
							$("#opt_in").val("0");
						});
						$("#double").click(function(){
							$("#opt_in").val("1");
						});
					});
				</script>
	        </div>
	        
			<input type="hidden" name="opt_in" id="opt_in" value="">
	        <input type="hidden" name="app" value="<?php echo get_app_info('app');?>">
			
			<br/>
	        
	        <button type="submit" class="btn btn-inverse"><i class="icon icon-plus"></i> <?php echo _('Add');?></button> 
	        <a href="javascript:void(0)" id="close-add-list-btn"><span class="icon icon-remove-sign"></span> <?php echo _('Cancel');?></a>
	    </form>
		
		<br/>
		
		<?php $has_gdpr_subscribers = has_gdpr_subscribers();?>
    	
	    <table class="table table-striped responsive">
		  <thead>
		    <tr>
		      <th><?php echo _('ID');?></th>
		      <th><?php echo _('List');?></th>
		      <th><?php echo _('Active');?></th>
		      <?php if($has_gdpr_subscribers):?>
		      <th><?php echo _('GDPR');?></th>
		      <?php endif;?>
		      <th title="Segments"><?php echo _('Segs');?></th>
		      <th title="Autoresponders"><?php echo _('ARs');?></th>
		      <th><?php echo _('Unsubscribed');?></th>
		      <th><?php echo _('Bounced');?></th>
		      <th><?php echo _('Hide');?></th>
		      <th><?php echo _('Edit');?></th>
		      <th><?php echo _('Delete');?></th>
		    </tr>
		  </thead>
		  <tbody>
		  	
		  	<!-- Auto select encrypted listID -->
		  	<script type="text/javascript">
		  		$(document).ready(function() {
					$(".encrypted-list-id").mouseover(function(){
						$(this).selectText();
					});
				});
			</script>
		  	
		  	<?php 
			  	$limit = get_app_data('campaign_report_rows');
					$total_lists = totals(get_app_info('app'));
					$total_pages = ceil($total_lists/$limit);
					$p = sendy_current_page($total_pages);
					$offset = ($p-1) * $limit;

			  	$q = 'SELECT id, name, hide FROM lists WHERE app = '.get_app_info('app').' AND userID = '.get_app_info('main_userID').' '.$show_hidden_lists.' ORDER BY '.$sortby.' LIMIT '.$offset.','.$limit;
			  	$r = mysqli_query($mysqli, $q);
				$list_rows = array();
				$list_ids = array();
			  	if ($r && mysqli_num_rows($r) > 0)
			  	{
			  	    while($row = mysqli_fetch_array($r))
			  	    {
						$list_rows[] = $row;
						$list_ids[] = (int)$row['id'];
					}
				}
				$list_subscriber_counts = get_list_subscriber_counts($list_ids);
				if (count($list_rows) > 0)
				{
					foreach($list_rows as $row)
					{
			  			$id = $row['id'];
			  			$name = stripslashes($row['name']);
			  			$hidden = stripslashes($row['hide']);
						$subscribers_count = $list_subscriber_counts[$id]['active'];
						$unsubscribers_count = $list_subscriber_counts[$id]['unsubscribed'];
						$bounces_count = $list_subscriber_counts[$id]['bounced'];
			  			if(strlen(encrypt_val($id))>5) $listid = substr(encrypt_val($id), 0, 5).'..';
			  			else $listid = encrypt_val($id);
			  			
			  			$us_percentage = get_unsubscribers_percentage($subscribers_count, $unsubscribers_count);
			  			
			  			$b_percentage = get_bounced_percentage($bounces_count, $subscribers_count);
			  			
			  			$no_of_segs = get_segment_count($id);
			  			$seg_label = $no_of_segs>0 ? 'label-info' : 'label';
			  			$seg_count = $no_of_segs>0 ? '<a href="'.get_app_info('path').'/segments-list?i='.get_app_info('app').'&l='.$id.'" style="color:white;" title="'._('View segments for this list').'">'.$no_of_segs.'</a>' : '<a href="'.get_app_info('path').'/segments-list?i='.get_app_info('app').'&l='.$id.'" title="'._('Create segments for this list').'">'.$no_of_segs.'</a>';
			  			
			  			$no_of_ars = get_ar_count($id);
			  			$ar_label = $no_of_ars>0 ? 'label-info' : 'label';
			  			$ar_count = $no_of_ars>0 ? '<a href="'.get_app_info('path').'/autoresponders-list?i='.get_app_info('app').'&l='.$id.'" style="color:white;" title="'._('View autoresponders for this list').'">'.$no_of_ars.'</a>' : '<a href="'.get_app_info('path').'/autoresponders-list?i='.get_app_info('app').'&l='.$id.'" title="'._('Create autoresponders for this list').'">'.$no_of_ars.'</a>';
			  			
						$is_hidden = $hidden ? '.5' : '1';
						$icon_eye = $hidden ? 'icon-eye-open' : 'icon-eye-close';
						$hide_unhide_title = $hidden ? _('Unhide this list') : _('Hide this list');
						$to_hide = $hidden ? 0 : 1;
						  
			  			echo '
			  			
			  			<tr id="'.$id.'" style="opacity: '.$is_hidden.'">
			  			  <td><span class="label" id="list'.$id.'">'.$listid.'</span><span class="label encrypted-list-id" id="list'.$id.'-encrypted" style="display:none;">'.encrypt_val($id).'</span></td>
					      <td><a href="'.get_app_info('path').'/subscribers?i='.get_app_info('app').'&l='.$id.'" title="">'.$name.'</a></td>
					      <td id="progress'.$id.'"><span class="badge badge-success">'.$subscribers_count.'</span></td>';
					      
					    if($has_gdpr_subscribers)
					    {
						    $gdpr_count = $list_subscriber_counts[$id]['gdpr'];
						    $gdpr_subscribers = str_replace(',', '', $gdpr_count);
						    $gdpr_total_subscribers = str_replace(',', '', $subscribers_count);
						    $gdpr_subscribers = !is_numeric($gdpr_subscribers) ? 0 : $gdpr_subscribers;
						    $gdpr_total_subscribers = !is_numeric($gdpr_total_subscribers) ? 0 : $gdpr_total_subscribers;
						    $gdpr_percentage = $gdpr_subscribers==0 || $gdpr_total_subscribers==0 ? 0 : round($gdpr_subscribers / $gdpr_total_subscribers * 100, 2);
						    echo '<td><span class="label label-warning">'.$gdpr_percentage.'%</span> '.$gdpr_count.'</td>';
						}
					      
					    echo '
					      <td><span class="label '.$seg_label.'">'.$seg_count.'</span></td>
					      <td><span class="label '.$ar_label.'">'.$ar_count.'</span></td>
					      <td><span class="label">'.$us_percentage.'%</span> '.$unsubscribers_count.'</td>
					      <td><span class="label">'.$b_percentage.'%</span> '.$bounces_count.'</td>
					      <td><a href="includes/list/hide-list.php" id="hide-btn-'.$id.'" title="'.$hide_unhide_title.'" data-l="'.$id.'" data-hide="'.$to_hide.'"><i class="icon '.$icon_eye.'"></i></a></td>
					      <td><a href="edit-list?i='.get_app_info('app').'&l='.$id.'" title="'._('List settings').'"><i class="icon icon-pencil"></i></a></td>
					      <td><a href="#delete-list" title="'._('Delete').' '.$name.'" id="delete-btn-'.$id.'" data-toggle="modal"><span class="icon icon-trash"></span></a></td>
					      <script type="text/javascript">
						  	$("#hide-btn-'.$id.'").click(function(e){
								e.preventDefault(); 
								$.post($(this).attr("href"), {l:$(this).data("l"), hide:$(this).attr("data-hide")},
									function(data) 
									{
										if(data==1) 
										{
											$("#'.$id.'").css("opacity", "0.5");
											$("#hide-btn-'.$id.'").html("<i class=\"icon icon-eye-open\"></i>");
											$("#hide-btn-'.$id.'").attr("title", "'._('Unhide this list').'");
											$("#hide-btn-'.$id.'").attr("data-hide", 0);
											to_hide = 0;
										}
										else 
										{
											$("#'.$id.'").css("opacity", "1");
											$("#hide-btn-'.$id.'").html("<i class=\"icon icon-eye-close\"></i>");
											$("#hide-btn-'.$id.'").attr("title", "'._('Hide this list').'");
											$("#hide-btn-'.$id.'").attr("data-hide", 1);
											to_hide = 1;
										}
									}
								);
							});
					        $("#delete-btn-'.$id.'").click(function(e){
								e.preventDefault(); 
								$("#delete-list-btn").attr("data-id", '.$id.');
								$("#list-to-delete").text("'.$name.'");
								$("#delete-text").val("");
							});
							$("#list'.$id.'").mouseover(function(){
								$("#list'.$id.'-encrypted").show();
								$(this).hide();
							});
							$("#list'.$id.'-encrypted").mouseout(function(){
								$(this).hide();
								$("#list'.$id.'").show();
							});
							</script>
					    </tr>
			  			';
					}
			  	}
			  	else
			  	{
				  	echo '
				  		<tr>
				  			<td>'._('No list yet.').' <a href="javascript:void(0)" title="" id="add-one-btn" style="text-decoration: underline;">'._('Add one').'</a>!</td>
				  			<td></td>
				  			<td></td>
				  			<td></td>
				  			<td></td>
				  			<td></td>
				  			<td></td>
				  			<td></td>
				  		</tr>
				  	';
			  	}
		  	?>
		    
		  </tbody>
		</table>

		<!-- Manage subscription -->
		<div id="manage-subscription-modal" class="modal hide fade">
		  <div class="modal-header">
		    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
		    <h3><i class="icon-envelope"></i> <?php echo _('Manage subscription');?></h3>
		  </div>
		  <div class="modal-body">
		    <p class="muted"><?php echo _("Choose which lists subscribers can see and manage on 'Manage subscription' page. You can customize each list’s public name and description, then drag the lists into the order subscribers will see.");?></p>
		    <div id="manage-subscription-status" class="alert"></div>
		    <div class="manage-subscription-layout">
		      <div class="manage-subscription-lists">
		        <p><strong><?php echo _('Lists');?></strong><br><small><?php echo _('Select lists to show and drag them into your desired order.');?></small></p>
		        <?php if(count($manage_subscription_lists)):?>
		        <ul id="manage-subscription-sortable">
		          <?php foreach($manage_subscription_lists as $manage_list):?>
		          <li class="manage-subscription-item" data-id="<?php echo $manage_list['id'];?>">
		            <span class="manage-subscription-handle" title="<?php echo _('Drag to reorder');?>"><i class="icon-reorder"></i></span>
		            <input type="checkbox" class="manage-subscription-checkbox" <?php if($manage_list['visible']) echo 'checked';?>>
		            <span class="manage-subscription-list-name"><?php echo htmlspecialchars($manage_list['name'], ENT_QUOTES, 'UTF-8');?></span>
		          </li>
		          <?php endforeach;?>
		        </ul>
		        <?php else:?>
		        <p class="muted"><?php echo _('There are no unhidden lists to configure.');?></p>
		        <?php endif;?>
		      </div>
		      <div class="manage-subscription-editor">
		        <p id="manage-subscription-empty-editor" class="muted"><?php echo _('Select a list to edit its public details.');?></p>
		        <div id="manage-subscription-fields" style="display:none;">
		          <h4 id="manage-subscription-selected-name"></h4>
		          <label for="manage-subscription-public-name"><strong><?php echo _('Public list name');?></strong> (<?php echo _('optional');?>)</label>
		          <input type="text" id="manage-subscription-public-name" maxlength="150">
		          <label for="manage-subscription-description" style="margin-top:16px;"><strong><?php echo _('Public description');?></strong> (<?php echo _('optional');?>)</label>
		          <textarea id="manage-subscription-description"></textarea>
		        </div>
		      </div>
		    </div>
		  </div>
		  <div class="modal-footer">
		    <button type="button" class="btn" data-dismiss="modal"><?php echo _('Cancel');?></button>
		    <a class="btn" href="<?php echo htmlspecialchars($manage_subscription_preview_url, ENT_QUOTES, 'UTF-8');?>" target="_blank"><i class="icon-eye-open"></i> <?php echo _('Preview');?></a>
		    <button type="button" id="save-manage-subscription" class="btn btn-primary" <?php if(!count($manage_subscription_lists)) echo 'disabled';?>><?php echo _('Save settings');?></button>
		  </div>
		</div>
		
		<!-- Delete -->
		<div id="delete-list" class="modal hide fade">
		  <div class="modal-header">
		    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
		    <h3><?php echo _('Delete list');?></h3>
		  </div>
		  <div class="modal-body">
		    <p><?php echo _('All subscribers, custom fields, autoresponders and segments in this list will be permanently deleted. Confirm delete <span id="list-to-delete" style="font-weight:bold;"></span>?');?></p>
		  </div>
		  <div class="modal-footer">
			<?php if(get_app_info('strict_delete')):?>
			<input autocomplete="off" type="text" class="input-large" id="delete-text" name="delete-text" placeholder="<?php echo _('Type the word');?> DELETE" style="margin: -2px 7px 0 0;"/>
			<?php endif;?>
			
		    <a href="javascript:void(0)" id="delete-list-btn" data-id="" class="btn btn-primary"><?php echo _('Delete');?></a>
		  </div>
		</div>
		
		<script type="text/javascript">
			$("#delete-list-btn").click(function(e){
				e.preventDefault(); 
				
				<?php if(get_app_info('strict_delete')):?>
				if($("#delete-text").val()=='DELETE'){
				<?php endif;?>
				
					$.post("includes/list/delete.php", { list_id: $(this).attr("data-id") },
					  function(data) {
					      if(data)
					      {
					        $("#delete-list").modal('hide');
					        $("#"+$("#delete-list-btn").attr("data-id")).fadeOut(); 
					      }
					      else alert("<?php echo _('Sorry, unable to delete. Please try again later!')?>");
					  }
					);
				
				<?php if(get_app_info('strict_delete')):?>
				}
				else alert("<?php echo _('Type the word');?> DELETE");
				<?php endif;?>
			});
		</script>
		
		<?php pagination($limit); ?>	
		
		<?php 
			//Show hidden lists button
			$q = 'SELECT hide FROM lists WHERE app = '.get_app_info('app').' AND hide = 1';
			$r = mysqli_query($mysqli, $q);
			if ($r && mysqli_num_rows($r) > 0)
			{
				$no_of_hidden_lists = mysqli_num_rows($r);
			}
			
			//If there are hidden lists
			if($no_of_hidden_lists > 0)
			{
				//Hidden lists are being shown, ask if user wants to hide them.
				if(!$hide_lists)
					echo '<p style="font-size: 12px; opacity: .8; margin-top: 20px;"><a href="'.get_app_info('path').'/includes/list/show-hide-lists.php?hide=1&app='.get_app_info('app').'"><span class="icon icon-eye-close"></span> '._('Hide hidden lists?').'</a></p>';
				else
					echo '<p style="font-size: 12px; opacity: .8; margin-top: 20px;"><a href="'.get_app_info('path').'/includes/list/show-hide-lists.php?hide=0&app='.get_app_info('app').'"><span class="icon icon-eye-open"></span> '._('Show hidden lists?').'</a></p>';
			}
		?>
    </div>   
</div>
<?php include('includes/footer.php');?>
