<?php
/**
 * Submission detail for the WordPress.org free build.
 *
 * @package CF7MasterPro\Admin\Templates
 *
 * @var array $submission Formatted submission.
 * @var array $raw_fields Decoded form data.
 * @var array $meta       Request metadata.
 */
defined( 'ABSPATH' ) || exit;

$cf7mp_back_url = admin_url( 'admin.php?page=frontierline-forms-submissions' );
$cf7mp_mailto   = '';
if ( ! empty( $submission['sender'] ) && filter_var( $submission['sender'], FILTER_VALIDATE_EMAIL ) ) {
	$cf7mp_mailto = sprintf(
		'mailto:%s?subject=%s',
		rawurlencode( $submission['sender'] ),
		rawurlencode( 'Re: ' . ( $submission['subject'] ?? '' ) )
	);
}
?>
<div id="cf7mp-admin-root" data-view="submission-detail">
	<h1 class="cf7mp-admin__title">
		<a href="<?php echo esc_url( $cf7mp_back_url ); ?>" style="text-decoration:none;color:#646970;">&larr;</a>
		<?php esc_html_e( 'Submission detail', 'frontierline-forms' ); ?> #<?php echo esc_html( $submission['id'] ); ?>
	</h1>

	<div class="cf7mp-submission-list" style="margin-bottom:24px;">
		<table class="cf7mp-submission-list__table">
			<tbody>
				<tr><th><?php esc_html_e( 'Date', 'frontierline-forms' ); ?></th><td><?php echo esc_html( $submission['created_at'] ); ?></td></tr>
				<tr><th><?php esc_html_e( 'Form', 'frontierline-forms' ); ?></th><td><?php echo esc_html( $submission['form_title'] ); ?></td></tr>
				<tr><th><?php esc_html_e( 'Sender', 'frontierline-forms' ); ?></th><td>
					<?php echo esc_html( $submission['sender'] ); ?>
					<?php if ( $cf7mp_mailto ) : ?>
						&nbsp;<a class="button button-primary" href="<?php echo esc_url( $cf7mp_mailto ); ?>"><?php esc_html_e( 'Reply', 'frontierline-forms' ); ?></a>
					<?php endif; ?>
				</td></tr>
				<tr><th><?php esc_html_e( 'Subject', 'frontierline-forms' ); ?></th><td><?php echo esc_html( $submission['subject'] ); ?></td></tr>
				<tr><th><?php esc_html_e( 'Priority', 'frontierline-forms' ); ?></th><td>
					<span class="cf7mp-submission-list__priority cf7mp-submission-list__priority--<?php echo esc_attr( $submission['priority'] ); ?>">
						<?php echo esc_html( $submission['priority_label'] ); ?>
					</span>
				</td></tr>
			</tbody>
		</table>
	</div>

	<h2 style="margin-top:32px;"><?php esc_html_e( 'Submission content', 'frontierline-forms' ); ?></h2>
	<div class="cf7mp-submission-list">
		<table class="cf7mp-submission-list__table">
			<tbody>
				<?php if ( empty( $raw_fields ) ) : ?>
					<tr><td colspan="2" style="text-align:center;color:#646970;"><?php esc_html_e( 'No data', 'frontierline-forms' ); ?></td></tr>
				<?php else : ?>
					<?php foreach ( $raw_fields as $cf7mp_key => $cf7mp_val ) : ?>
						<?php
						if ( 0 === strpos( $cf7mp_key, '_wpcf7' ) || 0 === strpos( $cf7mp_key, 'cf7mp_hp_' ) ) {
							continue;
						}
						?>
						<tr>
							<th style="width:240px;"><?php echo esc_html( $cf7mp_key ); ?></th>
							<td><?php echo nl2br( esc_html( is_array( $cf7mp_val ) ? implode( ', ', $cf7mp_val ) : (string) $cf7mp_val ) ); ?></td>
						</tr>
					<?php endforeach; ?>
				<?php endif; ?>
			</tbody>
		</table>
	</div>

	<h2 style="margin-top:32px;"><?php esc_html_e( 'Meta information', 'frontierline-forms' ); ?></h2>
	<div class="cf7mp-submission-list">
		<table class="cf7mp-submission-list__table">
			<tbody>
				<tr><th style="width:240px;">IP</th><td><?php echo esc_html( $meta['ip_address'] ?? '-' ); ?></td></tr>
				<tr><th>Referer</th><td><?php echo esc_html( $meta['referer'] ?? '-' ); ?></td></tr>
				<tr><th>User Agent</th><td><?php echo esc_html( $meta['user_agent'] ?? '-' ); ?></td></tr>
				<tr><th>utm_source</th><td><?php echo esc_html( $meta['utm_source'] ?? '-' ); ?></td></tr>
				<tr><th>utm_medium</th><td><?php echo esc_html( $meta['utm_medium'] ?? '-' ); ?></td></tr>
				<tr><th>utm_campaign</th><td><?php echo esc_html( $meta['utm_campaign'] ?? '-' ); ?></td></tr>
			</tbody>
		</table>
	</div>

	<p style="margin-top:24px;">
		<a class="button" href="<?php echo esc_url( $cf7mp_back_url ); ?>">&larr; <?php esc_html_e( 'Back to list', 'frontierline-forms' ); ?></a>
	</p>
</div>
