<?php
/**
 * Pricing / upgrade page view.
 *
 * Variables from CAFW_Pricing::render_page():
 *   $cafw_pricing_data  array   Normalized pricing data (plan_id, currency, tiers).
 *   $cafw_upgrade_url   string  Freemius upgrade URL (fallback).
 *   $cafw_support_url   string  Support / contact URL.
 *
 * @package CraftAddonsForWPBakery
 */

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

$cafw_tiers    = isset( $cafw_pricing_data['tiers'] )    ? $cafw_pricing_data['tiers']    : array();
$cafw_currency = isset( $cafw_pricing_data['currency'] ) ? $cafw_pricing_data['currency'] : 'usd';

$cafw_currency_symbols = array( 'usd' => '$', 'eur' => '€', 'gbp' => '£' );
$cafw_symbol           = isset( $cafw_currency_symbols[ $cafw_currency ] ) ? $cafw_currency_symbols[ $cafw_currency ] : '$';

$cafw_has_lifetime = false;
foreach ( $cafw_tiers as $cafw_t ) {
	if ( ! empty( $cafw_t['lifetime'] ) ) {
		$cafw_has_lifetime = true;
		break;
	}
}

$cafw_plan_highlights = array(
	__( 'All premium elements (Image Scroller, Before/After, Query Grid, Popup Builder...)', 'craft-addons-for-wpbakery' ),
	__( 'Responsive Visibility on every element', 'craft-addons-for-wpbakery' ),
	__( 'Dynamic Content tokens', 'craft-addons-for-wpbakery' ),
	__( 'Scroll Animations', 'craft-addons-for-wpbakery' ),
	__( 'Cross-element modules - no per-element setup', 'craft-addons-for-wpbakery' ),
	__( 'Priority support', 'craft-addons-for-wpbakery' ),
);

$cafw_pro_benefits = array(
	__( 'All premium elements', 'craft-addons-for-wpbakery' ),
	__( 'Image Scroller element', 'craft-addons-for-wpbakery' ),
	__( 'Before/After element', 'craft-addons-for-wpbakery' ),
	__( 'Query Grid element', 'craft-addons-for-wpbakery' ),
	__( 'Popup Builder element', 'craft-addons-for-wpbakery' ),
	__( 'Responsive Visibility', 'craft-addons-for-wpbakery' ),
	__( 'Scroll Animations', 'craft-addons-for-wpbakery' ),
	__( 'Priority support', 'craft-addons-for-wpbakery' ),
);

$cafw_faqs = array(
	array(
		'q' => __( 'How does the license work?', 'craft-addons-for-wpbakery' ),
		'a' => __( 'Each plan covers a fixed number of sites (1, 3, or 10). Activate Pro on those sites and you get every premium element, all modules, and updates for the duration of your plan.', 'craft-addons-for-wpbakery' ),
	),
	array(
		'q' => __( "What is the difference between Annual and Lifetime?", 'craft-addons-for-wpbakery' ),
		'a' => __( 'Annual renews every year and includes updates and support for that year. Lifetime is a one-time payment with updates and support for as long as the product exists. Both unlock exactly the same features.', 'craft-addons-for-wpbakery' ),
	),
	array(
		'q' => __( 'Do I need WPBakery Page Builder?', 'craft-addons-for-wpbakery' ),
		'a' => __( 'Yes. Craft Addons is an extension for WPBakery Page Builder (js_composer). It adds new elements to the WPBakery editor - you need an active copy of WPBakery to use it.', 'craft-addons-for-wpbakery' ),
	),
	array(
		'q' => __( 'Is there a money-back guarantee?', 'craft-addons-for-wpbakery' ),
		'a' => __( '14-day no-questions-asked refund. Try Pro on a real site and if it does not fit your workflow, email us for a full refund.', 'craft-addons-for-wpbakery' ),
	),
	array(
		'q' => __( 'Will my checkout be secure?', 'craft-addons-for-wpbakery' ),
		'a' => __( 'Yes. Payments are processed by Freemius, a PCI-compliant merchant of record, right inside your WordPress dashboard. We never see your card details.', 'craft-addons-for-wpbakery' ),
	),
);

/**
 * Return tier display label from license count.
 *
 * @param int $licenses
 * @return string
 */
function cafw_tier_label( $licenses ) {
	switch ( $licenses ) {
		case 1:
			return __( '1 Site', 'craft-addons-for-wpbakery' );
		case 3:
			return __( '3 Sites', 'craft-addons-for-wpbakery' );
		case 10:
			return __( '10 Sites', 'craft-addons-for-wpbakery' );
		default:
			if ( 0 === (int) $licenses ) {
				return __( 'Unlimited', 'craft-addons-for-wpbakery' );
			}
			return sprintf(
				/* translators: %d: number of sites */
				_n( '%d Site', '%d Sites', $licenses, 'craft-addons-for-wpbakery' ),
				$licenses
			);
	}
}
?>
<div class="cafw-upgrade">
	<div class="cafw-upgrade__container">

		<!-- ═══ Hero ═══════════════════════════════════════════════════════ -->
		<header class="cafw-upgrade-hero">
			<div class="cafw-upgrade-hero__bg" aria-hidden="true">
				<div class="cafw-upgrade-hero__blob cafw-upgrade-hero__blob--teal"></div>
				<div class="cafw-upgrade-hero__blob cafw-upgrade-hero__blob--pink"></div>
				<div class="cafw-upgrade-hero__blob cafw-upgrade-hero__blob--orange"></div>
				<div class="cafw-upgrade-hero__grid"></div>
			</div>
			<div class="cafw-upgrade-hero__inner">
				<div class="cafw-upgrade-hero__brand">
					<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg" aria-label="Craft Addons" role="img">
						<rect width="32" height="32" rx="8" fill="white" fill-opacity="0.18"/>
						<path d="M16 6L20 13H25L21 18L23 25L16 21L9 25L11 18L7 13H12L16 6Z" fill="white"/>
					</svg>
					<span class="cafw-upgrade-hero__brand-name"><?php esc_html_e( 'Craft Addons for WPBakery', 'craft-addons-for-wpbakery' ); ?></span>
				</div>
				<div class="cafw-upgrade-hero__eyebrow"><?php esc_html_e( 'Upgrade to Pro', 'craft-addons-for-wpbakery' ); ?></div>
				<h1 class="cafw-upgrade-hero__title"><?php esc_html_e( 'More elements. Smarter builds.', 'craft-addons-for-wpbakery' ); ?></h1>
				<p class="cafw-upgrade-hero__subcopy">
					<?php esc_html_e( 'Unlock all premium WPBakery elements, Responsive Visibility, Scroll Animations, and more. Buy securely without leaving your dashboard.', 'craft-addons-for-wpbakery' ); ?>
				</p>
				<div class="cafw-upgrade-hero__trust">
					<span>
						<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/><path d="M9 12l2 2 4-4"/></svg>
						<?php esc_html_e( '14-day money-back guarantee', 'craft-addons-for-wpbakery' ); ?>
					</span>
					<span class="cafw-upgrade-hero__trust-sep" aria-hidden="true">&bull;</span>
					<span><?php esc_html_e( 'Secure Freemius checkout', 'craft-addons-for-wpbakery' ); ?></span>
				</div>
			</div>
		</header>

		<!-- ═══ Pricing ═════════════════════════════════════════════════════ -->
		<section class="cafw-upgrade-section cafw-upgrade-pricing">
			<div class="cafw-upgrade-section__head cafw-upgrade-pricing__head">
				<div class="cafw-upgrade-section__heading-text">
					<div class="cafw-upgrade-section__eyebrow"><?php esc_html_e( 'Pricing', 'craft-addons-for-wpbakery' ); ?></div>
					<h2 class="cafw-upgrade-section__title"><?php esc_html_e( 'Choose the plan that fits your sites', 'craft-addons-for-wpbakery' ); ?></h2>
				</div>

				<?php if ( ! empty( $cafw_tiers ) ) : ?>
				<div class="cafw-upgrade-toggle" role="tablist" aria-label="<?php esc_attr_e( 'Billing period', 'craft-addons-for-wpbakery' ); ?>">
					<button type="button" role="tab" aria-selected="true"
						class="cafw-upgrade-toggle__btn is-active"
						data-billing="annual">
						<?php esc_html_e( 'Annual', 'craft-addons-for-wpbakery' ); ?>
					</button>
					<?php if ( $cafw_has_lifetime ) : ?>
					<button type="button" role="tab" aria-selected="false"
						class="cafw-upgrade-toggle__btn"
						data-billing="lifetime">
						<?php esc_html_e( 'Lifetime', 'craft-addons-for-wpbakery' ); ?>
						<span class="cafw-upgrade-toggle__badge"><?php esc_html_e( 'Best value', 'craft-addons-for-wpbakery' ); ?></span>
					</button>
					<?php endif; ?>
				</div>
				<?php endif; ?>
			</div>

			<?php if ( empty( $cafw_tiers ) ) : ?>
			<div class="cafw-upgrade-error">
				<p><?php esc_html_e( 'Pricing is unavailable right now.', 'craft-addons-for-wpbakery' ); ?></p>
				<?php if ( ! empty( $cafw_upgrade_url ) ) : ?>
				<a href="<?php echo esc_url( $cafw_upgrade_url ); ?>" class="cafw-upgrade-btn cafw-upgrade-btn--secondary">
					<?php esc_html_e( 'Open account &amp; pricing', 'craft-addons-for-wpbakery' ); ?>
				</a>
				<?php endif; ?>
			</div>
			<?php else : ?>

			<div class="cafw-upgrade-grid cafw-upgrade-grid--<?php echo count( $cafw_tiers ) === 1 ? 'single' : 'multi'; ?>">
				<?php
				foreach ( $cafw_tiers as $cafw_idx => $cafw_tier ) :
					$cafw_featured   = ( count( $cafw_tiers ) > 1 && 1 === $cafw_idx );
					$cafw_annual_raw = isset( $cafw_tier['annual'] )   ? (float) $cafw_tier['annual']   : null;
					$cafw_life_raw   = isset( $cafw_tier['lifetime'] ) ? (float) $cafw_tier['lifetime'] : null;
					$cafw_label      = cafw_tier_label( (int) $cafw_tier['licenses'] );

					if ( null === $cafw_annual_raw && null === $cafw_life_raw ) {
						continue;
					}

					$cafw_show_annual = ( null !== $cafw_annual_raw );
					$cafw_amount      = $cafw_show_annual
						? $cafw_symbol . number_format( $cafw_annual_raw / 12, 2 )
						: $cafw_symbol . number_format( (int) $cafw_life_raw );
					$cafw_period      = $cafw_show_annual ? __( '/ mo', 'craft-addons-for-wpbakery' ) : __( 'one-time', 'craft-addons-for-wpbakery' );
					$cafw_sub         = $cafw_show_annual
						? $cafw_symbol . number_format( (int) $cafw_annual_raw ) . ' ' . __( 'billed yearly', 'craft-addons-for-wpbakery' )
						: __( 'Lifetime updates & support', 'craft-addons-for-wpbakery' );

					$cafw_life_amount = null !== $cafw_life_raw ? $cafw_symbol . number_format( (int) $cafw_life_raw ) : '';
					$cafw_ann_amount  = null !== $cafw_annual_raw ? $cafw_symbol . number_format( $cafw_annual_raw / 12, 2 ) : '';
				?>
				<div class="cafw-plan-card<?php echo $cafw_featured ? ' cafw-plan-card--featured' : ''; ?>"
					data-annual-amount="<?php echo esc_attr( $cafw_ann_amount ); ?>"
					data-annual-period="<?php echo esc_attr( __( '/ mo', 'craft-addons-for-wpbakery' ) ); ?>"
					data-annual-sub="<?php echo null !== $cafw_annual_raw ? esc_attr( $cafw_symbol . number_format( (int) $cafw_annual_raw ) . ' ' . __( 'billed yearly', 'craft-addons-for-wpbakery' ) ) : ''; ?>"
					data-lifetime-amount="<?php echo esc_attr( $cafw_life_amount ); ?>"
					data-lifetime-period="<?php echo esc_attr( __( 'one-time', 'craft-addons-for-wpbakery' ) ); ?>"
					data-lifetime-sub="<?php echo esc_attr( __( 'Lifetime updates & support', 'craft-addons-for-wpbakery' ) ); ?>">

					<?php if ( $cafw_featured ) : ?>
					<div class="cafw-plan-card__ribbon"><?php esc_html_e( 'Most popular', 'craft-addons-for-wpbakery' ); ?></div>
					<?php endif; ?>

					<div class="cafw-plan-card__name"><?php echo esc_html( $cafw_label ); ?></div>
					<div class="cafw-plan-card__price">
						<span class="cafw-plan-card__amount js-cafw-amount"><?php echo esc_html( $cafw_amount ); ?></span>
						<span class="cafw-plan-card__period js-cafw-period"><?php echo esc_html( $cafw_period ); ?></span>
					</div>
					<div class="cafw-plan-card__sub js-cafw-sub"><?php echo esc_html( $cafw_sub ); ?></div>

					<button type="button"
						class="cafw-plan-card__cta js-cafw-buy<?php echo $cafw_featured ? ' cafw-plan-card__cta--primary' : ''; ?>"
						data-pricing-id="<?php echo esc_attr( (string) $cafw_tier['pricing_id'] ); ?>"
						data-licenses="<?php echo esc_attr( (string) $cafw_tier['licenses'] ); ?>"
						data-billing="annual">
						<?php esc_html_e( 'Upgrade now', 'craft-addons-for-wpbakery' ); ?>
					</button>

					<ul class="cafw-plan-card__features">
						<?php foreach ( $cafw_plan_highlights as $cafw_feat ) : ?>
						<li>
							<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
								<path d="M3.5 8.5l3 3 6-7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
							</svg>
							<span><?php echo esc_html( $cafw_feat ); ?></span>
						</li>
						<?php endforeach; ?>
					</ul>

				</div>
				<?php endforeach; ?>
			</div>

			<div class="cafw-upgrade-pricing__note">
				<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/><path d="M9 12l2 2 4-4"/></svg>
				<span><?php esc_html_e( 'Secure checkout by Freemius (PCI compliant) &middot; 14-day money-back guarantee', 'craft-addons-for-wpbakery' ); ?></span>
			</div>

			<?php endif; ?>
		</section>

		<!-- ═══ Pro benefits + CTA ══════════════════════════════════════════ -->
		<section class="cafw-upgrade-section">
			<div class="cafw-upgrade-section__head">
				<div class="cafw-upgrade-section__heading-text">
					<div class="cafw-upgrade-section__eyebrow"><?php esc_html_e( 'Upgrade to Pro', 'craft-addons-for-wpbakery' ); ?></div>
					<h2 class="cafw-upgrade-section__title"><?php esc_html_e( 'Everything in Free, plus the full element library', 'craft-addons-for-wpbakery' ); ?></h2>
				</div>
			</div>

			<div class="cafw-upgrade-benefits">
				<div class="cafw-upgrade-benefits__body">
					<div class="cafw-upgrade-benefits__intro"><?php esc_html_e( 'Everything Pro unlocks, at a glance', 'craft-addons-for-wpbakery' ); ?></div>
					<ul class="cafw-upgrade-benefits__list">
						<?php foreach ( $cafw_pro_benefits as $cafw_b ) : ?>
						<li>
							<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
								<path d="M3.5 8.5l3 3 6-7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
							</svg>
							<span><?php echo esc_html( $cafw_b ); ?></span>
						</li>
						<?php endforeach; ?>
					</ul>
					<div class="cafw-upgrade-benefits__footer">
						<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
							<circle cx="12" cy="12" r="10"/>
							<path d="M9 12l2 2 4-4"/>
						</svg>
						<span><?php esc_html_e( 'Trusted by builders worldwide.', 'craft-addons-for-wpbakery' ); ?></span>
					</div>
				</div>
				<div class="cafw-upgrade-benefits__cta">
					<svg class="cafw-upgrade-benefits__cta-art" width="56" height="56" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
						<path d="M5 3l1.5 4 4 1.5L6.5 10 5 14l-1.5-4-4-1.5L3.5 7 5 3z" transform="translate(2 2)"/>
						<path d="M18 13l1 2.5 2.5 1L19 17.5l-1 2.5-1-2.5-2.5-1L17 15.5 18 13z"/>
					</svg>
					<div class="cafw-upgrade-benefits__cta-headline"><?php esc_html_e( 'See the plan that fits your sites', 'craft-addons-for-wpbakery' ); ?></div>
					<div class="cafw-upgrade-benefits__cta-sub"><?php esc_html_e( 'Single-site, freelancer, and lifetime options available.', 'craft-addons-for-wpbakery' ); ?></div>
					<?php if ( ! empty( $cafw_tiers ) ) : ?>
					<button type="button" class="cafw-upgrade-btn cafw-upgrade-btn--light js-cafw-scroll-pricing">
						<?php esc_html_e( 'See pricing above', 'craft-addons-for-wpbakery' ); ?>
						<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
					</button>
					<?php elseif ( ! empty( $cafw_upgrade_url ) ) : ?>
					<a class="cafw-upgrade-btn cafw-upgrade-btn--light" href="<?php echo esc_url( $cafw_upgrade_url ); ?>">
						<?php esc_html_e( 'Upgrade to Pro', 'craft-addons-for-wpbakery' ); ?>
						<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
					</a>
					<?php endif; ?>
					<div class="cafw-upgrade-benefits__cta-note"><?php esc_html_e( '14-day money-back guarantee', 'craft-addons-for-wpbakery' ); ?></div>
				</div>
			</div>
		</section>

		<!-- ═══ FAQ ══════════════════════════════════════════════════════════ -->
		<section class="cafw-upgrade-section">
			<div class="cafw-upgrade-section__head">
				<div class="cafw-upgrade-section__heading-text">
					<div class="cafw-upgrade-section__eyebrow"><?php esc_html_e( 'FAQ', 'craft-addons-for-wpbakery' ); ?></div>
					<h2 class="cafw-upgrade-section__title"><?php esc_html_e( 'Questions before you upgrade', 'craft-addons-for-wpbakery' ); ?></h2>
				</div>
			</div>

			<div class="cafw-faq js-cafw-faq">
				<?php foreach ( $cafw_faqs as $cafw_fi => $cafw_faq ) : ?>
				<div class="cafw-faq__item<?php echo 0 === $cafw_fi ? ' is-open' : ''; ?>">
					<button type="button"
						class="cafw-faq__question"
						aria-expanded="<?php echo 0 === $cafw_fi ? 'true' : 'false'; ?>">
						<span class="cafw-faq__num" aria-hidden="true"><?php echo esc_html( str_pad( $cafw_fi + 1, 2, '0', STR_PAD_LEFT ) ); ?></span>
						<span class="cafw-faq__text"><?php echo esc_html( $cafw_faq['q'] ); ?></span>
						<span class="cafw-faq__toggle" aria-hidden="true">
							<svg class="cafw-faq__chevron" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
								<path d="M6 9l6 6 6-6"/>
							</svg>
						</span>
					</button>
					<div class="cafw-faq__answer"<?php echo 0 !== $cafw_fi ? ' hidden' : ''; ?>>
						<?php echo esc_html( $cafw_faq['a'] ); ?>
					</div>
				</div>
				<?php endforeach; ?>
			</div>

			<div class="cafw-faq__footer">
				<span><?php esc_html_e( 'Still have a question?', 'craft-addons-for-wpbakery' ); ?></span>
				<?php if ( ! empty( $cafw_support_url ) ) : ?>
				<a class="cafw-faq__footer-link" href="<?php echo esc_url( $cafw_support_url ); ?>">
					<?php esc_html_e( 'Ask our team', 'craft-addons-for-wpbakery' ); ?>
					<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
				</a>
				<?php endif; ?>
			</div>
		</section>

	</div><!-- /.cafw-upgrade__container -->
</div><!-- /.cafw-upgrade -->
