<?php
/**
 * Coupon list view.
 *
 * @var array  $coupons
 * @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-coupons' ) ) ); ?>">
			<?php esc_html_e( '+ New coupon campaign', 'suitemkt3k' ); ?>
		</a>
		<a class="wmk-btn wmk-btn-ghost" href="<?php echo esc_url( add_query_arg( array( 'view' => 'calendar' ), admin_url( 'admin.php?page=suitemkt3k-coupons' ) ) ); ?>">
			<?php esc_html_e( 'Calendar view', 'suitemkt3k' ); ?>
		</a>
		<a class="wmk-btn wmk-btn-ghost" href="<?php echo esc_url( add_query_arg( array( 'view' => 'email_template' ), admin_url( 'admin.php?page=suitemkt3k-coupons' ) ) ); ?>">
			<?php esc_html_e( 'Edit coupon opt-in email template', 'suitemkt3k' ); ?>
		</a>
	</div>
</div>

<div class="wmk-card">
	<table class="wmk-table">
		<thead>
			<tr>
				<th>#</th>
				<th><?php esc_html_e( 'Name', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Format', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Discount', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Status', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Uses', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Dates', 'suitemkt3k' ); ?></th>
				<th class="wmk-col-actions"><?php esc_html_e( 'Actions', 'suitemkt3k' ); ?></th>
			</tr>
		</thead>
		<tbody>
		<?php if ( ! $coupons ) : ?>
			<tr><td colspan="8" class="wmk-empty"><?php esc_html_e( 'No coupon campaigns yet. Create your first one!', 'suitemkt3k' ); ?></td></tr>
		<?php endif; ?>
		<?php foreach ( $coupons 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( 'optin' === $c['format_type'] ? __( 'Opt-in', 'suitemkt3k' ) : __( 'Classic', 'suitemkt3k' ) ); ?></td>
				<td>
					<?php
					echo 'percent' === $c['discount_type']
						? esc_html( rtrim( rtrim( number_format( (float) $c['discount_value'], 2, '.', '' ), '0' ), '.' ) . '%' )
						: esc_html( rtrim( rtrim( number_format( (float) $c['discount_value'], 2, '.', '' ), '0' ), '.' ) );
					?>
				</td>
				<td><span class="wmk-pill wmk-pill-<?php echo esc_attr( $c['status'] ); ?>"><?php echo esc_html( \SuiteMkt3k\Modules\Coupons\Coupon_Manager::status_label( $c['status'] ) ); ?></span></td>
				<td><?php echo esc_html( (string) $c['usage_count'] ); ?><?php echo ! empty( $c['usage_limit'] ) ? esc_html( ' / ' . $c['usage_limit'] ) : ''; ?></td>
				<td><small><?php echo esc_html( ( $c['starts_at'] ?? '—' ) . ' → ' . ( $c['ends_at'] ?? '∞' ) ); ?></small></td>
				<td class="wmk-col-actions">
					<?php
					$can_toggle = in_array( $c['status'], array( 'active', 'paused', 'draft' ), true );
					$is_active  = 'active' === $c['status'];
					?>
					<?php if ( $can_toggle ) : ?>
						<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="display:inline-block;">
							<input type="hidden" name="action" value="suitemkt3k_coupon_toggle">
							<input type="hidden" name="id" value="<?php echo (int) $c['id']; ?>">
							<?php wp_nonce_field( \SuiteMkt3k\Security\Nonces::ADMIN_ACTION ); ?>
							<?php if ( $is_active ) : ?>
								<button class="wmk-icon-btn" type="submit" title="<?php esc_attr_e( 'Pause', 'suitemkt3k' ); ?>">
									<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><rect x="6" y="5" width="4" height="14" rx="1"/><rect x="14" y="5" width="4" height="14" rx="1"/></svg>
								</button>
							<?php else : ?>
								<button class="wmk-icon-btn" type="submit" title="<?php esc_attr_e( 'Activate', 'suitemkt3k' ); ?>" style="color:var(--wmk-success);">
									<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><polygon points="6 4 20 12 6 20 6 4"/></svg>
								</button>
							<?php endif; ?>
						</form>
					<?php endif; ?>
					<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-coupons' ) ) ); ?>" 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_coupon_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_coupon_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>
