<?php
/**
 * Abandoned cart list.
 *
 * @var array  $carts
 * @var string $nonce
 * @package SuiteMkt3k
 */
defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$imported_raw = filter_input( INPUT_GET, 'imported', FILTER_SANITIZE_NUMBER_INT );
$imported     = max( 0, (int) $imported_raw );
?>
<div class="wmk-toolbar wmk-card">
	<div class="wmk-toolbar-actions">
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="display:inline-block;">
			<input type="hidden" name="action" value="suitemkt3k_cart_import_wc">
			<?php wp_nonce_field( \SuiteMkt3k\Security\Nonces::ADMIN_ACTION ); ?>
			<button type="submit" class="wmk-btn wmk-btn-primary">
				<?php esc_html_e( 'List abandoned carts', 'suitemkt3k' ); ?>
			</button>
		</form>
		<a class="wmk-btn wmk-btn-ghost" href="<?php echo esc_url( add_query_arg( array( 'view' => 'template' ), admin_url( 'admin.php?page=suitemkt3k-cart-recovery' ) ) ); ?>">
			<?php esc_html_e( 'Edit recovery email template', 'suitemkt3k' ); ?>
		</a>
	</div>
	<div class="wmk-toolbar-actions" style="margin-left:auto;">
		<?php if ( $imported > 0 ) : ?>
			<div class="wmk-flash wmk-flash-success" style="margin-right:8px;">
				<?php
				printf(
					/* translators: %d: number of imported carts. */
					esc_html__( '%d cancelled/draft orders imported as abandoned carts.', 'suitemkt3k' ),
					(int) $imported
				);
				?>
			</div>
		<?php endif; ?>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"
			onsubmit="return confirm('<?php echo esc_js( __( 'This will permanently remove every abandoned cart. Continue?', 'suitemkt3k' ) ); ?>')">
			<input type="hidden" name="action" value="suitemkt3k_cart_delete_all">
			<?php wp_nonce_field( \SuiteMkt3k\Security\Nonces::ADMIN_ACTION ); ?>
			<button type="submit" class="wmk-btn wmk-btn-danger">
				<?php esc_html_e( 'Delete all', 'suitemkt3k' ); ?>
			</button>
		</form>
	</div>
</div>
<div class="wmk-card">
	<table class="wmk-table">
		<thead><tr>
			<th>#</th>
			<th><?php esc_html_e( 'Customer', 'suitemkt3k' ); ?></th>
			<th><?php esc_html_e( 'Email', 'suitemkt3k' ); ?></th>
			<th><?php esc_html_e( 'Total', 'suitemkt3k' ); ?></th>
			<th><?php esc_html_e( 'Last activity', 'suitemkt3k' ); ?></th>
			<th><?php esc_html_e( 'Status', 'suitemkt3k' ); ?></th>
			<th class="wmk-col-actions"><?php esc_html_e( 'Actions', 'suitemkt3k' ); ?></th>
		</tr></thead>
		<tbody>
		<?php if ( ! $carts ) : ?>
			<tr><td colspan="7" class="wmk-empty"><?php esc_html_e( 'No abandoned carts captured yet.', 'suitemkt3k' ); ?></td></tr>
		<?php endif; ?>
		<?php foreach ( $carts as $c ) : ?>
			<tr>
				<td>#<?php echo esc_html( (string) $c['id'] ); ?></td>
				<td><?php echo esc_html( (string) ( $c['name'] ?: '—' ) ); ?></td>
				<td><?php echo esc_html( (string) $c['email'] ); ?></td>
				<td><?php echo esc_html( number_format( (float) $c['total'], 2, '.', ',' ) . ' ' . ( $c['currency'] ?? '' ) ); ?></td>
				<td><small><?php echo esc_html( (string) $c['last_activity_at'] ); ?></small></td>
				<td><span class="wmk-pill wmk-pill-<?php echo esc_attr( $c['status'] ); ?>"><?php echo esc_html( \SuiteMkt3k\Modules\Cart_Recovery\Cart_Recovery_Manager::status_label( $c['status'] ) ); ?></span></td>
				<td class="wmk-col-actions">
					<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="display:inline-block;">
						<input type="hidden" name="action" value="suitemkt3k_cart_send_email">
						<input type="hidden" name="cart_id" value="<?php echo (int) $c['id']; ?>">
						<?php wp_nonce_field( \SuiteMkt3k\Security\Nonces::ADMIN_ACTION ); ?>
						<button class="wmk-icon-btn" type="submit" title="<?php esc_attr_e( 'Send recovery email', 'suitemkt3k' ); ?>">
							<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 2L11 13"/><path d="M22 2l-7 20-4-9-9-4z"/></svg>
						</button>
					</form>
					<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="display:inline-block;" onsubmit="return confirm('<?php echo esc_js( __( 'Remove this cart?', 'suitemkt3k' ) ); ?>')">
						<input type="hidden" name="action" value="suitemkt3k_cart_delete">
						<input type="hidden" name="id" value="<?php echo (int) $c['id']; ?>">
						<?php wp_nonce_field( \SuiteMkt3k\Security\Nonces::ADMIN_ACTION ); ?>
						<button class="wmk-icon-btn wmk-icon-btn-danger" type="submit" title="<?php esc_attr_e( 'Delete', 'suitemkt3k' ); ?>">
							<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 6h18"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/></svg>
						</button>
					</form>
				</td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>
</div>
