<?php
/**
 * Bundle Tab Template
 *
 * China Plugins Bundle — cross-promote CPC + SIC + BIC.
 *
 * @package BlockedInChina
 */

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

global $bic_fs;

// Plugin detection — file paths as they appear in wp_get_active_and_recently_activated_plugins()
$bundle_plugins = array(
	'cpc' => array(
		'slug'   => 'china-payments-plugin',
		'file'   => 'china-payments-plugin/wpstripeglobal.php',
		'label'  => '💳 China Payments',
		'desc'   => 'Accept WeChat Pay, Alipay, and UnionPay payments from Chinese customers via Stripe integration.',
		'features' => array(
			'WeChat Pay & Alipay',
			'UnionPay via Stripe',
			'Multi-currency display (CNY)',
			'Checkout localization',
		),
	),
	'sic' => array(
		'slug'   => 'speed-in-china',
		'file'   => 'speed-in-china/speed-in-china.php',
		'label'  => '⚡ Speed in China',
		'desc'   => 'Test your website speed from China, detect blocked resources, localize Google Fonts, and get optimization recommendations.',
		'features' => array(
			'Speed tests from China locations',
			'GFW resource analysis',
			'Google Font localization',
			'Plugin slowdown detection',
		),
	),
);

// Determine plugin states
foreach ( $bundle_plugins as $key => &$plugin ) {
	$plugin['is_active']    = is_plugin_active( $plugin['file'] );
	$plugin['is_installed'] = file_exists( WP_PLUGIN_DIR . '/' . $plugin['file'] );
}
unset( $plugin );
$current_tab = 'bundle';
?>
<div class="wrap">

	<h1><?php esc_html_e( 'Blocked in China', 'blocked-in-china' ); ?></h1>

	<?php require_once GPBIC_PLUGIN_PATH . '/admin/templates/nav-tabs.php'; ?>

	<div class="bic-bundle-header" style="margin-top: 20px; text-align: center; padding: 30px 20px; background: linear-gradient(135deg, #1e3a5f 0%, #2563eb 100%); color: #fff; border-radius: 8px;">
		<h2 style="color: #fff; font-size: 24px; margin: 0 0 8px;"><?php esc_html_e( '⭐ China Plugins Bundle ⭐', 'blocked-in-china' ); ?></h2>
		<p style="font-size: 16px; opacity: 0.9; margin: 0 0 16px;"><?php esc_html_e( 'Everything you need to optimize your WordPress site for China', 'blocked-in-china' ); ?></p>
		<p style="font-size: 14px; background: rgba(255,255,255,0.15); display: inline-block; padding: 6px 16px; border-radius: 20px; margin: 0 0 16px;">
			<?php esc_html_e( '💰 Save 35% with the Bundle vs. individual plugins', 'blocked-in-china' ); ?>
		</p>
		<br>
		<a href="https://chinaplugins.com" target="_blank" class="button button-hero" style="background: #fff; color: #1e3a5f; border: none; font-weight: 600;">
			<?php esc_html_e( '🎯 Get the Bundle', 'blocked-in-china' ); ?>
		</a>
	</div>

	<div class="bic-bundle-grid" style="display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 20px; margin-top: 24px;">

		<!-- Blocked in China (Current Plugin — always active) -->
		<div class="bic-bundle-card" style="background: #fff; border: 2px solid #2563eb; border-radius: 8px; padding: 24px; position: relative;">
			<span style="position: absolute; top: -10px; right: 16px; background: #2563eb; color: #fff; padding: 2px 12px; border-radius: 12px; font-size: 11px; font-weight: 600;"><?php esc_html_e( 'CURRENT', 'blocked-in-china' ); ?></span>
			<h3 style="margin-top: 0;">🔒 <?php esc_html_e( 'Blocked in China', 'blocked-in-china' ); ?></h3>
			<p><?php esc_html_e( 'Check if your site is accessible in China and monitor Great Firewall blocking status with real-time alerts.', 'blocked-in-china' ); ?></p>
			<ul style="list-style: none; padding: 0; margin: 12px 0;">
				<li>✅ <?php esc_html_e( 'GFW blocking detection', 'blocked-in-china' ); ?></li>
				<li>✅ <?php esc_html_e( 'Real-time monitoring', 'blocked-in-china' ); ?></li>
				<li>✅ <?php esc_html_e( 'Email/Slack alerts', 'blocked-in-china' ); ?></li>
				<li>✅ <?php esc_html_e( 'DNS propagation check', 'blocked-in-china' ); ?></li>
			</ul>
			<span class="button button-primary" style="width: 100%; text-align: center; pointer-events: none; opacity: 0.7;"><?php esc_html_e( 'Active', 'blocked-in-china' ); ?></span>
		</div>

		<?php foreach ( $bundle_plugins as $key => $plugin ) : ?>
		<div class="bic-bundle-card" style="background: #fff; border: 1px solid <?php echo $plugin['is_active'] ? '#2563eb' : '#d1d5db'; ?>; border-radius: 8px; padding: 24px; position: relative;">
			<?php if ( $plugin['is_active'] ) : ?>
				<span style="position: absolute; top: -10px; right: 16px; background: #22c55e; color: #fff; padding: 2px 12px; border-radius: 12px; font-size: 11px; font-weight: 600;"><?php esc_html_e( 'INSTALLED', 'blocked-in-china' ); ?></span>
			<?php endif; ?>
			<h3 style="margin-top: 0;"><?php echo esc_html( $plugin['label'] ); ?></h3>
			<p><?php echo esc_html( $plugin['desc'] ); ?></p>
			<ul style="list-style: none; padding: 0; margin: 12px 0;">
				<?php foreach ( $plugin['features'] as $feature ) : ?>
					<li>✅ <?php echo esc_html( $feature ); ?></li>
				<?php endforeach; ?>
			</ul>

			<?php if ( $plugin['is_active'] ) : ?>
				<span class="button" style="width: 100%; text-align: center; pointer-events: none; opacity: 0.7;"><?php esc_html_e( 'Active', 'blocked-in-china' ); ?></span>
			<?php elseif ( $plugin['is_installed'] ) : ?>
				<button class="button button-primary bic-activate-plugin" style="width: 100%; text-align: center;"
					data-plugin-file="<?php echo esc_attr( $plugin['file'] ); ?>"
					data-nonce="<?php echo esc_attr( wp_create_nonce( 'bic_activate_plugin' ) ); ?>">
					<?php esc_html_e( 'Activate', 'blocked-in-china' ); ?>
				</button>
			<?php else : ?>
				<button class="button button-primary bic-install-plugin" style="width: 100%; text-align: center;"
					data-slug="<?php echo esc_attr( $plugin['slug'] ); ?>"
					data-plugin-file="<?php echo esc_attr( $plugin['file'] ); ?>"
					data-nonce="<?php echo esc_attr( wp_create_nonce( 'bic_install_plugin' ) ); ?>">
					<?php esc_html_e( 'Install Plugin', 'blocked-in-china' ); ?>
				</button>
			<?php endif; ?>
		</div>
		<?php endforeach; ?>

	</div>

	<!--Features-->
	<?php
		require_once GPBIC_PLUGIN_PATH . '/libs/features-api-integration-library/templates/features.php';
	?>
</div>

<script>
jQuery(document).ready(function($) {

	// AJAX Install Plugin from wordpress.org
	$('.bic-install-plugin').on('click', function(e) {
		e.preventDefault();
		var btn = $(this);
		var slug = btn.data('slug');
		var pluginFile = btn.data('plugin-file');
		var nonce = btn.data('nonce');

		btn.attr('disabled', true).text('Installing...');

		$.ajax({
			type: 'POST',
			url: ajaxurl,
			data: {
				action: 'bic_install_plugin',
				slug: slug,
				security: nonce
			},
			success: function(response) {
				if (response.success) {
					btn.removeClass('bic-install-plugin')
					   .addClass('bic-activate-plugin')
					   .attr('disabled', false)
					   .text('Activate')
					   .data('nonce', '<?php echo esc_js( wp_create_nonce( 'bic_activate_plugin' ) ); ?>');
				} else {
					btn.attr('disabled', false).text('Install Plugin');
					alert('Install failed: ' + (response.data || 'Unknown error'));
				}
			},
			error: function(xhr) {
				btn.attr('disabled', false).text('Install Plugin');
				alert('Install request failed (HTTP ' + xhr.status + ')');
			}
		});
	});

	// AJAX Activate Plugin
	$(document).on('click', '.bic-activate-plugin', function(e) {
		e.preventDefault();
		var btn = $(this);
		var pluginFile = btn.data('plugin-file');
		var nonce = btn.data('nonce');

		btn.attr('disabled', true).text('Activating...');

		$.ajax({
			type: 'POST',
			url: ajaxurl,
			data: {
				action: 'bic_activate_plugin',
				plugin_file: pluginFile,
				security: nonce
			},
			success: function(response) {
				if (response.success) {
					btn.replaceWith('<span class="button" style="width: 100%; text-align: center; pointer-events: none; opacity: 0.7;">Active</span>');
					btn.closest('.bic-bundle-card').css('border-color', '#2563eb');
				} else {
					btn.attr('disabled', false).text('Activate');
					alert('Activation failed: ' + (response.data || 'Unknown error'));
				}
			},
			error: function(xhr) {
				btn.attr('disabled', false).text('Activate');
				alert('Activation failed (HTTP ' + xhr.status + ')');
			}
		});
	});
});
</script>