<?php

/**

 * Activity log view.

 *

 * @var array $rows

 * @var int   $paged

 * @var int   $pages

 * @var int   $total

 * @var int   $per

 * @var int[] $per_page_options

 * @package SuiteMkt3k

 */

defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound



use SuiteMkt3k\Utils\Activity_Labels;



$base_url = admin_url( 'admin.php?page=suitemkt3k-activity' );



$paginate_url = static function ( int $page ) use ( $base_url, $per ): string {

	return esc_url(

		add_query_arg(

			array(

				'paged'    => max( 1, $page ),

				'per_page' => $per,

			),

			$base_url

		)

	);

};



$from = $total > 0 ? ( ( $paged - 1 ) * $per ) + 1 : 0;

$to   = $total > 0 ? min( $paged * $per, $total ) : 0;

?>

<div class="wmk-card">

	<div class="wmk-card-header" style="display:flex;justify-content:space-between;align-items:center;gap:12px;flex-wrap:wrap;">

		<h3><?php esc_html_e( 'Activity log', 'suitemkt3k' ); ?></h3>

		<form method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" class="wmk-activity-toolbar">

			<input type="hidden" name="page" value="suitemkt3k-activity">

			<label class="wmk-activity-per-page">

				<span><?php esc_html_e( 'Rows per page', 'suitemkt3k' ); ?></span>

				<select class="wmk-select wmk-select-sm" name="per_page" onchange="this.form.submit()">

					<?php foreach ( $per_page_options as $option ) : ?>

						<option value="<?php echo esc_attr( (string) $option ); ?>" <?php selected( $per, $option ); ?>>

							<?php echo esc_html( (string) $option ); ?>

						</option>

					<?php endforeach; ?>

				</select>

			</label>

		</form>

	</div>

	<div class="wmk-card-body">

		<?php if ( ! $rows ) : ?>

			<p class="wmk-help"><?php esc_html_e( 'No activity yet.', 'suitemkt3k' ); ?></p>

		<?php else : ?>

			<p class="wmk-help wmk-activity-summary">

				<?php

				printf(

					/* translators: 1: first row number, 2: last row number, 3: total entries */

					esc_html__( 'Showing %1$d–%2$d of %3$d entries', 'suitemkt3k' ),

					(int) $from,

					(int) $to,

					(int) $total

				);

				?>

			</p>

			<ul class="wmk-activity">

				<?php foreach ( $rows as $log ) : ?>

					<li class="wmk-activity-item wmk-activity-<?php echo esc_attr( $log['level'] ); ?>">

						<div class="wmk-activity-dot"></div>

						<div class="wmk-activity-text">

							<strong><?php echo esc_html( Activity_Labels::context( (string) $log['context'] ) ); ?></strong>

							<span><?php echo esc_html( Activity_Labels::message( (string) $log['message'] ) ); ?></span>

							<small><?php echo esc_html( $log['created_at'] ); ?></small>

						</div>

					</li>

				<?php endforeach; ?>

			</ul>



			<?php if ( $pages > 1 ) : ?>

				<div class="wmk-pagination">

					<?php if ( $paged > 1 ) : ?>

						<a class="wmk-btn wmk-btn-ghost wmk-btn-sm" href="<?php echo esc_url( $paginate_url( $paged - 1 ) ); ?>">&laquo; <?php esc_html_e( 'Previous', 'suitemkt3k' ); ?></a>

					<?php endif; ?>

					<span class="wmk-pagination-info">

						<?php

						printf(

							/* translators: 1: current page 2: total pages */

							esc_html__( 'Page %1$d of %2$d', 'suitemkt3k' ),

							(int) $paged,

							(int) $pages

						);

						?>

					</span>

					<?php if ( $paged < $pages ) : ?>

						<a class="wmk-btn wmk-btn-ghost wmk-btn-sm" href="<?php echo esc_url( $paginate_url( $paged + 1 ) ); ?>"><?php esc_html_e( 'Next', 'suitemkt3k' ); ?> &raquo;</a>

					<?php endif; ?>

				</div>

			<?php endif; ?>

		<?php endif; ?>

	</div>

</div>

