<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for layout mode detection only
$instant_popup_builder_is_edit_mode = isset($_GET['edit_id']) && !empty(sanitize_text_field(wp_unslash($_GET['edit_id'])));
$instant_popup_builder_scheduling_section_classes = 'ipb-section';
if (! $instant_popup_builder_is_edit_mode) {
    $instant_popup_builder_scheduling_section_classes .= ' ipb-new-popup-panel';
}
$instant_popup_builder_scheduling_active_wrapper_classes = 'ipb-scheduling-active-wrap';
if (! $instant_popup_builder_is_edit_mode) {
    $instant_popup_builder_scheduling_active_wrapper_classes .= ' ipb-new-popup-panel';
}
// Get current popup ID for loading existing settings
$instant_popup_builder_popup_id = 0;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for popup ID retrieval, value is sanitized
if (isset($_GET['edit_id'])) {
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for popup ID retrieval, value is sanitized
    $instant_popup_builder_popup_id = intval($_GET['edit_id']);
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for popup ID retrieval, value is sanitized
} elseif (isset($_GET['id'])) {
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for popup ID retrieval, value is sanitized
    $instant_popup_builder_popup_id = intval($_GET['id']);
}
// Check if scheduling extension is active
$instant_popup_builder_scheduling_extension_active = is_plugin_active('instant-popup-scheduling/instant-popup-scheduling.php');
?>
<div class="<?php echo esc_attr($instant_popup_builder_scheduling_section_classes); ?>">
 <div class="ipb-section-header">
    <h3>Scheduling</h3>
</div>
<div class="ipb-section-content" data-section="display">
<div class="design_background" id="scheduling_settings_container">
    <?php if ($instant_popup_builder_scheduling_extension_active): ?>
        <!-- Extension is active - it will replace this content via AJAX -->
        <div class="scheduling_extension_loading">
                           <p>Loading Scheduling Extension...</p>

            </div>

        </div>
        <script>

        jQuery(document).ready(function($) {

            // Check if scheduling extension is active

            <?php

            $instant_popup_builder_is_scheduling_extension_active = function_exists('is_plugin_active') &&

                                            is_plugin_active('instant-popup-scheduling/instant-popup-scheduling.php');

            ?>
            <?php if ($instant_popup_builder_is_scheduling_extension_active): ?>
            // Extension is active - wait for extension to replace content
            var checkCount = 0;
            var maxChecks = 20;
            function checkForExtensionContent() {
                if (checkCount >= maxChecks) {
                    // Fallback: try to load content manually
                    var popupId = extractPopupIdFromUrl();
                    if (popupId) {
                        loadSchedulingContentManually(popupId);
                    }else if(popupId == 0){
                       loadSchedulingContentManually(popupId);
                    }
                    return;
                }
                var extensionContent = $('#scheduling').find('.scheduling_content');
                if (extensionContent.length === 0) {
                    checkCount++;
                    setTimeout(checkForExtensionContent, 100);
                }
            }
            function loadSchedulingContentManually(popupId) {
                $.ajax({
                    url: ajaxurl,
                    method: 'POST',
                    data: {
                        action: 'get_scheduling_content',
                        popup_id: popupId,
                        nonce: '<?php echo esc_html( wp_create_nonce( 'scheduling_content_nonce' ) ); ?>'
                    },
                    success: function(response) {
                        if (response.success) {
                            var wrappedContent = '<div class="<?php echo esc_attr($instant_popup_builder_scheduling_active_wrapper_classes); ?>">' + response.data + '</div>';
                            $('#scheduling').html(wrappedContent);
                        }
                    }
                });
            }
            // Start checking for extension content
            setTimeout(checkForExtensionContent, 100);
            <?php else: ?>
            // Extension is not active - show upgrade notice
            // Content is already loaded via PHP
            <?php endif; ?>
            function extractPopupIdFromUrl() {
                var urlParams = new URLSearchParams(window.location.search);
                return urlParams.get('edit_id') || urlParams.get('id') || 0;
            }
        });
        </script>
    <?php else: ?>
        <!-- Extension is not active - show upsell -->
		<div class="ipb-upsell-card">
			<div class="ipb-upsell-card__title"><?php esc_html_e( 'Advanced Scheduling', 'instant-popup-builder' ); ?></div>
			<p class="ipb-upsell-card__desc">
				<?php esc_html_e( 'Take control of when your popups appear with advanced scheduling rules. Upgrade to unlock scheduling features.', 'instant-popup-builder' ); ?>
			</p>
			<div class="ipb-upsell-card__actions">
				<a class="button button-primary" target="_blank" href="https://instantpopupbuilder.com/extensions/scheduling/">
					<?php esc_html_e( 'Upgrade to Pro', 'instant-popup-builder' ); ?>
				</a>
			</div>
		</div>
    <?php endif; ?>
</div>
</div>
</div>