<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $bic_fs;
$current_tab = 'schedule';

$bic_is_pro    = $bic_fs->is_plan( 'pro', true );
$bic_is_agency = $bic_fs->is_plan( 'agency', true );
$bic_can_schedule = $bic_is_pro || $bic_is_agency;

$show = [];
if ( $bic_fs->is_plan( 'personal', true ) ) {
	$show = [ 'monthly', 'weekly' ];
} else if ( $bic_is_pro ) {
	$show = [ 'monthly', 'weekly', 'daily' ];
} else if ( $bic_is_agency ) {
	$show = [ 'monthly', 'weekly', 'daily', 'hourly' ];
}
?>
<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_can_schedule ) : ?>
		<!-- Upgrade upsell for non-Pro 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( '⏰ Automated Schedule', 'blocked-in-china' ); ?></h2>
			<p><?php esc_html_e( 'Automatically check if your site is blocked in China on a recurring schedule. Get notified of changes without lifting a finger.', 'blocked-in-china' ); ?></p>

			<!-- Blurred preview -->
			<div style="margin:20px 0;">
				<table class="widefat" style="opacity: 0.3; pointer-events: none; max-width:400px; margin:0 auto;">
					<tbody>
						<tr>
							<td><input type="checkbox" checked disabled> <?php esc_html_e( 'Schedule Status Updates', 'blocked-in-china' ); ?></td>
							<td>
								<select disabled>
									<option><?php esc_html_e( 'Daily', 'blocked-in-china' ); ?></option>
								</select>
							</td>
						</tr>
					</tbody>
				</table>
			</div>

			<div style="margin-top:20px;">
				<p><strong><?php esc_html_e( 'Upgrade to Pro to unlock automated scheduling', 'blocked-in-china' ); ?></strong></p>
				<ul style="list-style:none; padding:0; margin:10px 0 20px;">
					<li>✅ <?php esc_html_e( 'Daily automated block checks', 'blocked-in-china' ); ?></li>
					<li>✅ <?php esc_html_e( 'Automatic email notifications on status changes', 'blocked-in-china' ); ?></li>
					<li>✅ <?php esc_html_e( 'Set it and forget it — runs in the background', 'blocked-in-china' ); ?></li>
					<li>✅ <?php esc_html_e( 'Full log history included', '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 Pro', 'blocked-in-china' ); ?>
				</a>
			</div>
		</div>
	<?php else : ?>
		<h2><?php _e( 'Schedule settings', 'blocked-in-china' ); ?></h2>
	
		<div id="gpbic_form_response"></div>

		<form id="gpbic_form" method="POST" novalidate="novalidate">
			<input type="hidden" id="form_action" name="action" value="gpbic_run_api" />
			<?php wp_nonce_field( 'gpbic_nonce' ); ?>

			<?php $is_cron_on = get_option('bic_cron_on');?>
			<table>
				<tr>
					<td>
						<label>
							<input id="bic_cron_on" type="checkbox" name="bic_cron_on" <?php echo $is_cron_on ? 'checked' : ''; ?>> <?php _e( 'Schedule Status Updates', 'blocked-in-china' )?>
						</label>
					</td>
					<td>
						<?php 
						$schedules = wp_get_schedules();
						
						$bic_schedules = array(
							'monthly' => $schedules['monthly'],
							'weekly' => $schedules['weekly'],
							'daily' => $schedules['daily'],
							'hourly' => $schedules['hourly'],
						);

						$intervals = array_column($bic_schedules, 'interval');
						array_multisort($intervals, SORT_DESC, $bic_schedules);
						$current_selection = !empty(get_option( 'bic_cron_schedule' )) ? get_option( 'bic_cron_schedule' ) : 'monthly';
						if( isset( $bic_schedules ) ){

							printf( '<select id="bic_cron_frequency" name="frequency">' );
							foreach ($bic_schedules as $key => $schedule) {

								printf(
									'<option value="%s" %s %s>%s</option>',
									$key,
									$current_selection == $key ? 'selected' : '',
									!in_array( $key, $show ) ? 'disabled' : '',
									esc_html( str_replace( "Once ", "", $schedule['display'] ) )
								);


							}
							printf( '</select>' );
						}?>
					</td>
				</tr>
			</table>
			
			<p class="submit">
				<button type="submit" name="submit" id="submit" class="button button-primary"><?php echo esc_html( __( 'Save Changes', 'blocked-in-china' ) ); ?></button>
				<span class="spinner"></span>
			</p>
		</form>
	<?php endif; ?>

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