<?php
/**
 * Transactions Table View.
 *
 * This file contains the HTML template for displaying the AnyCoin payment transactions table
 * in the WordPress admin area.
 *
 * @package AnyCoin_Payment_Gateway
 */

defined( 'ABSPATH' ) || exit;

?>

<div class="wrap anycoin-admin">
	<h1>AnyCoin Transactions</h1>
	
	<!-- Transaction Summary -->
	<?php
	$summary = AnyCoin_Database_Manager::get_payment_status_summary();
	?>
	
	<div class="anycoin-summary">
		<span><strong><?php echo esc_html( esc_html( number_format_i18n( $total_items ) ) ); ?></strong> Total</span>
		<span class="status-confirmed"><strong><?php echo esc_html( $summary['confirmed'] ?? 0 ); ?></strong> Confirmed</span>
		<span class="status-pending"><strong><?php echo esc_html( $summary['pending'] ?? 0 ); ?></strong> Pending</span>
		<span class="status-failed"><strong><?php echo esc_html( $summary['failed'] ?? 0 ); ?></strong> Failed</span>
	</div>
	
	<div class="tablenav top">
		<div class="alignleft actions">
			<a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=anycoin-transactions&action=update-order-statuses' ), 'anycoin_update_statuses' ) ); ?>" 
				class="button button-primary anycoin-confirm" 
				data-confirm="<?php echo esc_attr( 'This will check all pending payments and update their statuses. This may take a while. Continue?' ); ?>">
				<?php echo 'Update Order Statuses'; ?>
			</a>
			
			<span class="description" style="margin-left: 10px; font-style: italic;">
				<?php echo 'Smart check: only verifies expired payments (>20min) and unconfirmed transactions that need checking.'; ?>
			</span>
		</div>
		
		<?php if ( $total_pages > 1 ) : ?>
		<div class="tablenav-pages">
			<span class="displaying-num">
				<?php printf( '%s items', esc_html( number_format_i18n( $total_items ) ) ); ?>
			</span>
			
			<?php
			$page_links = paginate_links(
				[
					'base'      => add_query_arg( 'paged', '%#%' ),
					'format'    => '',
					'prev_text' => '&laquo;',
					'next_text' => '&raquo;',
					'total'     => $total_pages,
					'current'   => $current_page,
					'type'      => 'array',
				]
			);

			if ( $page_links ) {
				echo '<span class="pagination-links">';
				echo implode( "\n", array_map( 'wp_kses_post', $page_links ) );
				echo '</span>';
			}
			?>
		</div>
		<?php endif; ?>
	</div>
	
	<table class="wp-list-table widefat fixed striped">
		<thead>
			<tr>
				<th scope="col" class="manage-column column-order-id">
					<?php echo 'Order ID'; ?>
				</th>
				<th scope="col" class="manage-column column-payment-id">
					<?php echo 'Payment ID'; ?>
				</th>
				<th scope="col" class="manage-column column-amount">
					<?php echo 'Amount (ZANO)'; ?>
				</th>
				<th scope="col" class="manage-column column-tx-hash">
					<?php echo 'Transaction Hash'; ?>
				</th>
				<th scope="col" class="manage-column column-status">
					<?php echo 'Status'; ?>
				</th>
				<th scope="col" class="manage-column column-block-info">
					<?php echo 'Block Info'; ?>
				</th>
				<th scope="col" class="manage-column column-date">
					<?php echo 'Date'; ?>
				</th>
				<th scope="col" class="manage-column column-age">
					<?php echo 'Age'; ?>
				</th>
				<th scope="col" class="manage-column column-actions">
					<?php echo 'Actions'; ?>
				</th>
			</tr>
		</thead>
		
		<tbody>
			<?php if ( empty( $transactions ) ) : ?>
				<tr>
					<td colspan="9" class="no-items">
						<?php echo 'No transactions found.'; ?>
					</td>
				</tr>
			<?php else : ?>
				<?php foreach ( $transactions as $transaction ) : ?>
					<?php
					$order        = wc_get_order( $transaction['order_id'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Local variable shadowing only.
					$is_duplicate = ! empty( $transaction['tx_hash'] ) && in_array( $transaction['tx_hash'], $duplicate_hashes, true );
					?>
					<tr>
						<td class="column-order-id">
							<?php if ( $order ) : ?>
								<a href="<?php echo esc_url( $order->get_edit_order_url() ); ?>" target="_blank">
									#<?php echo esc_html( $transaction['order_id'] ); ?>
								</a>
								<br><small><?php echo esc_html( $order->get_status() ); ?></small>
							<?php else : ?>
								#<?php echo esc_html( $transaction['order_id'] ); ?>
								<br><small class="description"><?php echo 'processing'; ?></small>
							<?php endif; ?>
						</td>
						
						<td class="column-payment-id">
							<span title="<?php echo esc_attr( $transaction['payment_id'] ); ?>">
								<?php echo esc_html( $transaction['payment_id'] ); ?>
							</span>
						</td>
						
						<td class="column-amount">
							<?php echo esc_html( number_format( $transaction['amount'], 8 ) ); ?>
							<?php if ( ! empty( $transaction['received_amount'] ) ) : ?>
								<br><small>
									<?php echo 'Received:'; ?> 
									<?php echo esc_html( number_format( $transaction['received_amount'], 8 ) ); ?>
								</small>
							<?php endif; ?>
						</td>
						
						<td class="column-tx-hash">
							<?php if ( ! empty( $transaction['tx_hash'] ) ) : ?>
								<?php echo esc_html( substr( $transaction['tx_hash'], 0, 12 ) ); ?>...
								<br>
								<a href="https://explorer.zano.org/transaction/<?php echo esc_attr( $transaction['tx_hash'] ); ?>" 
									target="_blank" 
									style="font-size: 11px;">
									<?php echo 'View'; ?>
								</a>
							<?php else : ?>
								—
							<?php endif; ?>
						</td>
						
						<td class="column-status">
							<?php
							$status_label = ucfirst( $transaction['status'] );
							if ( $transaction['status'] === 'confirmed' && $transaction['confirmations'] > 0 ) {
								$status_label = 'confirmed (' . $transaction['confirmations'] . ' confirmations)';
							}
							?>
							<span class="status-<?php echo esc_attr( $transaction['status'] ); ?>">
								<?php echo esc_html( $status_label ); ?>
							</span>
							
							<?php if ( $is_duplicate ) : ?>
								<br><small style="color: #d63638;">
									<?php echo 'Duplicate TX Hash'; ?>
								</small>
							<?php endif; ?>
						</td>
						
						<td class="column-block-info">
							<?php if ( ! empty( $transaction['received_block'] ) || ! empty( $transaction['current_block'] ) ) : ?>
								<?php if ( ! empty( $transaction['received_block'] ) ) : ?>
									<strong><?php echo 'Received:'; ?></strong> <?php echo esc_html( number_format( $transaction['received_block'] ) ); ?>
									<br>
								<?php endif; ?>
								<?php if ( ! empty( $transaction['current_block'] ) ) : ?>
									<strong><?php echo 'Current:'; ?></strong> <?php echo esc_html( number_format( $transaction['current_block'] ) ); ?>
									<br>
								<?php endif; ?>
								<?php if ( ! empty( $transaction['keeper_block'] ) ) : ?>
									<strong><?php echo 'Keeper:'; ?></strong> <?php echo esc_html( number_format( $transaction['keeper_block'] ) ); ?>
								<?php endif; ?>
							<?php else : ?>
								—
							<?php endif; ?>
						</td>
						
						<td class="column-date">
							<?php
							$created_time = strtotime( $transaction['created_at'] );
							echo esc_html( gmdate( 'M j, Y g:i a', $created_time ) );
							?>
						</td>
						
						<td class="column-age">
							<?php
							$created_time = strtotime( $transaction['created_at'] );
							echo esc_html( human_time_diff( $created_time, time() ) . ' ago' );
							?>
						</td>
						
						<td class="column-actions">
							<?php if ( ! empty( $transaction['tx_hash'] ) ) : ?>
								<a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=anycoin-transactions&action=clear-tx-hash&payment_id=' . rawurlencode( $transaction['payment_id'] ) ), 'anycoin_transaction_action' ) ); ?>"
									class="anycoin-confirm"
									data-confirm="<?php echo esc_attr( 'Clear the transaction hash and reset payment to pending?' ); ?>">
									<?php echo 'Clear TX Hash'; ?>
								</a>
								|
							<?php endif; ?>
							
							<a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=anycoin-transactions&action=cancel&payment_id=' . rawurlencode( $transaction['payment_id'] ) ), 'anycoin_transaction_action' ) ); ?>"
								class="anycoin-confirm"
								data-confirm="<?php echo esc_attr( 'Cancel this order?' ); ?>">
								<?php echo 'Cancel'; ?>
							</a>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>
	
	<?php if ( $total_pages > 1 ) : ?>
	<div class="tablenav bottom">
		<div class="tablenav-pages">
			<span class="displaying-num">
				<?php printf( '%s items', esc_html( number_format_i18n( $total_items ) ) ); ?>
			</span>
			
			<?php
			if ( $page_links ) {
				echo '<span class="pagination-links">';
				echo implode( "\n", array_map( 'wp_kses_post', $page_links ) );
				echo '</span>';
			}
			?>
		</div>
	</div>
	<?php endif; ?>
	

</div>

