<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $bic_fs;
$current_tab = 'log';
$bic_is_paid = $bic_fs->is_plan( 'personal', true ) || $bic_fs->is_plan( 'pro', true ) || $bic_fs->is_plan( 'agency', true );
?>
<div class="wrap">
	<h1><?php _e( 'Blocked in China', 'blocked-in-china' ); ?></h1>

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

	<?php if ( ! $bic_is_paid ) : ?>
		<!-- Upgrade upsell for free users -->
		<div class="sic_container" style="text-align:center; max-width:600px; margin:30px auto; padding:30px;">
			<div style="font-size:48px; margin-bottom:10px;">🔒</div>
			<h2><?php esc_html_e( '📋 Block Check Log History', 'blocked-in-china' ); ?></h2>
			<p><?php esc_html_e( 'Keep a history of your block checks and easily review which URLs were blocked, accessible, or timed out from different locations in China.', 'blocked-in-china' ); ?></p>

			<!-- Blurred preview -->
			<div style="margin:20px 0;">
				<table class="wp-list-table widefat fixed striped" style="opacity: 0.3; pointer-events: none;">
					<thead>
						<tr>
							<th width="30%"><?php esc_html_e( 'Location', 'blocked-in-china' ); ?></th>
							<th width="50%"><?php esc_html_e( 'Lookup Result', 'blocked-in-china' ); ?></th>
							<th width="20%"><?php esc_html_e( 'Status', 'blocked-in-china' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<tr><td>Beijing, CN</td><td>203.0.113.50</td><td><span style="color:#22c55e;">✓ Accessible</span></td></tr>
						<tr><td>Shanghai, CN</td><td>—</td><td><span style="color:#ef4444;">✗ Blocked</span></td></tr>
						<tr><td>Shenzhen, CN</td><td>203.0.113.50</td><td><span style="color:#22c55e;">✓ Accessible</span></td></tr>
					</tbody>
				</table>
			</div>

			<div style="margin-top:20px;">
				<p><strong><?php esc_html_e( 'Upgrade to Personal to unlock log history', 'blocked-in-china' ); ?></strong></p>
				<ul style="list-style:none; padding:0; margin:10px 0 20px;">
					<li>✅ <?php esc_html_e( 'View past block check results', 'blocked-in-china' ); ?></li>
					<li>✅ <?php esc_html_e( 'Compare results across dates', 'blocked-in-china' ); ?></li>
					<li>✅ <?php esc_html_e( 'Track your site\'s accessibility over time', 'blocked-in-china' ); ?></li>
					<li>✅ <?php esc_html_e( 'Weekly automated checks (vs monthly on free)', 'blocked-in-china' ); ?></li>
				</ul>
				<a href="<?php echo esc_url( $bic_fs->get_upgrade_url() ); ?>" class="button button-primary button-hero">
					<?php esc_html_e( '⬆️ Upgrade to Personal', 'blocked-in-china' ); ?>
				</a>
			</div>
		</div>
	<?php else : ?>
		<h2><?php _e( 'Log History', 'blocked-in-china' ); ?></h2>

		<p><?php _e( 'You can see all your API call log history.', 'blocked-in-china' ); ?></p>
		<div id="gpbic_form_response"></div>
		<?php 
			global $wpdb;
			$query = $wpdb->prepare(
				"SELECT * FROM $wpdb->options WHERE `option_name` LIKE %s",
				'bic_cron_log-%'
			);
			$logs = $wpdb->get_results($query);
			
			if( isset($logs) ): 
				printf(
					'<label>%s: </label>',
					__( 'Filter log', 'blocked-in-china' )
				);
				printf( '<select id="bic-filter-logs">' );
					printf(
						'<option value="0">--%s--</option>',
						__( 'Select', 'blocked-in-china' )
					);
					foreach ($logs as $key => $log) {

						$label = str_replace("bic_cron_", "", $log->option_name) . " UTC";
						echo sprintf(
							'<option value="%1$s">%2$s</option>',
							esc_attr($log->option_id),
							esc_attr($label)
						);
					} 
				printf( '</select>' );
			endif; 
		?>
		<hr/>
		<table id="gpbic_form_result" class="wp-list-table widefat fixed striped">
			<thead>
				<tr>
					<th width="30%"><?php _e( 'Location', 'blocked-in-china' ); ?></th>
					<th width="50%"><?php _e( 'Lookup Result', 'blocked-in-china' ); ?></th>
					<th width="20%"><?php _e( 'Status', 'blocked-in-china' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td colspan="3"><?php _e( 'Select option to see the results.', 'blocked-in-china' ); ?></td>
				</tr>
			</tbody>
		</table>
	<?php endif; ?>

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