<?php
/**
 * GoValid QR Code Verification Page Template.
 *
 * Rendered when a visitor hits /v/{token} on the WordPress site.
 *
 * @package GoValid_QR
 */

defined( 'ABSPATH' ) || exit;

$govalid_verify_token = sanitize_text_field( get_query_var( 'govalid_verify_token', '' ) );
$govalid_verify_token = rtrim( $govalid_verify_token, '/' );
$govalid_base_url     = GoValid_QR::get_base_url();

wp_enqueue_script(
	'govalid-verify-page',
	GOVALID_QR_PLUGIN_URL . 'public/js/verify-page.js',
	array(),
	GOVALID_QR_VERSION,
	true
);
wp_localize_script( 'govalid-verify-page', 'govalidVerifyData', array(
	'token'  => $govalid_verify_token,
	'apiUrl' => esc_url_raw( $govalid_base_url . '/api/v1/ojs/verify-signed/?token=' . rawurlencode( $govalid_verify_token ) ),
	'i18n'   => array(
		'noToken'            => __( 'No Token Provided', 'govalid-qr-validator' ),
		'noTokenMsg'         => __( 'No QR code token was found in the URL.', 'govalid-qr-validator' ),
		'notVerified'        => __( 'QR Code Not Verified', 'govalid-qr-validator' ),
		'notVerifiedMsg'     => __( 'This QR code could not be verified.', 'govalid-qr-validator' ),
		'verifyError'        => __( 'Verification Error', 'govalid-qr-validator' ),
		'verifyErrorMsg'     => __( 'An error occurred while processing the verification response.', 'govalid-qr-validator' ),
		'notFound'           => __( 'QR Code Not Found', 'govalid-qr-validator' ),
		'notFoundMsg'        => __( 'This QR code was not found in the GoValid system.', 'govalid-qr-validator' ),
		'tooManyRequests'    => __( 'Too Many Requests', 'govalid-qr-validator' ),
		'tooManyRequestsMsg' => __( 'Please wait a moment and try again.', 'govalid-qr-validator' ),
		'verifyFailed'       => __( 'Verification Failed', 'govalid-qr-validator' ),
		'verifyFailedMsg'    => __( 'Unable to verify this QR code. Please try again later.', 'govalid-qr-validator' ),
		'connError'          => __( 'Connection Error', 'govalid-qr-validator' ),
		'connErrorMsg'       => __( 'Unable to connect to the verification server. Please check your internet connection.', 'govalid-qr-validator' ),
		'timeout'            => __( 'Request Timeout', 'govalid-qr-validator' ),
		'timeoutMsg'         => __( 'The verification request timed out. Please try again.', 'govalid-qr-validator' ),
		'counterfeit'        => __( 'Counterfeit Detected', 'govalid-qr-validator' ),
		'counterfeitMsg'     => __( 'This QR code has security violations.', 'govalid-qr-validator' ),
		'warnings'           => __( 'Verified with Warnings', 'govalid-qr-validator' ),
		'warningsMsg'        => __( 'This QR code is valid but has security warnings.', 'govalid-qr-validator' ),
		'inactive'           => __( 'QR Code Inactive', 'govalid-qr-validator' ),
		'inactiveMsg'        => __( 'This QR code has been deactivated by its owner.', 'govalid-qr-validator' ),
		'verified'           => __( 'Verified Authentic', 'govalid-qr-validator' ),
		'verifiedMsg'        => __( 'This QR code is genuine and active.', 'govalid-qr-validator' ),
		'active'             => __( 'Active', 'govalid-qr-validator' ),
		'inactiveBadge'      => __( 'Inactive', 'govalid-qr-validator' ),
	),
) );
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
	<meta charset="<?php bloginfo( 'charset' ); ?>">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="robots" content="noindex, nofollow">
	<title><?php echo esc_html__( 'QR Code Verification', 'govalid-qr-validator' ) . ' - ' . esc_html( get_bloginfo( 'name' ) ); ?></title>
	<?php wp_head(); ?>
</head>
<body <?php body_class( 'govalid-verify-page' ); ?>>

<div class="govalid-verify-wrapper">
	<div class="govalid-verify-card">
		<div class="govalid-verify-header">
			<h1><?php esc_html_e( 'QR Code Verification', 'govalid-qr-validator' ); ?></h1>
			<p><?php echo esc_html( get_bloginfo( 'name' ) ); ?></p>
		</div>

		<div class="govalid-verify-body">
			<!-- Loading -->
			<div id="govalid-verify-loading" class="govalid-verify-loading">
				<div class="govalid-verify-spinner"></div>
				<p><?php esc_html_e( 'Verifying QR code...', 'govalid-qr-validator' ); ?></p>
			</div>

			<!-- Result -->
			<div id="govalid-verify-result" class="govalid-verify-result">
				<div id="govalid-verify-status" class="govalid-verify-status">
					<div class="govalid-verify-status-icon"></div>
					<h2 id="govalid-verify-status-text"></h2>
					<p class="govalid-verify-subtitle" id="govalid-verify-status-sub"></p>
				</div>

				<table class="govalid-verify-details">
					<tr id="govalid-row-name">
						<th><?php esc_html_e( 'QR Name', 'govalid-qr-validator' ); ?></th>
						<td id="govalid-verify-name"></td>
					</tr>
					<tr id="govalid-row-type">
						<th><?php esc_html_e( 'Type', 'govalid-qr-validator' ); ?></th>
						<td id="govalid-verify-type"></td>
					</tr>
					<tr id="govalid-row-creator">
						<th><?php esc_html_e( 'Created by', 'govalid-qr-validator' ); ?></th>
						<td id="govalid-verify-creator"></td>
					</tr>
					<tr id="govalid-row-institution">
						<th><?php esc_html_e( 'Institution', 'govalid-qr-validator' ); ?></th>
						<td id="govalid-verify-institution"></td>
					</tr>
					<tr id="govalid-row-scans">
						<th><?php esc_html_e( 'Scan count', 'govalid-qr-validator' ); ?></th>
						<td id="govalid-verify-scans"></td>
					</tr>
					<tr id="govalid-row-status">
						<th><?php esc_html_e( 'Status', 'govalid-qr-validator' ); ?></th>
						<td id="govalid-verify-active"></td>
					</tr>
					<tr id="govalid-row-created">
						<th><?php esc_html_e( 'Created', 'govalid-qr-validator' ); ?></th>
						<td id="govalid-verify-created"></td>
					</tr>
				</table>

				<div id="govalid-verify-violations" class="govalid-verify-violations" style="display:none;">
					<h3><?php esc_html_e( 'Security Alerts', 'govalid-qr-validator' ); ?></h3>
					<div id="govalid-violations-list"></div>
				</div>
			</div>

			<!-- Error -->
			<div id="govalid-verify-error" class="govalid-verify-error">
				<div class="govalid-verify-error-icon">
					<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
						<circle cx="12" cy="12" r="10"></circle>
						<line x1="12" y1="8" x2="12" y2="12"></line>
						<line x1="12" y1="16" x2="12.01" y2="16"></line>
					</svg>
				</div>
				<h2 id="govalid-error-title"><?php esc_html_e( 'Verification Failed', 'govalid-qr-validator' ); ?></h2>
				<p id="govalid-error-message"><?php esc_html_e( 'Unable to verify this QR code. It may be invalid or expired.', 'govalid-qr-validator' ); ?></p>
			</div>
		</div>

		<?php if ( get_option( 'govalid_qr_show_powered_by', '' ) === '1' ) : ?>
		<div class="govalid-verify-footer">
			<a href="https://govalid.org" target="_blank" rel="noopener" class="govalid-powered-by">
				<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" width="20" height="20">
					<circle cx="128" cy="128" r="128" fill="#4169E1"/>
					<path fill="#ffffff" fill-rule="evenodd" d="M225.86,102.82c-3.77-3.94-7.67-8-9.14-11.57-1.36-3.27-1.44-8.69-1.52-13.94-.15-9.76-.31-20.82-8-28.51s-18.75-7.85-28.51-8c-5.25-.08-10.67-.16-13.94-1.52-3.56-1.47-7.63-5.37-11.57-9.14C146.28,23.51,138.44,16,128,16s-18.27,7.51-25.18,14.14c-3.94,3.77-8,7.67-11.57,9.14C88,40.64,82.56,40.72,77.31,40.8c-9.76.15-20.82.31-28.51,8S41,67.55,40.8,77.31c-.08,5.25-.16,10.67-1.52,13.94-1.47,3.56-5.37,7.63-9.14,11.57C23.51,109.72,16,117.56,16,128s7.51,18.27,14.14,25.18c3.77,3.94,7.67,8,9.14,11.57,1.36,3.27,1.44,8.69,1.52,13.94.15,9.76.31,20.82,8,28.51s18.75,7.85,28.51,8c5.25.08,10.67.16,13.94,1.52,3.56,1.47,7.63,5.37,11.57,9.14C109.72,232.49,117.56,240,128,240s18.27-7.51,25.18-14.14c3.94-3.77,8-7.67,11.57-9.14,3.27-1.36,8.69-1.44,13.94-1.52,9.76-.15,20.82-.31,28.51-8s7.85-18.75,8-28.51c.08-5.25.16-10.67,1.52-13.94,1.47-3.56,5.37-7.63,9.14-11.57C232.49,146.28,240,138.44,240,128S232.49,109.73,225.86,102.82Zm-52.2,6.84-56,56a8,8,0,0,1-11.32,0l-24-24a8,8,0,0,1,11.32-11.32L112,148.69l50.34-50.35a8,8,0,0,1,11.32,11.32Z"/>
				</svg>
				<?php echo esc_html__( 'Powered by', 'govalid-qr-validator' ); ?>
				<span>GoValid</span>
			</a>
		</div>
		<?php endif; ?>
	</div>
</div>

<?php wp_footer(); ?>
</body>
</html>
