<?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_location_section_classes = 'ipb-section';
if (! $instant_popup_builder_is_edit_mode) {
    $instant_popup_builder_location_section_classes .= ' ipb-new-popup-panel';
}
$instant_popup_builder_location_active_wrapper_classes = 'ipb-location-active-wrap';
if (! $instant_popup_builder_is_edit_mode) {
    $instant_popup_builder_location_active_wrapper_classes .= ' ipb-new-popup-panel';
}

// Check if Geo Targeting Extension is active

$instant_popup_builder_is_geo_targeting_active = class_exists('Instant_Popup_Geo_Targeting') ||
                          is_plugin_active('instant-popup-geo-targeting/instant-popup-geo-targeting.php') ||
                          defined('INSTANT_POPUP_GEO_TARGETING_VERSION');

if ($instant_popup_builder_is_geo_targeting_active) {
    echo '<div class="' . esc_attr($instant_popup_builder_location_active_wrapper_classes) . '">';
    // If extension is active, let it render the location interface
    if (has_action('instant_popup_builder_tab_content_condition')) {
        do_action('instant_popup_builder_tab_content_condition', 'location');
        echo '</div>';
        return;
    }
    ?>
    <div class="<?php echo esc_attr($instant_popup_builder_location_section_classes); ?>">
 <div class="ipb-section-header">
    <h3>GEO Location</h3>
</div>
<div class="ipb-section-content" data-section="display">
    <div class="design_background">
        <div class="display_wrapper">
            <div class="display_inner">
                <div class="upgrade-notice geo-targeting-active">
                    <div class="upgrade-content">
                        <div class="upgrade-icon">
                            <i class="fa-solid fa-check-circle" style="color: #00a32a; font-size: 48px;"></i>
                        </div>
                        <h3>Geo Targeting Extension Active</h3>
                        <p class="description">The Geo Targeting Extension is now handling location-based popup targeting. All geo-targeting features are available through the extension interface.</p>
                        <div class="upgrade-features">
                            <ul>
                                <li><i class="fa-solid fa-check"></i> Country-Based Targeting</li>
                                <li><i class="fa-solid fa-check"></i> Region or City Targeting</li>
                                <li><i class="fa-solid fa-check"></i> Inclusion and Exclusion Rules</li>
                                <li><i class="fa-solid fa-check"></i> IP Address Targeting</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </div>
    </div>
    <?php echo '</div>'; ?>
    <?php
} else {
    // If extension is not active, show upgrade notice
    ?>
    <div class="<?php echo esc_attr($instant_popup_builder_location_section_classes); ?>">
 <div class="ipb-section-header">
    <h3><?php esc_html_e( 'GEO Location', 'instant-popup-builder' ); ?></h3>
</div>
<div class="ipb-section-content" data-section="display">
	<div class="ipb-upsell-card">
		<div class="ipb-upsell-card__title"><?php esc_html_e( 'GEO Location', 'instant-popup-builder' ); ?></div>
		<p class="ipb-upsell-card__desc">
			<?php esc_html_e( 'Target popups by country/region/city using geo rules. Upgrade to unlock geo-based visibility (IP-based geo targeting).', 'instant-popup-builder' ); ?>
		</p>
		<div class="ipb-upsell-card__actions">
			<a class="button button-primary" target="_blank" href="https://instantpopupbuilder.com/extensions/geo-targeting-popup-for-wordpress/">
				<?php esc_html_e( 'Upgrade to Pro', 'instant-popup-builder' ); ?>
			</a>
		</div>
	</div>
</div>
</div>
    <?php
}
