<?php
/**
 * Subscribers list view.
 *
 * @var array  $subscribers
 * @var string $nonce
 * @var string $search
 * @var string $source
 * @var string $status
 * @package SuiteMkt3k
 */
defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$rows  = $subscribers['rows'] ?? array();
$total = $subscribers['total'] ?? 0;
$pages = $subscribers['pages'] ?? 1;
$paged = $subscribers['paged'] ?? 1;
?>
<div class="wmk-toolbar wmk-card" x-data="{ createOpen: false }">
	<form method="get" class="wmk-toolbar-form">
		<input type="hidden" name="page" value="suitemkt3k-subscribers">
		<input type="search" name="s" value="<?php echo esc_attr( $search ); ?>" class="wmk-input" placeholder="<?php esc_attr_e( 'Search by name or email…', 'suitemkt3k' ); ?>">
		<select name="source" class="wmk-input">
			<option value=""><?php esc_html_e( 'All sources', 'suitemkt3k' ); ?></option>
			<?php foreach ( \SuiteMkt3k\Modules\Subscribers\Subscriber_Manager::sources() as $key => $label ) : ?>
				<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $source, $key ); ?>><?php echo esc_html( $label ); ?></option>
			<?php endforeach; ?>
		</select>
		<select name="status" class="wmk-input">
			<option value=""><?php esc_html_e( 'All statuses', 'suitemkt3k' ); ?></option>
			<?php foreach ( \SuiteMkt3k\Modules\Subscribers\Subscriber_Manager::statuses() as $key => $label ) : ?>
				<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $status, $key ); ?>><?php echo esc_html( $label ); ?></option>
			<?php endforeach; ?>
		</select>
		<button type="submit" class="wmk-btn wmk-btn-ghost"><?php esc_html_e( 'Filter', 'suitemkt3k' ); ?></button>
	</form>
	<div class="wmk-toolbar-actions" style="margin-left:auto; display:flex; gap:8px;">
		<button type="button" class="wmk-btn wmk-btn-primary" @click="createOpen = true">
			<?php esc_html_e( '+ New', 'suitemkt3k' ); ?>
		</button>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
			<input type="hidden" name="action" value="suitemkt3k_subscribers_export">
			<?php wp_nonce_field( \SuiteMkt3k\Security\Nonces::ADMIN_ACTION ); ?>
			<button class="wmk-btn wmk-btn-ghost" type="submit"><?php esc_html_e( 'Export CSV', 'suitemkt3k' ); ?></button>
		</form>
	</div>

	<div class="wmk-modal" x-show="createOpen" x-cloak @keydown.escape.window="createOpen = false" style="display:none;">
		<div class="wmk-modal-backdrop" @click="createOpen = false"></div>
		<div class="wmk-modal-dialog">
			<div class="wmk-modal-header">
				<h3><?php esc_html_e( 'New subscriber', 'suitemkt3k' ); ?></h3>
				<button type="button" class="wmk-icon-btn" @click="createOpen = false" aria-label="<?php esc_attr_e( 'Close', 'suitemkt3k' ); ?>">×</button>
			</div>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wmk-modal-body">
				<input type="hidden" name="action" value="suitemkt3k_subscriber_save">
				<?php wp_nonce_field( \SuiteMkt3k\Security\Nonces::ADMIN_ACTION ); ?>
				<input type="hidden" name="id" value="0">
				<label class="wmk-field">
					<span><?php esc_html_e( 'Name', 'suitemkt3k' ); ?></span>
					<input class="wmk-input" type="text" name="name" placeholder="<?php esc_attr_e( 'e.g. Maria López', 'suitemkt3k' ); ?>">
				</label>
				<label class="wmk-field">
					<span><?php esc_html_e( 'Email', 'suitemkt3k' ); ?> *</span>
					<input class="wmk-input" type="email" name="email" required placeholder="name@example.com">
				</label>
				<label class="wmk-field">
					<span><?php esc_html_e( 'Birth date', 'suitemkt3k' ); ?></span>
					<input class="wmk-input" type="date" name="birth_date">
				</label>
				<label class="wmk-field">
					<span><?php esc_html_e( 'Status', 'suitemkt3k' ); ?></span>
					<select class="wmk-input" name="status">
						<?php foreach ( \SuiteMkt3k\Modules\Subscribers\Subscriber_Manager::statuses() as $key => $label ) : ?>
							<option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, 'active' ); ?>><?php echo esc_html( $label ); ?></option>
						<?php endforeach; ?>
					</select>
				</label>
				<div class="wmk-modal-footer">
					<button class="wmk-btn wmk-btn-ghost" type="button" @click="createOpen = false"><?php esc_html_e( 'Cancel', 'suitemkt3k' ); ?></button>
					<button class="wmk-btn wmk-btn-primary" type="submit"><?php esc_html_e( 'Create subscriber', 'suitemkt3k' ); ?></button>
				</div>
			</form>
		</div>
	</div>
</div>

<div class="wmk-card" x-data="{ editId: 0, editData: {}, openEdit(id, data){ this.editId = id; this.editData = data; }, closeEdit(){ this.editId = 0; }, deleteId: 0, openDelete(id){ this.deleteId = id; }, closeDelete(){ this.deleteId = 0; } }">
	<table class="wmk-table">
		<thead>
			<tr>
				<th>#</th>
				<th><?php esc_html_e( 'Name', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Email', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Birth date', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Subscribed', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Source', '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 ( ! $rows ) : ?>
			<tr><td colspan="8" class="wmk-empty"><?php esc_html_e( 'No subscribers yet.', 'suitemkt3k' ); ?></td></tr>
		<?php endif; ?>
		<?php foreach ( $rows as $r ) : ?>
			<tr>
				<td>#<?php echo esc_html( (string) $r['id'] ); ?></td>
				<td><?php echo esc_html( (string) $r['name'] ); ?></td>
				<td><?php echo esc_html( (string) $r['email'] ); ?></td>
				<td><?php echo esc_html( (string) ( $r['birth_date'] ?? '—' ) ); ?></td>
				<td><?php echo esc_html( (string) $r['subscribed_at'] ); ?></td>
				<td><?php echo esc_html( \SuiteMkt3k\Modules\Subscribers\Subscriber_Manager::source_label( $r ) ); ?></td>
				<td><span class="wmk-pill wmk-pill-<?php echo esc_attr( $r['status'] ); ?>"><?php echo esc_html( \SuiteMkt3k\Modules\Subscribers\Subscriber_Manager::statuses()[ $r['status'] ] ?? $r['status'] ); ?></span></td>
				<td class="wmk-col-actions">
					<button class="wmk-icon-btn" type="button"
						@click="openEdit(<?php echo (int) $r['id']; ?>, <?php echo esc_attr( wp_json_encode( array( 'name' => $r['name'], 'email' => $r['email'], 'birth_date' => $r['birth_date'], 'status' => $r['status'] ) ) ); ?>)" 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>
					</button>
					<button class="wmk-icon-btn wmk-icon-btn-danger" type="button"
						@click="openDelete(<?php echo (int) $r['id']; ?>)" 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>
				</td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>

	<?php if ( $pages > 1 ) : ?>
	<div class="wmk-pagination">
	<?php
	for ( $i = 1; $i <= $pages; $i++ ) {
		$url = add_query_arg( array( 'paged' => $i ) );
		printf( '<a href="%s" class="%s">%d</a>', esc_url( $url ), $i === (int) $paged ? 'is-active' : '', (int) $i );
	}
	?>
	</div>
	<?php endif; ?>

	<div class="wmk-modal" x-show="editId > 0" x-cloak @keydown.escape.window="closeEdit()" style="display:none;">
		<div class="wmk-modal-backdrop" @click="closeEdit()"></div>
		<div class="wmk-modal-dialog">
			<div class="wmk-modal-header">
				<h3><?php esc_html_e( 'Edit subscriber', 'suitemkt3k' ); ?></h3>
				<button type="button" class="wmk-icon-btn" @click="closeEdit()" aria-label="<?php esc_attr_e( 'Close', 'suitemkt3k' ); ?>">×</button>
			</div>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wmk-modal-body">
				<input type="hidden" name="action" value="suitemkt3k_subscriber_save">
				<?php wp_nonce_field( \SuiteMkt3k\Security\Nonces::ADMIN_ACTION ); ?>
				<input type="hidden" name="id" :value="editId">
				<label class="wmk-field">
					<span><?php esc_html_e( 'Name', 'suitemkt3k' ); ?></span>
					<input class="wmk-input" type="text" name="name" :value="editData.name">
				</label>
				<label class="wmk-field">
					<span><?php esc_html_e( 'Email', 'suitemkt3k' ); ?></span>
					<input class="wmk-input" type="email" name="email" :value="editData.email" required>
				</label>
				<label class="wmk-field">
					<span><?php esc_html_e( 'Birth date', 'suitemkt3k' ); ?></span>
					<input class="wmk-input" type="date" name="birth_date" :value="editData.birth_date">
				</label>
				<label class="wmk-field">
					<span><?php esc_html_e( 'Status', 'suitemkt3k' ); ?></span>
					<select class="wmk-input" name="status">
						<?php foreach ( \SuiteMkt3k\Modules\Subscribers\Subscriber_Manager::statuses() as $key => $label ) : ?>
							<option value="<?php echo esc_attr( $key ); ?>" :selected="editData.status === '<?php echo esc_js( $key ); ?>'"><?php echo esc_html( $label ); ?></option>
						<?php endforeach; ?>
					</select>
				</label>
				<div class="wmk-modal-footer">
					<button class="wmk-btn wmk-btn-ghost" type="button" @click="closeEdit()"><?php esc_html_e( 'Cancel', 'suitemkt3k' ); ?></button>
					<button class="wmk-btn wmk-btn-primary" type="submit"><?php esc_html_e( 'Save changes', 'suitemkt3k' ); ?></button>
				</div>
			</form>
		</div>
	</div>

	<div class="wmk-modal" x-show="deleteId > 0" x-cloak @keydown.escape.window="closeDelete()" style="display:none;">
		<div class="wmk-modal-backdrop" @click="closeDelete()"></div>
		<div class="wmk-modal-dialog">
			<div class="wmk-modal-header">
				<h3><?php esc_html_e( 'Delete subscriber', 'suitemkt3k' ); ?></h3>
				<button type="button" class="wmk-icon-btn" @click="closeDelete()" aria-label="<?php esc_attr_e( 'Close', 'suitemkt3k' ); ?>">×</button>
			</div>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wmk-modal-body">
				<input type="hidden" name="action" value="suitemkt3k_subscriber_delete">
				<?php wp_nonce_field( \SuiteMkt3k\Security\Nonces::ADMIN_ACTION ); ?>
				<input type="hidden" name="id" :value="deleteId">
				<p><?php esc_html_e( 'This will permanently remove the subscriber and all their tracking data. This action cannot be undone.', 'suitemkt3k' ); ?></p>
				<div class="wmk-modal-footer">
					<button class="wmk-btn wmk-btn-ghost" type="button" @click="closeDelete()"><?php esc_html_e( 'Cancel', 'suitemkt3k' ); ?></button>
					<button class="wmk-btn wmk-btn-danger" type="submit"><?php esc_html_e( 'Yes, delete', 'suitemkt3k' ); ?></button>
				</div>
			</form>
		</div>
	</div>
</div>
