<?php
/**
 * AnyCoin Payment Page Template - Modern White Design
 *
 * This template displays the payment instructions and status for cryptocurrency payments.
 *
 * @package AnyCoin_Payment_Gateway
 */

defined( 'ABSPATH' ) || exit;

// Variables available:
// $order - WC_Order object
// $order_id - Order ID
// $payment - Payment record from database
// $payment_id - Payment ID for reference
// $wallet_address - Zano integrated address (includes payment ID)
// $zano_amount - Amount in Zano
// $order_key - Order key
// $price_display - Current Zano price display string.


$gateways = WC()->payment_gateways()->get_available_payment_gateways();
$gateway  = isset( $gateways['anycoin_payment'] ) ? $gateways['anycoin_payment'] : null;


$required_confirmations = ( $gateway && isset( $gateway->confirmations ) ) ? (int) $gateway->confirmations : 10;


$is_test_mode = ( $gateway && isset( $gateway->test_mode ) ) ? $gateway->test_mode === 'yes' : false;


$created_time         = strtotime( $payment['created_at'] );
$expires_at           = $created_time + ( 15 * 60 );
$time_remaining       = max( 0, $expires_at - time() );
$expiration_timestamp = $expires_at * 1000;

// Asset IDs.
$zano_asset_id = 'd6329b5b1f7c0805b5c345f4957554002a2f557845f64d7645dae0e051a6498a';
$fusd_asset_id = '86143388bd056a8f0bab669f78f14873fac8e2dd8d57898cdb725a2d5e2e4f8f';

$order_total_usd = $order->get_total();
$fusd_amount     = $order_total_usd;

$formatted_zano_amount = number_format( (float) $zano_amount, 8, '.', '' );
$formatted_fusd_amount = number_format( (float) $fusd_amount, 6, '.', '' );
$usd_equivalent        = $price_display ? "≈ {$price_display}" : '';

$zano_deeplink = "zano:action=send&address={$wallet_address}&amount={$formatted_zano_amount}";
$fusd_deeplink = "zano:action=send&address={$wallet_address}&amount={$formatted_fusd_amount}&asset_id={$fusd_asset_id}";

	$qr_nonce    = wp_create_nonce( 'anycoin_qr' );
	$zano_qr_url = add_query_arg(
		[
			'data'     => $zano_deeplink,
			'_wpnonce' => $qr_nonce,
		],
		admin_url( 'admin-ajax.php?action=anycoin_generate_qr' )
	);
	$fusd_qr_url = add_query_arg(
		[
			'data'     => $fusd_deeplink,
			'_wpnonce' => $qr_nonce,
		],
		admin_url( 'admin-ajax.php?action=anycoin_generate_qr' )
	);
	?>

<div class="woocommerce-anycoin-payment">
	<div class="anycoin-payment-container">
		
		<header class="anycoin-payment-header">
			<div class="anycoin-branding">
				<img src="<?php echo esc_url( plugin_dir_url( __FILE__ ) . '../assets/images/anycoin/logo-blue-1.png' ); ?>" alt="AnyCoin Payment Gateway" class="anycoin-logo">
			</div>
			<h1>Complete Your Payment</h1>
			<p class="anycoin-payment-instruction">Please send the exact amount to finalize your order #<?php echo esc_html( $order_id ); ?>.</p>
		</header>
				
				<?php if ( $is_test_mode ) : ?>
				<div class="anycoin-test-mode-notice">
			⚠️ <strong>Test Mode</strong> - This is a test payment. No real funds will be transferred.
				</div>
				<?php endif; ?>
				
		<div class="anycoin-timer-card">
			<div class="anycoin-timer-display" id="payment-timer" data-expires="<?php echo esc_attr( $expiration_timestamp ); ?>">
				<?php
				$minutes = floor( $time_remaining / 60 );
				$seconds = $time_remaining % 60;
				printf( '%02d:%02d', esc_html( $minutes ), esc_html( $seconds ) );
				?>
			</div>
			<p class="anycoin-timer-label">Time Remaining</p>
		</div>
		
		<?php
		$anycoin_debug_info = [
			'createdAt'               => $payment['created_at'],
			'createdTimestamp'        => $created_time,
			'expiresAt'               => $expires_at,
			'expirationTimestamp'     => $expiration_timestamp,
			'timeRemaining'           => $time_remaining,
			'currentPHPTime'          => time(),
			'currentPHPTimeFormatted' => gmdate( 'Y-m-d H:i:s' ),
		];
		wp_add_inline_script(
			'anycoin-payment-script',
			'console.log("PHP Timer Debug Info:", ' . wp_json_encode( $anycoin_debug_info ) . ');',
			'after'
		);
		?>

		<div class="anycoin-payment-content">
			

				<div class="anycoin-left-column">
				

							<div class="anycoin-card anycoin-payment-details-card">
				<div class="anycoin-card-header">
					<h2 class="anycoin-card-title">Payment Details</h2>
				</div>
				<div class="anycoin-card-content">
						

											<div class="anycoin-asset-selector">
						<?php

						$payment_asset  = isset( $payment['asset_symbol'] ) ? strtolower( $payment['asset_symbol'] ) : 'zano';
						$is_zano_active = 'active';
						$is_fusd_active = '';
						?>
						<button class="anycoin-asset-button <?php echo esc_attr( $is_zano_active ); ?>" data-asset="zano">
							<img src="<?php echo esc_url( plugin_dir_url( __FILE__ ) . '../assets/images/zano-icon.png' ); ?>" alt="ZANO" class="anycoin-asset-icon">
							ZANO
						</button>
						<button class="anycoin-asset-button <?php echo esc_attr( $is_fusd_active ); ?>" data-asset="fusd">
							<img src="<?php echo esc_url( plugin_dir_url( __FILE__ ) . '../assets/images/fusd-icon.png' ); ?>" alt="FUSD" class="anycoin-asset-icon">
							FUSD
						</button>
					</div>
						

						<div id="zano-details" class="asset-details">

							<div class="anycoin-payment-item">
								<div class="anycoin-payment-item-header">
									<div class="anycoin-payment-item-label">
										<svg class="anycoin-payment-item-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
											<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1"></path>
										</svg>
										Amount
									</div>
																	<button class="anycoin-copy-button" data-copy="<?php echo esc_attr( $formatted_zano_amount ); ?>" title="Copy amount">
									<svg class="anycoin-copy-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
											<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path>
										</svg>
										Copy
									</button>
								</div>
								<p class="anycoin-payment-item-value"><?php echo esc_html( $formatted_zano_amount ); ?> ZANO</p>
								<?php if ( $usd_equivalent ) : ?>
								<p class="anycoin-payment-item-subvalue"><?php echo esc_html( $usd_equivalent ); ?></p>
								<?php endif; ?>
								<p class="anycoin-asset-id">Asset ID: <?php echo esc_html( $zano_asset_id ); ?></p>
							</div>
						</div>
						

						<div id="fusd-details" class="asset-details" style="display: none;">

							<div class="anycoin-payment-item">
								<div class="anycoin-payment-item-header">
									<div class="anycoin-payment-item-label">
										<svg class="anycoin-payment-item-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
											<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1"></path>
										</svg>
										Amount
									</div>
									<button class="anycoin-copy-button" data-copy="<?php echo esc_attr( $formatted_fusd_amount ); ?>" title="Copy amount">
										<svg class="anycoin-copy-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
											<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path>
										</svg>
										Copy
							</button>
								</div>
								<p class="anycoin-payment-item-value"><?php echo esc_html( $formatted_fusd_amount ); ?> FUSD</p>
								<p class="anycoin-payment-item-subvalue">≈ $<?php echo esc_html( $formatted_fusd_amount ); ?> USD</p>
								<p class="anycoin-asset-id">Asset ID: <?php echo esc_html( $fusd_asset_id ); ?></p>
							</div>
						</div>


						<div class="anycoin-payment-item">
							<div class="anycoin-payment-item-header">
								<div class="anycoin-payment-item-label">
									<svg class="anycoin-payment-item-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
										<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 01-9 9m9-9a9 9 0 00-9-9m9 9H3m9 9a9 9 0 01-9-9m9 9c1.657 0 3-4.03 3-9s1.343-9-3-9m0 18c-1.657 0-3-4.03-3-9s1.343-9 3-9m-9 9a9 9 0 019-9"></path>
									</svg>
									Wallet Address
								</div>
								<button class="anycoin-copy-button" data-copy="<?php echo esc_attr( $wallet_address ); ?>" title="Copy address">
									<svg class="anycoin-copy-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
										<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path>
									</svg>
									Copy
								</button>
							</div>
															<p class="anycoin-payment-item-value" style="font-size: 0.875rem; word-break: break-all;"><?php echo esc_html( $wallet_address ); ?></p>
						</div>

						</div>
					</div>
					

							<div class="anycoin-card anycoin-instructions-card">
				<div class="anycoin-card-header">
					<h2 class="anycoin-card-title">How to Pay</h2>
				</div>
				<div class="anycoin-card-content">
						<div class="anycoin-instructions">
							<ol>
								<li>Select your preferred asset (ZANO or FUSD) above.</li>
								<li>Copy the wallet address and amount.</li>
								<li>Open your Zano wallet and paste the details.</li>
								<li>Submit your transaction and wait for confirmation.</li>
							</ol>
						</div>
					</div>
				</div>


			</div>


			<div class="anycoin-right-column">
				

				<div class="anycoin-card anycoin-qr-card">
					<div class="anycoin-card-header">
						<h2 class="anycoin-card-title">Scan to Pay</h2>
					</div>
					<div class="anycoin-card-content">
						<div class="anycoin-qr-container">
							<div class="anycoin-qr-wrapper" id="qr-code-container">
								
							</div>
													<div class="anycoin-deeplink-actions">
							<button id="copy-deeplink-btn" class="anycoin-button secondary anycoin-copy-button" data-copy="">
									Copy Payment Link
								</button>
							</div>
							<p class="anycoin-qr-instruction">Use your Zano wallet app to scan this QR code.</p>
						</div>
					</div>
				</div>


				<div class="anycoin-card anycoin-status-card">
					<div class="anycoin-card-header">
						<h2 class="anycoin-card-title">Payment Status</h2>
					</div>
					<div class="anycoin-card-content">
						<div id="payment-status">
							<div class="anycoin-status-indicator waiting">
								<svg class="anycoin-status-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
									<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
								</svg>
								<span>Waiting for payment confirmation...</span>
							</div>
						</div>
						
						<button id="check-payment-btn" class="anycoin-button primary full">
							Check Payment Status
						</button>
					</div>
				</div>

			</div>
					</div>
					
		<footer class="anycoin-footer">
			<div class="anycoin-footer-buttons">
				<a href="<?php echo esc_url( wc_get_account_endpoint_url( 'orders' ) ); ?>" class="anycoin-button secondary">
					View Order Status
				</a>
				<a href="<?php echo esc_url( wc_get_page_permalink( 'shop' ) ); ?>" class="anycoin-button outline">
					Continue Shopping
				</a>
			</div>
			<p class="anycoin-footer-text">
				Having trouble? 
				<a href="<?php echo esc_url( get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) ); ?>">Contact Support</a>
			</p>
			<div class="anycoin-powered-by">
				<span>Powered by</span>
				<a href="https://www.acceptanycoin.com/" target="_blank" rel="noopener noreferrer" aria-label="AnyCoin">
					<img src="<?php echo esc_url( plugin_dir_url( __FILE__ ) . '../assets/images/anycoin/logo-white-1.png' ); ?>" alt="AnyCoin" class="anycoin-footer-logo">
				</a>
			</div>
		</footer>
	</div>
</div>

<?php
$anycoin_page_config = [
	'currentAsset'          => 'zano',
	'orderId'               => $order_id,
	'requiredConfirmations' => $required_confirmations,
	'ajax'                  => admin_url( 'admin-ajax.php' ),
	'assets'                => [
		'zano' => [
			'amount'   => $formatted_zano_amount,
			'symbol'   => 'ZANO',
			'assetId'  => $zano_asset_id,
			'deeplink' => $zano_deeplink,
			'qrUrl'    => $zano_qr_url,
			'name'     => 'Zano',
			'decimals' => 8,
			'logoUrl'  => plugin_dir_url( __FILE__ ) . '../assets/images/zano-icon.png',
		],
		'fusd' => [
			'amount'   => $formatted_fusd_amount,
			'symbol'   => 'FUSD',
			'assetId'  => $fusd_asset_id,
			'deeplink' => $fusd_deeplink,
			'qrUrl'    => $fusd_qr_url,
			'name'     => 'Fakechain USD',
			'decimals' => 6,
			'logoUrl'  => plugin_dir_url( __FILE__ ) . '../assets/images/fusd-icon.png',
		],
	],
];

wp_add_inline_script(
	'anycoin-payment-template',
	'window.currentAsset = ' . wp_json_encode( $anycoin_page_config['currentAsset'] ) . ';' .
	'window.assetData = ' . wp_json_encode( $anycoin_page_config['assets'] ) . ';' .
	'window.anycoinPayment = ' . wp_json_encode(
		[
			'orderId'               => $anycoin_page_config['orderId'],
			'requiredConfirmations' => $anycoin_page_config['requiredConfirmations'],
			'ajax'                  => $anycoin_page_config['ajax'],
			'qrNonce'               => wp_create_nonce( 'anycoin_qr' ),
		]
	) . ';',
	'before'
);
?>