<?php

$page = $_GET['page'];
$paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1;

$columns = array(
	'id' => 'id',
	'type' => 'type',
	'default' => 'id'
);

$orderby = isset($_GET['orderby']) && isset($columns[$_GET['orderby']]) ? $columns[$_GET['orderby']] : $columns['default'];
$order = isset($_GET['order']) && $_GET['order'] == 'desc' ? 'DESC' : 'ASC';

global $wpdb;

if ( isset( $_GET['delete'] ) ) {
	$wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}bwsurvey_notifications WHERE id = %d;", $_GET['delete']));
}

$per_page = 25;


if (isset($_GET['survey'])) {
	$item_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(id) FROM {$wpdb->prefix}bwsurvey_notifications WHERE survey_id = %d;", $_GET['survey']));
	$page_count = ceil($item_count / $per_page);

	$paged = isset($_GET['paged']) ? min($page_count, max(1, $paged)) : 1;

	$survey_title = $wpdb->get_var($wpdb->prepare("SELECT title FROM {$wpdb->prefix}bwsurvey_surveys WHERE id = %d;", $_GET['survey']));

	$results = $wpdb->get_results($wpdb->prepare("SELECT *
		FROM {$wpdb->prefix}bwsurvey_notifications
		WHERE survey_id = %d
		ORDER BY {$orderby} {$order}
		LIMIT %d OFFSET %d",
		$_GET['survey'],
		$per_page,
		$per_page * ($paged - 1)
	));
} else {
	$item_count = $wpdb->get_var("SELECT COUNT(id) FROM {$wpdb->prefix}bwsurvey_notifications;");
	$page_count = ceil($item_count / $per_page);

	$paged = isset($_GET['paged']) ? min($page_count, max(1, $paged)) : 1;

	$results = $wpdb->get_results($wpdb->prepare("SELECT notification.*, survey.title as survey
		FROM {$wpdb->prefix}bwsurvey_notifications as notification
			INNER JOIN {$wpdb->prefix}bwsurvey_surveys as survey ON notification.survey_id = survey.id
		ORDER BY {$orderby} {$order}
		LIMIT %d OFFSET %d",
		$per_page,
		$per_page * ($paged - 1)
	));
}

?>
<div class="wrap">
	<h1 class="wp-heading-inline">Notifications</h1>
	<hr class="wp-header-end">

	<?php if ( isset( $_GET['survey'] ) ) : ?>
		<a
			href="<?php echo add_query_arg(array(
				'page' => $page,
				'edit' => '',
				'survey' => $_GET['survey']
			), admin_url('admin.php')); ?>"
			aria-label="Create Notification"
			class="button button-primary"
		>Create Notification</a>
	<?php endif; ?>
	
	<div>
		<div class="tablenav top">
			<div class="tablenav-pages <?php if ($page_count == 1) echo 'one-page'; ?>">
				<span class="displaying-num"><?php echo $item_count; ?> <?php echo $item_count == 1 ? 'item' : 'items'; ?></span>
				<span class="pagination-links">
					<?php if ($paged > 1): ?>
						<a class="tablenav-pages-navspan button" href="<?php echo add_query_arg('paged', 1); ?>" aria-hidden="true">«</a>
						<a class="tablenav-pages-navspan button" href="<?php echo add_query_arg('paged', $paged - 1); ?>" aria-hidden="true">‹</a>
					<?php else: ?>
						<span class="tablenav-pages-navspan button disabled" aria-hidden="true">«</span>
						<span class="tablenav-pages-navspan button disabled" aria-hidden="true">‹</span>
					<?php endif; ?>
					<span class="paging-input">
						<span class="tablenav-paging-text"><?php echo $paged; ?> of <span class="total-pages"><?php echo $page_count; ?></span>
						</span>
					</span>
					<?php if ($paged < $page_count): ?>
						<a class="tablenav-pages-navspan button" href="<?php echo add_query_arg('paged', $paged + 1); ?>" aria-hidden="true">›</a>
						<a class="tablenav-pages-navspan button" href="<?php echo add_query_arg('paged', $page_count); ?>" aria-hidden="true">»</a>
					<?php else: ?>
						<span class="tablenav-pages-navspan button disabled" aria-hidden="true">›</span>
						<span class="tablenav-pages-navspan button disabled" aria-hidden="true">»</span>
					<?php endif; ?>
				</span>
			</div>
			<br class="clear">
		</div>
		<h2 class="screen-reader-text">Survey Pages list</h2>
		<table class="wp-list-table widefat fixed striped table-view-list posts">
			<thead>
				<tr>
					<th scope="col" class="manage-column column-id">Name</th>
					<th scope="col" class="manage-column column-primary sortable <?php echo $orderby == 'type' && $order == 'ASC' ? 'desc' : 'asc'; ?>">
						<a href="<?php echo add_query_arg(array(
							'orderby' => 'type',
							'order' => $orderby == 'type' && $order == 'ASC' ? 'desc' : 'asc'
						)); ?>">
							<span>Type</span>
							<span class="sorting-indicator"></span>
						</a>
					</th>
					<th scope="col" class="manage-column column-id">Survey</th>
					<th scope="col" class="manage-column column-id">To</th>
				</tr>
			</thead>
			<tbody id="the-list">
				<?php foreach ($results as $result) : ?>
					<?php
						if (isset($result->survey)) $survey_title = $result->survey;
						$data = unserialize($result->data);
						$length = empty($data) ? 0 : count($data);
					?>
					<tr id="post-5" class="iedit author-self level-0 post-5 type-survey status-publish hentry">
						<td>
							<strong>
								<a class="row-title" href="<?php echo add_query_arg(array(
									'page' => $page,
									'edit' => '',
									'survey' => $result->survey_id,
									'notification' => $result->id
								), admin_url('admin.php')); ?>" aria-label="“<?php echo $result->id; ?>” (Edit)"><?php if ( isset( $data['name'] ) ) echo $data['name']; ?></a>
							</strong>
							<div class="row-actions">
								<span class="edit">
									<a href="<?php echo add_query_arg(array(
										'page' => $page,
										'edit' => '',
										'survey' => $result->survey_id,
										'notification' => $result->id
									), admin_url('admin.php')); ?>" aria-label="Edit “<?php echo $result->id; ?>”">Edit</a> | </span>
								<span class="delete">
									<a href="<?php echo add_query_arg(
											isset($_GET['survey']) ? array(
												'page' => $page,
												'survey' => $_GET['survey'],
												'delete' => $result->id
											) : array(
												'page' => $page,
												'delete' => $result->id
											),
											admin_url('admin.php')
									); ?>" class="submitdelete" aria-label="Delete “<?php echo $result->id; ?>”">Delete</a>
								</span>
							</div>
						</td>
						<td>
							<strong>
								<a class="row-title" href="<?php echo add_query_arg(array(
									'page' => $page,
									'edit' => '',
									'survey' => $result->survey_id,
									'notification' => $result->id
								), admin_url('admin.php')); ?>" aria-label="“<?php echo $result->id; ?>” (Edit)"><?php echo $result->type; ?></a>
							</strong>
						</td>
						<td><?php echo $survey_title; ?></td>
						<td>
							<?php if ( ! empty( $data['to'] ) ) foreach ($data['to'] as $to) echo "<span class=\"bwto\">$to</span>"; ?>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
			<tfoot>
				<tr>
					<th scope="col" class="manage-column column-id">Name</th>
					<th scope="col" class="manage-column column-primary sortable <?php echo $orderby == 'type' && $order == 'ASC' ? 'desc' : 'asc'; ?>">
						<a href="<?php echo add_query_arg(array(
							'orderby' => 'type',
							'order' => $orderby == 'type' && $order == 'ASC' ? 'desc' : 'asc'
						)); ?>">
							<span>Type</span>
							<span class="sorting-indicator"></span>
						</a>
					</th>
					<th scope="col" class="manage-column column-id">Survey</th>
					<th scope="col" class="manage-column column-id">To</th>
				</tr>
			</tfoot>
		</table>
		<div class="tablenav bottom">
			<div class="tablenav-pages <?php if ($page_count == 1) echo 'one-page'; ?>">
				<span class="displaying-num"><?php echo $item_count; ?> <?php echo $item_count == 1 ? 'item' : 'items'; ?></span>
				<span class="pagination-links">
					<?php if ($paged > 1): ?>
						<a class="tablenav-pages-navspan button" href="<?php echo add_query_arg('paged', 1); ?>" aria-hidden="true">«</a>
						<a class="tablenav-pages-navspan button" href="<?php echo add_query_arg('paged', $paged - 1); ?>" aria-hidden="true">‹</a>
					<?php else: ?>
						<span class="tablenav-pages-navspan button disabled" aria-hidden="true">«</span>
						<span class="tablenav-pages-navspan button disabled" aria-hidden="true">‹</span>
					<?php endif; ?>
					<span class="paging-input">
						<span class="tablenav-paging-text"><?php echo $paged; ?> of <span class="total-pages"><?php echo $page_count; ?></span>
						</span>
					</span>
					<?php if ($paged < $page_count): ?>
						<a class="tablenav-pages-navspan button" href="<?php echo add_query_arg('paged', $paged + 1); ?>" aria-hidden="true">›</a>
						<a class="tablenav-pages-navspan button" href="<?php echo add_query_arg('paged', $page_count); ?>" aria-hidden="true">»</a>
					<?php else: ?>
						<span class="tablenav-pages-navspan button disabled" aria-hidden="true">›</span>
						<span class="tablenav-pages-navspan button disabled" aria-hidden="true">»</span>
					<?php endif; ?>
				</span>
			</div>
			<br class="clear">
		</div>
	</div>
	<div class="clear"></div>
	<style>
		span.bwto {
			padding: 4px;
			white-space: nowrap;
		}
		span.bwto:not(:last-child):after {
			content: ','
		}
	</style>
</div>