<?php
/**
 * The recent activity table on the Logs tab.
 *
 * Computes everything it needs. Partials must never inherit variables from
 * whoever includes them — that is how bulk import once ended up locked for
 * paying customers while every test passed.
 *
 * @package Ordsync
 */

defined( 'ABSPATH' ) || exit;

$ordsync_events = class_exists( 'Ordsync_Events' ) ? Ordsync_Events::recent() : array();

// Instance id -> friendly name, so the table reads "Webhook 2", not "webhook_1".
$ordsync_names = array();
if ( class_exists( 'Ordsync_Destinations' ) ) {
	foreach ( Ordsync_Destinations::all() as $ordsync_id => $ordsync_dest ) {
		$ordsync_names[ $ordsync_id ] = $ordsync_dest->display_name();
	}
}
?>

<div class="ordsync-card">
	<h2><?php esc_html_e( 'Recent activity', 'ordercarry-order-automation-for-woocommerce' ); ?></h2>

	<?php if ( empty( $ordsync_events ) ) : ?>

		<p class="description">
			<?php esc_html_e( 'Nothing has been sent yet. Once orders start arriving, every send, retry and failure shows up here.', 'ordercarry-order-automation-for-woocommerce' ); ?>
		</p>

	<?php else : ?>

		<p class="description ordsync-events-scroll-hint">
			<?php esc_html_e( 'Scroll right to see the result and the Retry button.', 'ordercarry-order-automation-for-woocommerce' ); ?>
		</p>

		<table class="ordsync-events widefat striped">
			<thead>
				<tr>
					<th scope="col"><?php esc_html_e( 'When', 'ordercarry-order-automation-for-woocommerce' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Order', 'ordercarry-order-automation-for-woocommerce' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Destination', 'ordercarry-order-automation-for-woocommerce' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Result', 'ordercarry-order-automation-for-woocommerce' ); ?></th>
				</tr>
			</thead>
			<tbody>
			<?php foreach ( $ordsync_events as $ordsync_event ) : ?>
				<?php
				$ordsync_state = isset( $ordsync_event['state'] ) ? $ordsync_event['state'] : 'sent';
				$ordsync_dest  = isset( $ordsync_event['dest'] ) ? $ordsync_event['dest'] : '';
				$ordsync_order = isset( $ordsync_event['order'] ) ? (int) $ordsync_event['order'] : 0;
				$ordsync_msg   = isset( $ordsync_event['message'] ) ? $ordsync_event['message'] : '';
				$ordsync_try   = isset( $ordsync_event['attempt'] ) ? (int) $ordsync_event['attempt'] : 1;

				$ordsync_name = isset( $ordsync_names[ $ordsync_dest ] )
					? $ordsync_names[ $ordsync_dest ]
					: $ordsync_dest;
				?>
				<tr>
					<td class="ordsync-events-when">
						<?php
						echo esc_html(
							sprintf(
								/* translators: %s: human readable time difference, for example "5 mins" */
								__( '%s ago', 'ordercarry-order-automation-for-woocommerce' ),
								human_time_diff( isset( $ordsync_event['time'] ) ? (int) $ordsync_event['time'] : time() )
							)
						);
						?>
					</td>

					<td>
						<?php if ( $ordsync_order ) : ?>
							<a href="<?php echo esc_url( admin_url( 'post.php?post=' . $ordsync_order . '&action=edit' ) ); ?>">
								<?php echo esc_html( '#' . $ordsync_order ); ?>
							</a>
						<?php elseif ( 'test' === $ordsync_state ) : ?>
							<?php esc_html_e( 'Test', 'ordercarry-order-automation-for-woocommerce' ); ?>
						<?php else : ?>
							<?php echo esc_html( '—' ); ?>
						<?php endif; ?>
					</td>

					<td><?php echo esc_html( $ordsync_name ); ?></td>

					<td>
						<?php if ( 'sent' === $ordsync_state ) : ?>

							<span class="ordsync-ev ordsync-ev-sent">
								<?php
								echo esc_html(
									$ordsync_try > 1
										? sprintf(
											/* translators: %d: attempt number */
											__( 'Sent on attempt %d', 'ordercarry-order-automation-for-woocommerce' ),
											$ordsync_try
										)
										: __( 'Sent', 'ordercarry-order-automation-for-woocommerce' )
								);
								?>
							</span>

						<?php elseif ( 'test' === $ordsync_state ) : ?>

							<span class="ordsync-ev ordsync-ev-test">
								<?php esc_html_e( 'Test passed', 'ordercarry-order-automation-for-woocommerce' ); ?>
							</span>

						<?php elseif ( 'retrying' === $ordsync_state ) : ?>

							<span class="ordsync-ev ordsync-ev-retry">
								<?php esc_html_e( 'Retrying', 'ordercarry-order-automation-for-woocommerce' ); ?>
							</span>
							<?php if ( '' !== $ordsync_msg ) : ?>
								<span class="ordsync-ev-msg"><?php echo esc_html( $ordsync_msg ); ?></span>
							<?php endif; ?>

						<?php else : ?>

							<span class="ordsync-ev ordsync-ev-failed">
								<?php esc_html_e( 'Failed', 'ordercarry-order-automation-for-woocommerce' ); ?>
							</span>
							<?php if ( '' !== $ordsync_msg ) : ?>
								<span class="ordsync-ev-msg"><?php echo esc_html( $ordsync_msg ); ?></span>
							<?php endif; ?>

							<?php if ( $ordsync_order && '' !== $ordsync_dest ) : ?>
								<button
									type="button"
									class="button button-small ordsync-retry"
									data-order="<?php echo esc_attr( $ordsync_order ); ?>"
									data-dest="<?php echo esc_attr( $ordsync_dest ); ?>">
									<?php esc_html_e( 'Retry now', 'ordercarry-order-automation-for-woocommerce' ); ?>
								</button>
								<span class="ordsync-retry-result"></span>
							<?php endif; ?>

						<?php endif; ?>
					</td>
				</tr>
			<?php endforeach; ?>
			</tbody>
		</table>

		<p class="description">
			<?php esc_html_e( 'The most recent 200 events. Older ones are dropped — the full history stays in the WooCommerce log below.', 'ordercarry-order-automation-for-woocommerce' ); ?>
		</p>

	<?php endif; ?>
</div>
