<?php
/**
 * Admin display: Interest Subscribers (category/tag subscriptions).
 *
 * @package Subs_Sys
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

global $wpdb;
$table = $wpdb->prefix . 'subscription_interest_subscriptions';
$rows  = array();

if ( Subs_Sys_Interest_Subscription::table_exists() ) {
	$rows = $wpdb->get_results( "SELECT * FROM $table ORDER BY created_at DESC LIMIT 200" );
}
?>
<div class="wrap">
	<h1><?php esc_html_e( 'Interest Subscribers', 'subscription-system' ); ?></h1>
	<p><?php esc_html_e( 'Users who subscribed via the popup modal and selected categories/tags.', 'subscription-system' ); ?></p>

	<table class="widefat striped">
		<thead>
			<tr>
				<th><?php esc_html_e( 'Name', 'subscription-system' ); ?></th>
				<th><?php esc_html_e( 'Email', 'subscription-system' ); ?></th>
				<th><?php esc_html_e( 'Categories', 'subscription-system' ); ?></th>
				<th><?php esc_html_e( 'Tags', 'subscription-system' ); ?></th>
				<th><?php esc_html_e( 'Plan', 'subscription-system' ); ?></th>
				<th><?php esc_html_e( 'Type', 'subscription-system' ); ?></th>
				<th><?php esc_html_e( 'Status', 'subscription-system' ); ?></th>
				<th><?php esc_html_e( 'Date', 'subscription-system' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( empty( $rows ) ) : ?>
				<tr>
					<td colspan="8"><?php esc_html_e( 'No interest subscriptions yet. Add the [subscriptionsystem_subscribe] shortcode to a page.', 'subscription-system' ); ?></td>
				</tr>
			<?php else : ?>
				<?php foreach ( $rows as $row ) : ?>
					<?php
					$category_ids = json_decode( $row->category_ids, true ) ?: array();
					$tag_ids      = json_decode( $row->tag_ids, true ) ?: array();
					$cat_names    = array();
					$tag_names    = array();

					foreach ( $category_ids as $cat_id ) {
						$term = get_term( $cat_id, 'category' );
						if ( $term && ! is_wp_error( $term ) ) {
							$cat_names[] = $term->name;
						}
					}

					foreach ( $tag_ids as $tag_id ) {
						$term = get_term( $tag_id, 'post_tag' );
						if ( $term && ! is_wp_error( $term ) ) {
							$tag_names[] = $term->name;
						}
					}

					$plan_name = '—';
					if ( ! empty( $row->plan_id ) ) {
						$plan = Subs_Sys_Interest_Subscription::get_plan_by_id( (int) $row->plan_id );
						if ( $plan ) {
							$plan_name = $plan->plan_name;
						}
					}
					?>
					<tr>
						<td><?php echo esc_html( $row->subscriber_name ); ?></td>
						<td><?php echo esc_html( $row->subscriber_email ); ?></td>
						<td><?php echo esc_html( implode( ', ', $cat_names ) ?: '—' ); ?></td>
						<td><?php echo esc_html( implode( ', ', $tag_names ) ?: '—' ); ?></td>
						<td><?php echo esc_html( $plan_name ); ?></td>
						<td><?php echo esc_html( ucfirst( $row->plan_type ) ); ?></td>
						<td><?php echo esc_html( ucfirst( $row->status ) ); ?></td>
						<td><?php echo esc_html( mysql2date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $row->created_at ) ); ?></td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>
</div>
