<?php
/**
 * Public plans + checkout shortcode template.
 *
 * @package Lucrativeit_Subscription_Recurring_Billing
 */

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

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

// Defaults when the template is loaded without the shortcode view context.
$instance_id       = isset( $instance_id ) ? $instance_id : 'lucrsure-plans';
$intervals         = isset( $intervals ) ? $intervals : lucrsure_billing_intervals();
$interval_order    = isset( $interval_order ) ? $interval_order : array_keys( $intervals );
$intervals_present = isset( $intervals_present ) ? $intervals_present : array();
$default_interval  = isset( $default_interval ) ? $default_interval : 'month';
$show_intervals    = isset( $show_intervals ) ? $show_intervals : true;
$popular_plan_id   = isset( $popular_plan_id ) ? (int) $popular_plan_id : 0;
$plans_heading     = isset( $plans_heading ) ? $plans_heading : '';
$subtitle          = isset( $subtitle ) ? $subtitle : '';
$footer            = isset( $footer ) ? $footer : '';
$plans             = isset( $plans ) ? $plans : array();
$gateways          = isset( $gateways ) ? $gateways : array();
$default_gateway   = isset( $default_gateway ) ? $default_gateway : 'manual';
$checkout_ajax_url = isset( $checkout_ajax_url ) ? $checkout_ajax_url : admin_url( 'admin-ajax.php' );
$checkout_nonce    = isset( $checkout_nonce ) ? $checkout_nonce : '';
$template          = isset( $template ) ? lucrsure_sanitize_plans_template( $template ) : 'cards';
$is_grid           = 'grid' === $template;
$is_list           = 'list' === $template;
$template_modifier = $is_grid ? 'grid' : ( $is_list ? 'list' : 'cards' );

$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' ),
);

$tab_labels = array(
	'day'      => __( 'Day', 'lucrativeit-subscription-recurring-billing' ),
	'week'     => __( 'Week', 'lucrativeit-subscription-recurring-billing' ),
	'month'    => __( 'Month', 'lucrativeit-subscription-recurring-billing' ),
	'year'     => __( 'Year', 'lucrativeit-subscription-recurring-billing' ),
	'lifetime' => __( 'Lifetime', 'lucrativeit-subscription-recurring-billing' ),
);

$panel_order = array();
foreach ( ( ! empty( $interval_order ) ? $interval_order : array_keys( $intervals ) ) as $key ) {
	if ( ! empty( $intervals_present[ $key ] ) ) {
		$panel_order[] = $key;
	}
}

if ( ! in_array( $default_interval, $panel_order, true ) && ! empty( $panel_order ) ) {
	$default_interval = $panel_order[0];
}

$has_interval_tabs = $show_intervals && count( $panel_order ) > 1;
?>

<div class="lucrsure-public lucrsure-public--template-<?php echo esc_attr( $template_modifier ); ?>" id="<?php echo esc_attr( $instance_id ); ?>" data-default-interval="<?php echo esc_attr( $default_interval ); ?>" data-template="<?php echo esc_attr( $template ); ?>" data-ajax-url="<?php echo esc_url( $checkout_ajax_url ); ?>" data-checkout-nonce="<?php echo esc_attr( $checkout_nonce ); ?>">
	<div class="lucrsure-public-plans-step" id="lucrsure-plans">
		<?php if ( $is_grid ) : ?>
			<?php include LUCRSURE_PLUGIN_DIR . 'public/views/plan-grid.php'; ?>
		<?php else : ?>
		<?php if ( $plans_heading || $subtitle ) : ?>
			<header class="lucrsure-public-header">
				<?php if ( $plans_heading ) : ?>
					<h2 class="lucrsure-public-title"><?php echo esc_html( $plans_heading ); ?></h2>
				<?php endif; ?>
				<?php if ( $subtitle ) : ?>
					<p class="lucrsure-public-subtitle"><?php echo esc_html( $subtitle ); ?></p>
				<?php endif; ?>
			</header>
		<?php endif; ?>

		<?php if ( $has_interval_tabs ) : ?>
			<div class="lucrsure-public-interval-section">
				<div class="lucrsure-public-intervals" role="tablist" aria-label="<?php esc_attr_e( 'Billing period', 'lucrativeit-subscription-recurring-billing' ); ?>">
					<?php foreach ( $panel_order as $key ) : ?>
						<?php
						$tab_label   = isset( $tab_labels[ $key ] ) ? $tab_labels[ $key ] : $key;
						$is_active   = $key === $default_interval;
						$tab_classes = 'lucrsure-public-interval' . ( $is_active ? ' is-active' : '' );
						?>
						<button
							type="button"
							class="<?php echo esc_attr( $tab_classes ); ?>"
							data-interval="<?php echo esc_attr( $key ); ?>"
							role="tab"
							id="<?php echo esc_attr( $instance_id . '-tab-' . $key ); ?>"
							aria-controls="<?php echo esc_attr( $instance_id . '-panel-' . $key ); ?>"
							aria-selected="<?php echo $is_active ? 'true' : 'false'; ?>"
						><?php echo esc_html( $tab_label ); ?></button>
					<?php endforeach; ?>
				</div>
			</div>
		<?php endif; ?>

		<?php foreach ( $panel_order as $panel_interval ) : ?>
			<?php
			$panel_plans = array();
			foreach ( $plans as $plan ) {
				if ( sanitize_key( $plan['billing_interval'] ) === $panel_interval ) {
					$panel_plans[] = $plan;
				}
			}
			if ( empty( $panel_plans ) ) {
				continue;
			}
			?>
			<div
				class="lucrsure-public-plan-panel"
				id="<?php echo esc_attr( $instance_id . '-panel-' . $panel_interval ); ?>"
				role="tabpanel"
				data-interval-panel="<?php echo esc_attr( $panel_interval ); ?>"
				<?php echo $panel_interval === $default_interval ? '' : ' hidden'; ?>
			>
				<div class="lucrsure-public-plan-list">
					<?php foreach ( $panel_plans as $plan ) : ?>
						<?php
						$features     = LUCRSURE_Plan_Repository::features( $plan );
						$interval_key = sanitize_key( $plan['billing_interval'] );
						$interval_lbl = isset( $interval_suffix[ $interval_key ] ) ? $interval_suffix[ $interval_key ] : $interval_key;
						$is_popular   = $popular_plan_id
							? (int) $plan['id'] === $popular_plan_id
							: ! empty( $plan['is_popular'] );
						$trial_days   = (int) $plan['trial_days'];
						$price_display = lucrsure_plan_format_price_display( $plan );
						$price_parts   = $price_display['effective'];
						$card_class   = 'lucrsure-public-plan-card' . ( $is_popular ? ' is-popular' : '' );
						?>
						<article
							class="<?php echo esc_attr( $card_class ); ?>"
							data-plan-id="<?php echo esc_attr( (string) $plan['id'] ); ?>"
							data-interval="<?php echo esc_attr( $interval_key ); ?>"
							data-name="<?php echo esc_attr( $plan['name'] ); ?>"
							data-price="<?php echo esc_attr( $price_parts ); ?>"
							data-interval-label="<?php echo esc_attr( $interval_lbl ); ?>"
							data-trial="<?php echo esc_attr( (string) $trial_days ); ?>"
						>
							<?php if ( $is_popular ) : ?>
								<span class="lucrsure-public-badge"><?php esc_html_e( 'Most Popular', 'lucrativeit-subscription-recurring-billing' ); ?></span>
							<?php endif; ?>
							<div class="lucrsure-public-plan-row">
								<div class="lucrsure-public-plan-content">
									<?php if ( $is_list ) : ?>
										<div class="lucrsure-public-plan-top">
											<div class="lucrsure-public-plan-header">
												<h3 class="lucrsure-public-plan-title"><?php echo esc_html( $plan['name'] ); ?></h3>
												<?php if ( $plan['description'] ) : ?>
													<p class="lucrsure-public-plan-desc"><?php echo esc_html( $plan['description'] ); ?></p>
												<?php endif; ?>
											</div>
											<div class="lucrsure-public-plan-pricing">
												<?php if ( $price_display['has_special'] ) : ?>
													<span class="lucrsure-public-plan-amount lucrsure-public-plan-amount--regular"><?php echo esc_html( $price_display['regular'] ); ?></span>
												<?php endif; ?>
												<span class="lucrsure-public-plan-amount"><?php echo esc_html( $price_parts ); ?></span>
												<span class="lucrsure-public-plan-period">/<?php echo esc_html( $interval_lbl ); ?></span>
											</div>
											<button type="button" class="lucrsure-public-plan-cta">
												<?php
												printf(
													/* translators: %s: plan name */
													esc_html__( 'Choose %s', 'lucrativeit-subscription-recurring-billing' ),
													esc_html( $plan['name'] )
												);
												?>
											</button>
										</div>
										<?php if ( ! empty( $features ) ) : ?>
											<ul class="lucrsure-public-features">
												<?php foreach ( $features as $feature ) : ?>
													<li><?php echo esc_html( $feature ); ?></li>
												<?php endforeach; ?>
											</ul>
										<?php endif; ?>
									<?php else : ?>
									<div class="lucrsure-public-plan-header">
										<h3 class="lucrsure-public-plan-title"><?php echo esc_html( $plan['name'] ); ?></h3>
										<?php if ( $plan['description'] ) : ?>
											<p class="lucrsure-public-plan-desc"><?php echo esc_html( $plan['description'] ); ?></p>
										<?php endif; ?>
										<div class="lucrsure-public-plan-pricing">
											<?php if ( $price_display['has_special'] ) : ?>
												<span class="lucrsure-public-plan-amount lucrsure-public-plan-amount--regular"><?php echo esc_html( $price_display['regular'] ); ?></span>
											<?php endif; ?>
											<span class="lucrsure-public-plan-amount"><?php echo esc_html( $price_parts ); ?></span>
											<span class="lucrsure-public-plan-period">/<?php echo esc_html( $interval_lbl ); ?></span>
										</div>
									</div>
									<?php if ( ! empty( $features ) ) : ?>
										<ul class="lucrsure-public-features">
											<?php foreach ( $features as $feature ) : ?>
												<li><?php echo esc_html( $feature ); ?></li>
											<?php endforeach; ?>
										</ul>
									<?php endif; ?>
									<button type="button" class="lucrsure-public-plan-cta">
										<?php
										printf(
											/* translators: %s: plan name */
											esc_html__( 'Choose %s', 'lucrativeit-subscription-recurring-billing' ),
											esc_html( $plan['name'] )
										);
										?>
									</button>
									<?php endif; ?>
								</div>
							</div>
						</article>
					<?php endforeach; ?>
				</div>
			</div>
		<?php endforeach; ?>

		<?php if ( $footer ) : ?>
			<p class="lucrsure-public-footer-note"><?php echo esc_html( $footer ); ?></p>
		<?php endif; ?>
		<?php endif; ?>
	</div>

	<div class="lucrsure-public-checkout-step" hidden>
		<button type="button" class="lucrsure-public-back">&larr; <?php esc_html_e( 'Back to plans', 'lucrativeit-subscription-recurring-billing' ); ?></button>
		<div class="lucrsure-public-checkout-grid">
			<div class="lucrsure-public-checkout-form-wrap">
				<h2><?php esc_html_e( 'Your details', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
				<form class="lucrsure-public-checkout-form" novalidate>
					<input type="hidden" name="plan_id" value="" />
					<div class="lucrsure-public-field">
						<label><?php esc_html_e( 'Full name', 'lucrativeit-subscription-recurring-billing' ); ?></label>
						<input type="text" name="name" required autocomplete="name" />
					</div>
					<div class="lucrsure-public-field">
						<label><?php esc_html_e( 'Email address', 'lucrativeit-subscription-recurring-billing' ); ?></label>
						<input type="email" name="email" required autocomplete="email" />
					</div>
					<div class="lucrsure-public-field">
						<label><?php esc_html_e( 'Phone (optional)', 'lucrativeit-subscription-recurring-billing' ); ?></label>
						<input type="tel" name="phone" autocomplete="tel" />
					</div>

					<h3 class="lucrsure-public-checkout-section"><?php esc_html_e( 'Billing address', 'lucrativeit-subscription-recurring-billing' ); ?></h3>
					<div class="lucrsure-public-field">
						<label><?php esc_html_e( 'Street address', 'lucrativeit-subscription-recurring-billing' ); ?></label>
						<input type="text" name="billing_address_1" required autocomplete="address-line1" />
					</div>
					<div class="lucrsure-public-field">
						<label><?php esc_html_e( 'Apartment, suite, etc. (optional)', 'lucrativeit-subscription-recurring-billing' ); ?></label>
						<input type="text" name="billing_address_2" autocomplete="address-line2" />
					</div>
					<div class="lucrsure-public-field-row">
						<div class="lucrsure-public-field">
							<label><?php esc_html_e( 'City', 'lucrativeit-subscription-recurring-billing' ); ?></label>
							<input type="text" name="billing_city" required autocomplete="address-level2" />
						</div>
						<div class="lucrsure-public-field">
							<label><?php esc_html_e( 'State / Province', 'lucrativeit-subscription-recurring-billing' ); ?></label>
							<input type="text" name="billing_state" autocomplete="address-level1" />
						</div>
					</div>
					<div class="lucrsure-public-field-row">
						<div class="lucrsure-public-field">
							<label><?php esc_html_e( 'Postal / ZIP code', 'lucrativeit-subscription-recurring-billing' ); ?></label>
							<input type="text" name="billing_postcode" required autocomplete="postal-code" />
						</div>
						<div class="lucrsure-public-field">
							<label><?php esc_html_e( 'Country', 'lucrativeit-subscription-recurring-billing' ); ?></label>
							<input type="text" name="billing_country" required autocomplete="country-name" />
						</div>
					</div>

					<?php if ( count( $gateways ) > 1 ) : ?>
						<div class="lucrsure-public-field">
							<label><?php esc_html_e( 'Payment method', 'lucrativeit-subscription-recurring-billing' ); ?></label>
							<select name="gateway">
								<?php foreach ( $gateways as $gateway_id => $gateway_title ) : ?>
									<option value="<?php echo esc_attr( $gateway_id ); ?>" <?php selected( $default_gateway, $gateway_id ); ?>><?php echo esc_html( $gateway_title ); ?></option>
								<?php endforeach; ?>
							</select>
						</div>
					<?php else : ?>
						<input type="hidden" name="gateway" value="<?php echo esc_attr( $default_gateway ); ?>" />
					<?php endif; ?>
					<p class="lucrsure-public-checkout-error" hidden></p>
					<button type="submit" class="lucrsure-public-submit"><?php esc_html_e( 'Complete subscription', 'lucrativeit-subscription-recurring-billing' ); ?></button>
				</form>
			</div>
			<aside class="lucrsure-public-summary">
				<p class="lucrsure-public-summary-label"><?php esc_html_e( 'Order summary', 'lucrativeit-subscription-recurring-billing' ); ?></p>
				<div class="lucrsure-public-summary-head">
					<strong class="lucrsure-public-summary-plan">—</strong>
					<strong class="lucrsure-public-summary-price">—</strong>
				</div>
				<p class="lucrsure-public-summary-note lucrsure-muted"></p>
				<ul class="lucrsure-public-summary-features"></ul>
			</aside>
		</div>
	</div>

	<div class="lucrsure-public-success" hidden>
		<div class="lucrsure-public-success-card">
			<h2><?php esc_html_e( 'You\'re all set!', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
			<p class="lucrsure-public-success-message"></p>
		</div>
	</div>
</div>
