<?php
/**
 * Public category plan grid with branch and type filters.
 *
 * @package Lucrativeit_Subscription_Recurring_Billing
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Included template; variables are local to the including scope.

$instance_id        = isset( $grid_instance_id ) ? $grid_instance_id : ( isset( $instance_id ) ? $instance_id : 'lucrsure-plan-grid' );
$heading            = isset( $heading ) ? trim( (string) $heading ) : '';
$subtitle           = isset( $subtitle ) ? trim( (string) $subtitle ) : '';
$plans              = isset( $plans ) ? $plans : array();
$filter_branches    = isset( $filter_branches ) ? $filter_branches : array();
$filter_intervals   = isset( $filter_intervals ) ? $filter_intervals : array();
$branch_names       = isset( $branch_names ) ? $branch_names : array();
$branch_images      = isset( $branch_images ) ? $branch_images : array();
$show_branch_filter = ! empty( $show_branch_filter );
$show_type_filter   = ! empty( $show_type_filter );
$checkout_url       = isset( $checkout_url ) ? $checkout_url : '#lucrsure-plans';
$button_text        = isset( $button_text ) ? $button_text : __( 'Join now', 'lucrativeit-subscription-recurring-billing' );
$intervals          = lucrsure_billing_intervals();
$interval_suffix    = array(
	'day'      => __( 'day', 'lucrativeit-subscription-recurring-billing' ),
	'week'     => __( 'wk', 'lucrativeit-subscription-recurring-billing' ),
	'month'    => __( 'mo', 'lucrativeit-subscription-recurring-billing' ),
	'year'     => __( 'yr', 'lucrativeit-subscription-recurring-billing' ),
	'lifetime' => __( 'once', 'lucrativeit-subscription-recurring-billing' ),
);
$show_filters       = $show_branch_filter || $show_type_filter;
$plan_count         = count( $plans );
$return_url         = remove_query_arg( array( 'lucrsure_plan', 'lucrsure_return' ) );
?>

<section
	class="lucrsure-plan-grid"
	id="<?php echo esc_attr( $instance_id ); ?>"
	data-active-branch="0"
	data-active-type=""
	data-branch-images="<?php echo esc_attr( wp_json_encode( $branch_images ) ); ?>"
>
	<?php if ( $heading || $subtitle ) : ?>
		<header class="lucrsure-plan-grid-header">
			<?php if ( $heading ) : ?>
				<h2 class="lucrsure-plan-grid-title"><?php echo esc_html( $heading ); ?></h2>
			<?php endif; ?>
			<?php if ( $subtitle ) : ?>
				<p class="lucrsure-plan-grid-subtitle"><?php echo esc_html( $subtitle ); ?></p>
			<?php endif; ?>
		</header>
	<?php endif; ?>

	<?php if ( $show_filters && ! empty( $plans ) ) : ?>
		<div class="lucrsure-plan-grid-toolbar">
			<p
				class="lucrsure-plan-grid-count"
				data-template-one="<?php echo esc_attr( /* translators: %s: number of plans */ __( 'Showing %s plan', 'lucrativeit-subscription-recurring-billing' ) ); ?>"
				data-template-many="<?php echo esc_attr( /* translators: %s: number of plans */ __( 'Showing %s plans', 'lucrativeit-subscription-recurring-billing' ) ); ?>"
			>
				<?php
				echo esc_html(
					sprintf(
						/* translators: %s: number of plans */
						_n( 'Showing %s plan', 'Showing %s plans', $plan_count, 'lucrativeit-subscription-recurring-billing' ),
						(string) $plan_count
					)
				);
				?>
			</p>
			<div class="lucrsure-plan-grid-controls">
				<?php if ( $show_branch_filter ) : ?>
					<div class="lucrsure-plan-grid-field">
						<select class="lucrsure-plan-grid-select" data-filter="branch" aria-label="<?php esc_attr_e( 'Filter by branch', 'lucrativeit-subscription-recurring-billing' ); ?>">
							<option value="0"><?php esc_html_e( 'All branches', 'lucrativeit-subscription-recurring-billing' ); ?></option>
							<?php foreach ( $filter_branches as $branch ) : ?>
								<option value="<?php echo esc_attr( (string) $branch['id'] ); ?>"><?php echo esc_html( $branch['name'] ); ?></option>
							<?php endforeach; ?>
						</select>
					</div>
				<?php endif; ?>
				<?php if ( $show_type_filter ) : ?>
					<div class="lucrsure-plan-grid-field">
						<select class="lucrsure-plan-grid-select" data-filter="type" aria-label="<?php esc_attr_e( 'Filter by type', 'lucrativeit-subscription-recurring-billing' ); ?>">
							<option value=""><?php esc_html_e( 'All types', 'lucrativeit-subscription-recurring-billing' ); ?></option>
							<?php foreach ( $filter_intervals as $interval_key ) : ?>
								<option value="<?php echo esc_attr( $interval_key ); ?>">
									<?php echo esc_html( isset( $intervals[ $interval_key ] ) ? $intervals[ $interval_key ] : $interval_key ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</div>
				<?php endif; ?>
				<button type="button" class="lucrsure-plan-grid-reset" hidden>
					<?php esc_html_e( 'Clear', 'lucrativeit-subscription-recurring-billing' ); ?>
				</button>
			</div>
		</div>
	<?php endif; ?>

	<?php if ( empty( $plans ) ) : ?>
		<p class="lucrsure-public-notice"><?php esc_html_e( 'No active plans in this category yet.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	<?php else : ?>
		<div class="lucrsure-plan-grid-cards">
			<?php foreach ( $plans as $plan ) : ?>
				<?php
				$plan_branch_id  = isset( $plan['branch_id'] ) ? (int) $plan['branch_id'] : 0;
				$plan_branch     = $plan_branch_id && isset( $branch_names[ $plan_branch_id ] ) ? $branch_names[ $plan_branch_id ] : '';
				$plan_image      = LUCRSURE_Plan_Repository::thumbnail_url( $plan, 'large' );
				$has_plan_image  = '' !== $plan_image;
				$branch_image    = ( $plan_branch_id && isset( $branch_images[ $plan_branch_id ] ) ) ? $branch_images[ $plan_branch_id ] : '';
				$image_url       = $has_plan_image ? $plan_image : $branch_image;
				$interval_key    = sanitize_key( $plan['billing_interval'] );
				$interval_name   = isset( $intervals[ $interval_key ] ) ? $intervals[ $interval_key ] : $interval_key;
				$interval_lbl    = isset( $interval_suffix[ $interval_key ] ) ? $interval_suffix[ $interval_key ] : $interval_key;
				$price_display   = lucrsure_plan_format_price_display( $plan );
				$plan_url        = $checkout_url;
				if ( $plan_url && '#' !== $plan_url[0] ) {
					$hash     = '';
					$base     = $plan_url;
					$hash_pos = strpos( $plan_url, '#' );
					if ( false !== $hash_pos ) {
						$base = substr( $plan_url, 0, $hash_pos );
						$hash = substr( $plan_url, $hash_pos );
					}
					$plan_url = add_query_arg( 'lucrsure_plan', (int) $plan['id'], $base );
					$checkout_path = wp_parse_url( $base, PHP_URL_PATH );
					$return_path   = wp_parse_url( $return_url, PHP_URL_PATH );
					if ( $return_url && $checkout_path !== $return_path ) {
						$plan_url = add_query_arg( 'lucrsure_return', $return_url, $plan_url );
					}
					$plan_url .= $hash;
				}
				$image_alt = $has_plan_image ? $plan['name'] : ( $plan_branch ? $plan_branch : $plan['name'] );
				$features  = LUCRSURE_Plan_Repository::features( $plan );
				$meta      = array_filter(
					array(
						$plan_branch,
						$interval_name,
					)
				);
				?>
				<article
					class="lucrsure-plan-grid-card<?php echo ! empty( $plan['is_popular'] ) ? ' is-popular' : ''; ?>"
					data-plan-id="<?php echo esc_attr( (string) $plan['id'] ); ?>"
					data-branch-id="<?php echo esc_attr( (string) $plan_branch_id ); ?>"
					data-type="<?php echo esc_attr( $interval_key ); ?>"
					data-interval="<?php echo esc_attr( $interval_key ); ?>"
					data-name="<?php echo esc_attr( $plan['name'] ); ?>"
					data-price="<?php echo esc_attr( $price_display['effective'] ); ?>"
					data-interval-label="<?php echo esc_attr( $interval_lbl ); ?>"
					data-trial="<?php echo esc_attr( (string) (int) $plan['trial_days'] ); ?>"
					data-has-plan-image="<?php echo $has_plan_image ? '1' : '0'; ?>"
					data-default-image="<?php echo esc_url( $image_url ); ?>"
				>
					<a class="lucrsure-plan-grid-media" href="<?php echo esc_url( $plan_url ); ?>">
						<img
							class="lucrsure-plan-grid-photo"
							<?php if ( $image_url ) : ?>
								src="<?php echo esc_url( $image_url ); ?>"
							<?php endif; ?>
							alt="<?php echo esc_attr( $image_alt ); ?>"
							loading="lazy"
							<?php echo $image_url ? '' : ' hidden'; ?>
						/>
						<div class="lucrsure-plan-grid-media-placeholder" aria-hidden="true"<?php echo $image_url ? ' hidden' : ''; ?>></div>
						<?php if ( $price_display['has_special'] ) : ?>
							<span class="lucrsure-plan-grid-badge lucrsure-plan-grid-badge--sale"><?php esc_html_e( 'Sale', 'lucrativeit-subscription-recurring-billing' ); ?></span>
						<?php elseif ( ! empty( $plan['is_popular'] ) ) : ?>
							<span class="lucrsure-plan-grid-badge"><?php esc_html_e( 'Popular', 'lucrativeit-subscription-recurring-billing' ); ?></span>
						<?php endif; ?>
					</a>
					<div class="lucrsure-plan-grid-body">
						<?php if ( ! empty( $meta ) ) : ?>
							<p class="lucrsure-plan-grid-meta"><?php echo esc_html( implode( ' · ', $meta ) ); ?></p>
						<?php endif; ?>
						<h3 class="lucrsure-plan-grid-name">
							<a href="<?php echo esc_url( $plan_url ); ?>"><?php echo esc_html( $plan['name'] ); ?></a>
						</h3>
						<?php if ( ! empty( $plan['description'] ) ) : ?>
							<p class="lucrsure-plan-grid-desc"><?php echo esc_html( $plan['description'] ); ?></p>
						<?php endif; ?>
						<div class="lucrsure-plan-grid-pricing">
							<?php if ( $price_display['has_special'] ) : ?>
								<span class="lucrsure-plan-grid-amount lucrsure-plan-grid-amount--regular"><?php echo esc_html( $price_display['regular'] ); ?></span>
							<?php endif; ?>
							<span class="lucrsure-plan-grid-amount"><?php echo esc_html( $price_display['effective'] ); ?></span>
						</div>
						<?php if ( (int) $plan['trial_days'] > 0 ) : ?>
							<p class="lucrsure-plan-grid-trial">
								<?php
								echo esc_html(
									sprintf(
										/* translators: %d days */
										__( '%d-day free trial', 'lucrativeit-subscription-recurring-billing' ),
										(int) $plan['trial_days']
									)
								);
								?>
							</p>
						<?php endif; ?>
						<?php if ( ! empty( $features ) ) : ?>
							<ul class="lucrsure-public-features lucrsure-plan-grid-features">
								<?php foreach ( $features as $feature ) : ?>
									<li><?php echo esc_html( $feature ); ?></li>
								<?php endforeach; ?>
							</ul>
						<?php endif; ?>
						<a class="lucrsure-plan-grid-cta" href="<?php echo esc_url( $plan_url ); ?>">
							<?php echo esc_html( $button_text ); ?>
						</a>
					</div>
				</article>
			<?php endforeach; ?>
		</div>
		<p class="lucrsure-plan-grid-empty lucrsure-public-notice" hidden><?php esc_html_e( 'No plans match these filters.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	<?php endif; ?>
</section>
