<?php
/**
 * Admin dashboard template.
 *
 * @package CraftAddonsForWPBakery
 *
 * @var array  $cafw_cards          Element cards keyed by slug.
 * @var array  $cafw_modules        Feature modules keyed by key.
 * @var bool   $cafw_wpbakery_ready Whether WPBakery is active.
 * @var bool   $cafw_updated        Whether settings were just saved.
 * @var string $cafw_upgrade_url    Freemius upgrade URL (empty if unavailable).
 */

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

/**
 * Render one element/feature card.
 *
 * @param string $slug    Card slug.
 * @param array  $card    Card data.
 * @param string $field   Form field name for the toggle.
 * @param bool   $feature Whether this is a feature card.
 * @return void
 */
function cafw_render_card( $slug, $card, $field, $feature = false ) {
	$locked   = ! empty( $card['locked'] );
	$enabled  = ! $locked && ! empty( $card['enabled'] );
	$icon_svg = 'admin/images/elements/' . $slug . '.svg';
	$has_svg  = is_readable( CAFW_PATH . $icon_svg );

	$classes = 'cafw-card';
	$classes .= $feature ? ' cafw-card--feature' : '';
	$classes .= $locked ? ' is-locked' : ( $enabled ? ' is-enabled' : ' is-off' );

	$tag  = $locked ? 'button' : 'div';
	$attr = $locked ? ' type="button" data-cafw-pro' : '';
	?>
	<<?php echo esc_html( $tag ) . $attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static tag + static attrs. ?> class="<?php echo esc_attr( $classes ); ?>">
		<div class="cafw-card__top">
			<span class="cafw-card__icon<?php echo $has_svg ? ' has-svg' : ''; ?>">
				<?php if ( $has_svg ) : ?>
					<img src="<?php echo esc_url( CAFW_URL . $icon_svg . '?v=' . filemtime( CAFW_PATH . $icon_svg ) ); ?>" alt="" width="48" height="48">
				<?php else : ?>
					<span class="dashicons <?php echo esc_attr( $card['icon'] ); ?>"></span>
				<?php endif; ?>
			</span>

			<?php if ( $locked ) : ?>
				<span class="cafw-card__pro">
					<svg viewBox="0 0 24 24" width="11" height="11" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="4" y="11" width="16" height="9" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></svg>
					<?php esc_html_e( 'Pro', 'craft-addons-for-wpbakery' ); ?>
				</span>
			<?php else : ?>
				<label class="cafw-switch" title="<?php esc_attr_e( 'Enable / disable', 'craft-addons-for-wpbakery' ); ?>">
					<input type="checkbox" name="<?php echo esc_attr( $field ); ?>[]" value="<?php echo esc_attr( $slug ); ?>" <?php checked( $enabled ); ?> />
					<span class="cafw-switch__slider"></span>
				</label>
			<?php endif; ?>
		</div>

		<h3 class="cafw-card__title"><?php echo esc_html( $card['label'] ); ?></h3>
		<p class="cafw-card__desc"><?php echo esc_html( $card['description'] ); ?></p>

		<?php if ( $locked ) : ?>
			<span class="cafw-card__unlock">
				<?php esc_html_e( 'Unlock with Pro', 'craft-addons-for-wpbakery' ); ?>
				<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
			</span>
		<?php endif; ?>
	</<?php echo esc_html( $tag ); ?>>
	<?php
}

/**
 * Split cards into free and locked Pro groups, preserving order.
 *
 * @param array $cards Cards keyed by slug.
 * @return array{0:array,1:array} Free cards and locked cards.
 */
function cafw_split_cards( $cards ) {
	$free   = array();
	$locked = array();
	foreach ( $cards as $slug => $card ) {
		if ( ! empty( $card['locked'] ) ) {
			$locked[ $slug ] = $card;
		} else {
			$free[ $slug ] = $card;
		}
	}
	return array( $free, $locked );
}

list( $cafw_free_elements, $cafw_pro_elements ) = cafw_split_cards( $cafw_cards );
list( $cafw_free_modules, $cafw_pro_modules )   = cafw_split_cards( $cafw_modules );
$cafw_can_upgrade = ( '' !== $cafw_upgrade_url ) && ( ! empty( $cafw_pro_elements ) || ! empty( $cafw_pro_modules ) );
?>
<div class="wrap cafw-wrap">

	<!-- Hero -->
	<header class="cafw-hero">
		<div class="cafw-hero__bg" aria-hidden="true">
			<div class="cafw-hero__blob cafw-hero__blob--violet"></div>
			<div class="cafw-hero__blob cafw-hero__blob--indigo"></div>
			<div class="cafw-hero__blob cafw-hero__blob--mid"></div>
			<div class="cafw-hero__grid"></div>
		</div>
		<div class="cafw-hero__inner">
			<div class="cafw-hero__content">
				<div class="cafw-hero__brand">
					<img class="cafw-hero__logo" src="<?php echo esc_url( CAFW_URL . 'admin/images/cafw-logo-white.svg' ); ?>" alt="" width="36" height="36" />
					<span class="cafw-hero__brand-name"><?php esc_html_e( 'Craft Addons for WPBakery', 'craft-addons-for-wpbakery' ); ?></span>
				</div>
				<p class="cafw-hero__subcopy"><?php esc_html_e( 'Lightweight, beautifully designed elements for the WPBakery Page Builder. Toggle what you need, hide what you don\'t.', 'craft-addons-for-wpbakery' ); ?></p>
				<div class="cafw-hero__trust">
					<span>
						<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="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( 'Free elements, zero configuration', 'craft-addons-for-wpbakery' ); ?>
					</span>
					<span class="cafw-hero__trust-sep" aria-hidden="true">&bull;</span>
					<span><?php esc_html_e( 'Disable unused elements to keep your site lean', 'craft-addons-for-wpbakery' ); ?></span>
				</div>
			</div>
			<?php if ( $cafw_can_upgrade ) : ?>
				<div class="cafw-hero__cta-wrap">
					<a class="cafw-hero__upgrade" href="<?php echo esc_url( $cafw_upgrade_url ); ?>" target="_blank" rel="noopener">
						<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" aria-hidden="true"><path d="M13 2L3 14h7l-1 8 10-12h-7l1-8z"/></svg>
						<?php esc_html_e( 'Upgrade to Pro', 'craft-addons-for-wpbakery' ); ?>
					</a>
					<span class="cafw-hero__upgrade-note"><?php esc_html_e( '14-day money-back guarantee', 'craft-addons-for-wpbakery' ); ?></span>
				</div>
			<?php endif; ?>
		</div>
	</header>

	<hr class="wp-header-end" />

	<?php if ( $cafw_updated ) : ?>
		<div class="cafw-toast" role="status">
			<span class="cafw-toast__icon" aria-hidden="true">
				<svg viewBox="0 0 20 20" width="13" height="13" fill="none" stroke="#fff" stroke-width="2.6" stroke-linecap="round" stroke-linejoin="round"><path d="M4 10.5l4 4 8-9"/></svg>
			</span>
			<span class="cafw-toast__msg"><?php esc_html_e( 'Your changes have been saved.', 'craft-addons-for-wpbakery' ); ?></span>
		</div>
	<?php endif; ?>

	<?php if ( ! $cafw_wpbakery_ready ) : ?>
		<div class="notice notice-warning cafw-notice">
			<p><?php esc_html_e( 'WPBakery Page Builder is not active. Activate it to use these elements on your pages.', 'craft-addons-for-wpbakery' ); ?></p>
		</div>
	<?php endif; ?>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<input type="hidden" name="action" value="cafw_save_elements" />
		<?php wp_nonce_field( CAFW_Admin::NONCE ); ?>

		<!-- Elements section -->
		<section class="cafw-section">
			<div class="cafw-section__head">
				<div class="cafw-section__eyebrow"><?php esc_html_e( 'Library', 'craft-addons-for-wpbakery' ); ?></div>
				<h2 class="cafw-section__title"><?php esc_html_e( 'Elements', 'craft-addons-for-wpbakery' ); ?></h2>
				<p class="cafw-section__hint"><?php esc_html_e( 'Toggle the elements you want available in the WPBakery editor.', 'craft-addons-for-wpbakery' ); ?></p>
			</div>

			<?php if ( empty( $cafw_cards ) ) : ?>
				<p><?php esc_html_e( 'No elements found.', 'craft-addons-for-wpbakery' ); ?></p>
			<?php else : ?>
				<div class="cafw-grid">
					<?php
					foreach ( $cafw_free_elements as $cafw_slug => $cafw_card ) {
						cafw_render_card( $cafw_slug, $cafw_card, 'cafw_elements' );
					}
					?>
				</div>

				<?php if ( ! empty( $cafw_pro_elements ) ) : ?>
					<div class="cafw-prohead">
						<span class="cafw-prohead__label">
							<svg viewBox="0 0 24 24" width="13" height="13" fill="currentColor" aria-hidden="true"><path d="M13 2L3 14h7l-1 8 10-12h-7l1-8z"/></svg>
							<?php esc_html_e( 'Pro elements', 'craft-addons-for-wpbakery' ); ?>
						</span>
						<span class="cafw-prohead__hint"><?php esc_html_e( 'Unlock these with Craft Addons Pro.', 'craft-addons-for-wpbakery' ); ?></span>
					</div>
					<div class="cafw-grid">
						<?php
						foreach ( $cafw_pro_elements as $cafw_slug => $cafw_card ) {
							cafw_render_card( $cafw_slug, $cafw_card, 'cafw_elements' );
						}
						?>
					</div>
				<?php endif; ?>
			<?php endif; ?>
		</section>

		<!-- Features section -->
		<?php if ( ! empty( $cafw_modules ) ) : ?>
			<section class="cafw-section">
				<div class="cafw-section__head">
					<div class="cafw-section__eyebrow"><?php esc_html_e( 'Modules', 'craft-addons-for-wpbakery' ); ?></div>
					<h2 class="cafw-section__title"><?php esc_html_e( 'Features', 'craft-addons-for-wpbakery' ); ?></h2>
					<p class="cafw-section__hint"><?php esc_html_e( 'Turn optional features on or off. A disabled feature loads nothing on your site.', 'craft-addons-for-wpbakery' ); ?></p>
				</div>

				<div class="cafw-grid">
					<?php
					foreach ( $cafw_free_modules as $cafw_mkey => $cafw_module ) {
						cafw_render_card( $cafw_mkey, $cafw_module, 'cafw_modules', true );
					}
					?>
				</div>

				<?php if ( ! empty( $cafw_pro_modules ) ) : ?>
					<div class="cafw-prohead">
						<span class="cafw-prohead__label">
							<svg viewBox="0 0 24 24" width="13" height="13" fill="currentColor" aria-hidden="true"><path d="M13 2L3 14h7l-1 8 10-12h-7l1-8z"/></svg>
							<?php esc_html_e( 'Pro features', 'craft-addons-for-wpbakery' ); ?>
						</span>
						<span class="cafw-prohead__hint"><?php esc_html_e( 'Unlock these with Craft Addons Pro.', 'craft-addons-for-wpbakery' ); ?></span>
					</div>
					<div class="cafw-grid">
						<?php
						foreach ( $cafw_pro_modules as $cafw_mkey => $cafw_module ) {
							cafw_render_card( $cafw_mkey, $cafw_module, 'cafw_modules', true );
						}
						?>
					</div>
				<?php endif; ?>
			</section>
		<?php endif; ?>

		<div class="cafw-savebar">
			<span class="cafw-savebar__hint"><?php esc_html_e( 'Changes apply to the editor once saved.', 'craft-addons-for-wpbakery' ); ?></span>
			<button type="submit" class="cafw-btn-save"><?php esc_html_e( 'Save Changes', 'craft-addons-for-wpbakery' ); ?></button>
		</div>
	</form>

	<?php
	$cafw_pro_features = array(
		__( 'Every premium element, ready to drop into your pages.', 'craft-addons-for-wpbakery' ),
		__( 'Advanced features like Scroll Animations and Image Scroller.', 'craft-addons-for-wpbakery' ),
		__( 'Priority updates and friendly support.', 'craft-addons-for-wpbakery' ),
	);
	?>
	<div class="cafw-modal" id="cafw-pro-modal" hidden>
		<div class="cafw-modal__backdrop" data-cafw-pro-close></div>
		<div class="cafw-modal__dialog" role="dialog" aria-modal="true" aria-labelledby="cafw-pro-title">
			<button type="button" class="cafw-modal__close" data-cafw-pro-close aria-label="<?php esc_attr_e( 'Close', 'craft-addons-for-wpbakery' ); ?>">
				<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round"><path d="M6 6l12 12M18 6L6 18"/></svg>
			</button>
			<span class="cafw-modal__badge" aria-hidden="true">
				<svg viewBox="0 0 24 24" width="28" height="28" fill="currentColor"><path d="M13 2L3 14h7l-1 8 10-12h-7l1-8z"/></svg>
			</span>
			<h2 class="cafw-modal__title" id="cafw-pro-title"><?php esc_html_e( 'Unlock the Pro elements', 'craft-addons-for-wpbakery' ); ?></h2>
			<p class="cafw-modal__lead"><?php esc_html_e( 'Upgrade to Craft Addons Pro to switch on every premium element and feature - and build pages faster.', 'craft-addons-for-wpbakery' ); ?></p>
			<ul class="cafw-modal__list">
				<?php foreach ( $cafw_pro_features as $cafw_feature ) : ?>
					<li>
						<svg class="cafw-modal__check" viewBox="0 0 20 20" width="17" height="17" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 10.5l4 4 8-9"/></svg>
						<span><?php echo esc_html( $cafw_feature ); ?></span>
					</li>
				<?php endforeach; ?>
			</ul>
			<a class="cafw-modal__cta" href="<?php echo esc_url( '' !== $cafw_upgrade_url ? $cafw_upgrade_url : '#' ); ?>"<?php echo ( '' !== $cafw_upgrade_url ) ? ' target="_blank" rel="noopener"' : ''; ?>>
				<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor" aria-hidden="true"><path d="M13 2L3 14h7l-1 8 10-12h-7l1-8z"/></svg>
				<?php esc_html_e( 'Upgrade to Pro', 'craft-addons-for-wpbakery' ); ?>
			</a>
			<a class="cafw-modal__learn" href="https://craftaddons.com/pricing/?utm_source=plugin&amp;utm_medium=dashboard&amp;utm_campaign=pro-modal" target="_blank" rel="noopener">
				<?php esc_html_e( 'Compare plans on craftaddons.com', 'craft-addons-for-wpbakery' ); ?>
			</a>
		</div>
	</div>

</div>
