<?php

$page = $_GET['page'];
$paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1;

$columns = array(
	'id' => 'id',
	'title' => 'title',
	'default' => 'title'

);

$orderby = isset($_GET['orderby']) && isset($columns[$_GET['orderby']]) ? $columns[$_GET['orderby']] : $columns['default'];
$order = isset($_GET['order']) && $_GET['order'] == 'desc' ? 'DESC' : 'ASC';


global $wpdb;

$per_page = 25;
$item_count = $wpdb->get_var("SELECT COUNT(id) FROM {$wpdb->prefix}bwsurvey_surveys;");
$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 id, title FROM {$wpdb->prefix}bwsurvey_surveys
	ORDER BY {$orderby} {$order}
	LIMIT %d OFFSET %d",
	$per_page,
	$per_page * ($paged - 1)
));

?>
<div class="wrap">
	<h1 class="wp-heading-inline">Surveys</h1>
	<a href="<?php echo add_query_arg(array(
		'page' => 'bw-surveys-new-survey'
	), admin_url('admin.php')); ?>" class="page-title-action">Add New</a>
	<hr class="wp-header-end">
	
	<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-title column-primary sortable <?php echo $orderby == 'title' && $order == 'ASC' ? 'desc' : 'asc'; ?>">
						<a href="<?php echo add_query_arg(array(
							'orderby' => 'title',
							'order' => $orderby == 'title' && $order == 'ASC' ? 'desc' : 'asc'
						)); ?>">
							<span>Title</span>
							<span class="sorting-indicator"></span>
						</a>
					</th>
					<th scope="col" class="manage-column column-id sortable <?php echo $orderby == 'id' && $order == 'ASC' ? 'desc' : 'asc'; ?>">
						<a href="<?php echo add_query_arg(array(
							'orderby' => 'id',
							'order' => $orderby == 'id' && $order == 'ASC' ? 'desc' : 'asc'
						)); ?>">
							<span>ID</span>
							<span class="sorting-indicator"></span>
						</a>
					</th>
				</tr>
			</thead>
			<tbody id="the-list">
				<?php foreach ($results as $result) : ?>
					<tr id="post-5" class="iedit author-self level-0 post-5 type-survey status-publish hentry">
						<td class="title column-title has-row-actions column-primary page-title" data-colname="Title">
							<strong>
								<a class="row-title" href="<?php echo add_query_arg(array(
									'page' => $page,
									'edit' => true,
									'survey' => $result->id
								), admin_url('admin.php')); ?>" aria-label="“<?php echo $result->title; ?>” (Edit)"><?php echo $result->title; ?></a>
							</strong>
							<div class="row-actions">
								<span class="edit">
									<a href="<?php echo add_query_arg(array(
										'page' => $page,
										'edit' => true,
										'survey' => $result->id
									), admin_url('admin.php')); ?>" aria-label="Edit “<?php echo $result->title; ?>”">Edit</a> | </span>
								<span class="reports">
									<a href="<?php echo add_query_arg(array(
										'page' => $page,
										'reports' => $result->id
									), admin_url('admin.php')); ?>" aria-label="Edit “<?php echo $result->title; ?>”">Reports</a> | </span>
								<span class="delete">
									<a href="#" class="submitdelete" aria-label="Delete “<?php echo $result->title; ?>”">Delete</a> | </span>
								<span class="view">
									<a href="<?php echo add_query_arg(array(
										'page' => 'bw-survey-notifications',
										'survey' => $result->id
									), admin_url('admin.php')); ?>" aria-label="Notifications">Notifications</a> | </span>
								<span class="view">
									<a href="<?php echo add_query_arg(array(
										'page' => 'bw-survey-confirmations',
										'survey' => $result->id
									), admin_url('admin.php')); ?>" aria-label="Notifications">Confirmations</a> | </span>
								<span class="view">
									<a href="<?php echo add_query_arg(array(
										'page' => 'bw-survey-entries',
										'survey' => $result->id
									), admin_url('admin.php')); ?>" aria-label="Notifications">Entries</a>
								</span>
							</div>
						</td>
						<td class="id column-id"><?php echo $result->id; ?></td>
					</tr>
				<?php endforeach; ?>
			</tbody>
			<tfoot>
				<tr>
					<th scope="col" class="manage-column column-title column-primary sortable <?php echo $orderby == 'title' && $order == 'ASC' ? 'desc' : 'asc'; ?>">
						<a href="<?php echo add_query_arg(array(
							'orderby' => 'title',
							'order' => $orderby == 'title' && $order == 'ASC' ? 'desc' : 'asc'
						)); ?>">
							<span>Title</span>
							<span class="sorting-indicator"></span>
						</a>
					</th>
					<th scope="col" class="manage-column column-id sortable <?php echo $orderby == 'id' && $order == 'ASC' ? 'desc' : 'asc'; ?>">
						<a href="<?php echo add_query_arg(array(
							'orderby' => 'id',
							'order' => $orderby == 'id' && $order == 'ASC' ? 'desc' : 'asc'
						)); ?>">
							<span>ID</span>
							<span class="sorting-indicator"></span>
						</a>
					</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>
</div>