<?php
/**
 * Newsletter list view.
 *
 * @var array  $campaigns
 * @var string $nonce
 * @package SuiteMkt3k
 */
defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
?>
<div class="wmk-toolbar wmk-card">
	<div class="wmk-toolbar-actions">
		<a class="wmk-btn wmk-btn-primary" href="<?php echo esc_url( add_query_arg( array( 'view' => 'edit', 'id' => 0 ), admin_url( 'admin.php?page=suitemkt3k-newsletter' ) ) ); ?>">
			<?php esc_html_e( '+ New newsletter', 'suitemkt3k' ); ?>
		</a>
		<a class="wmk-btn wmk-btn-ghost" href="<?php echo esc_url( add_query_arg( array( 'view' => 'forms' ), admin_url( 'admin.php?page=suitemkt3k-newsletter' ) ) ); ?>">
			<?php esc_html_e( 'Subscription forms', 'suitemkt3k' ); ?>
		</a>
	</div>
</div>

<div class="wmk-card">
	<table class="wmk-table">
		<thead>
			<tr>
				<th>#</th>
				<th><?php esc_html_e( 'Campaign', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Subject', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Status', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Recipients', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Opens', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Clicks', 'suitemkt3k' ); ?></th>
				<th class="wmk-col-actions"><?php esc_html_e( 'Actions', 'suitemkt3k' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( ! $campaigns ) : ?>
				<tr><td colspan="8" class="wmk-empty"><?php esc_html_e( 'No campaigns yet.', 'suitemkt3k' ); ?></td></tr>
			<?php endif; ?>
			<?php foreach ( $campaigns as $c ) : ?>
				<tr>
					<td>#<?php echo esc_html( (string) $c['id'] ); ?></td>
					<td><strong><?php echo esc_html( (string) $c['name'] ); ?></strong></td>
					<td><?php echo esc_html( (string) $c['subject'] ); ?></td>
					<td><span class="wmk-pill wmk-pill-<?php echo esc_attr( $c['status'] ); ?>"><?php echo esc_html( \SuiteMkt3k\Modules\Newsletter\Newsletter_Manager::status_label( $c['status'] ) ); ?></span></td>
					<td><?php echo esc_html( (string) $c['recipients_count'] ); ?></td>
					<td><?php echo esc_html( (string) $c['opens_count'] ); ?></td>
					<td><?php echo esc_html( (string) $c['clicks_count'] ); ?></td>
					<td class="wmk-col-actions">
						<a class="wmk-icon-btn" href="<?php echo esc_url( add_query_arg( array( 'view' => 'edit', 'id' => (int) $c['id'] ), admin_url( 'admin.php?page=suitemkt3k-newsletter' ) ) ); ?>" title="<?php esc_attr_e( 'Edit', 'suitemkt3k' ); ?>">
							<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><path d="M11 4H4v16h16v-7"/><path d="M18.5 2.5a2.12 2.12 0 1 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
						</a>
						<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="display:inline-block;">
							<input type="hidden" name="action" value="suitemkt3k_newsletter_duplicate">
							<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" type="submit" title="<?php esc_attr_e( 'Duplicate', 'suitemkt3k' ); ?>">
								<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15V5a2 2 0 0 1 2-2h10"/></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( __( 'Delete this campaign?', 'suitemkt3k' ) ); ?>')">
							<input type="hidden" name="action" value="suitemkt3k_newsletter_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"><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>
