<?php
$entry_id = $_GET['view'];

global $wpdb;
$entry = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}bwsurvey_entries WHERE id = %d", $entry_id));
if (empty($entry)) return;
$survey = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}bwsurvey_surveys WHERE id = %d", $entry->survey_id));
if (empty($survey)) return;
$sent = $wpdb->get_results($wpdb->prepare(
	"SELECT notification.type, notification.data
	FROM {$wpdb->prefix}bwsurvey_sent_notifications as sent
		INNER JOIN {$wpdb->prefix}bwsurvey_notifications as notification ON sent.notification_id = notification.id
	WHERE entry_id = %d;",
	$entry_id
));

$entry_data = unserialize($entry->data);
$survey_data = unserialize($survey->data);

?>
<div class="wrap">
	<h1 class="wp-heading-inline">Entry</h1>
	<hr class="wp-header-end">

	<p>
		<?php
		$date = strtotime( $entry->created_on );
		echo date( 'F jS, Y, g:i:s A', $date );
		?>
	<br/>
		<?php echo $entry->submitted ? 'Submitted' : 'Not Submitted'; ?>
	</p>
	
	<div>
		<table class="entry-fields">
			<thead>
				<tr>
					<th>Question</th>
					<th>Answer</th>
				</tr>
			</thead>
			<tbody>
				<?php foreach($survey_data['sections'] as $section): ?>
					<?php foreach($section as $question): if (!isset($entry_data[$question])) continue ?>
						<tr>
							<td><?php echo $survey_data['fields'][$question]['label']; ?></td>
							<td><?php echo $entry_data[$question]; ?></td>
						</tr>
					<?php endforeach; ?>
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>


	<h2 class="wp-heading-inline">Sent Notifications</h2>
	
	<div>
		<table class="entry-fields">
			<thead>
				<tr>
					<th>Type</th>
					<th>To</th>
					<th>Conditions</th>
				</tr>
			</thead>
			<tbody>
				<?php foreach($sent as $notification): $data = unserialize($notification->data); ?>
					<tr>
						<td><?php echo $notification->type; ?></td>
						<td>
							<?php foreach($data['to'] as $to): ?>
								<div><?php echo $to; ?></div>
							<?php endforeach; ?>
						</td>
						<td>
							<div class="conditional-logic-or">
								<?php foreach($data['conditionalLogic'] as $conditions): ?>
									<div class="conditional-logic-and">
										<?php foreach($conditions as $condition): ?>
											<div class="condition">
												<textarea readonly rows="1" cols="20"><?php echo esc_html($survey_data['fields'][$condition['field']]['label']); ?></textarea>
												&nbsp;&nbsp;
												<?php echo $condition['operator']; ?>
												&nbsp;&nbsp;
												<?php echo $condition['value']; ?>
											</div>
										<?php endforeach; ?>
									</div>
								<?php endforeach; ?>
							</div>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>

	<style>
		.entry-fields {
			border-collapse: collapse;
			table-layout: fixed;
			width: 100%;
		}

		.entry-fields td, .entry-fields th {
			border: 1px solid #ddd;
			padding: 10px;
		}

		.entry-fields tr:nth-child(even){background-color: #f2f2f2;}

		.entry-fields tr:hover {background-color: #ddd;}

		.entry-fields th {
			padding-top: 12px;
			padding-bottom: 12px;
			text-align: left;
			background-color: #38a7ad;
			color: white;
		}
		.conditional-logic-or > :not(:last-child)::after {
			display: block;
			content: "OR";
			font-weight: 700;
			padding: 1em 0;
		}
		.conditional-logic-and > :not(:last-child)::after {
			display: block;
			content: "AND";
			font-weight: 700;
			padding: 1em 0;
		}

		.condition {
			padding-left: 2em;
		}
	</style>
</div>
