<?php
// Show notice only on Themes page
add_action( 'admin_init', function () {
    global $pagenow;

    if ( is_admin() && $pagenow === 'themes.php' ) {
        add_action( 'admin_notices', 'classified_listings_activation_notice' );
    }
});

// Admin Notice Function
function classified_listings_activation_notice() {
 $classified_listings_meta = get_option( 'classified_listings_activation_notice' );

       if( !$classified_listings_meta ){
       if( is_network_admin() ){
            return;
        } if( ! current_user_can( 'manage_options' ) ){
            return;
        }if ( isset($_GET['page']) && $_GET['page'] === 'classified-listings-info' ) {
            return;
        } ?>
    <div id="classified-listings-welcome-notice" class="notice notice-success is-dismissible welcome-notice">
        <div class="notice-row">
            <div class="notice-text">

                <p class="welcome-text1">
                    <?php esc_html_e( '🎉 Welcome to VW Themes,', 'classified-listings' ); ?>
                </p>

                <p class="welcome-text2">
                    <?php esc_html_e(
                        'You are now using the Classified Listings, a beautifully designed theme to kickstart your website.',
                        'classified-listings'
                    ); ?>
                </p>

                <p class="welcome-text3">
                    <?php esc_html_e(
                        'To help you get started quickly, use the options below:',
                        'classified-listings'
                    ); ?>
                </p>

                <span class="import-btn">
                    <a href="javascript:void(0);" id="install-activate-button" class="button admin-button info-button">
                        <?php esc_html_e( 'GET STARTED', 'classified-listings' ); ?>
                    </a>
                </span>

                <span class="demo-btn">
                    <a href="https://www.vwthemes.net/classified-listings/','classified-listings"
                       class="button button-primary" target="_blank">
                        <?php esc_html_e( 'VIEW DEMO', 'classified-listings' ); ?>
                    </a>
                </span>

                <span class="upgrade-btn">
                    <a href="https://www.vwthemes.com/products/classified-wordpress-theme"
                       class="button button-primary" target="_blank">
                        <?php esc_html_e( 'UPGRADE TO PRO', 'classified-listings' ); ?>
                    </a>
                </span>

                <span class="bundle-btn">
                    <a href="https://www.vwthemes.com/products/wp-theme-bundle"
                       class="button button-primary" target="_blank">
                        <?php esc_html_e( 'BUNDLE OF 500+ THEMES', 'classified-listings' ); ?>
                    </a>
                </span>

            </div>

            <div class="notice-img1">
                <img src="<?php echo esc_url( get_template_directory_uri() . '/images/arrow-notice.png' ); ?>"
                     width="180"
                     alt="<?php esc_attr_e( 'Classified Listings', 'classified-listings' ); ?>" />
            </div>

            <div class="notice-img2">
                <img src="<?php echo esc_url( get_template_directory_uri() . '/images/bundle-notice.png' ); ?>"
                     width="180"
                     alt="<?php esc_attr_e( 'Classified Listings', 'classified-listings' ); ?>" />
            </div>
        </div>
    </div>

    <script type="text/javascript">
        jQuery(document).on('click', '#install-activate-button', function () {
            const button = jQuery(this);
            const redirectUrl = '<?php echo esc_url( admin_url( 'admin.php?page=classified-listings-info' ) ); ?>';

            jQuery.post(ajaxurl, { action: 'check_plugin_activation' }, function (response) {

                if (response.success && response.data.active) {
                    window.location.href = redirectUrl;
                } else {
                    button.text('Installing & Activating...');

                    jQuery.post(ajaxurl, {
                        action: 'install_and_activate_required_plugin',
                        nonce: '<?php echo wp_create_nonce( 'install_activate_nonce' ); ?>'
                    }, function (response) {

                        if (response.success) {
                            window.location.href = redirectUrl;
                        } else {
                            alert('Failed to activate the plugin.');
                            button.text('Try Again');
                        }
                    });
                }
            });
        });
    </script>
    <?php
}
}

//Add bundle image in customizer 
	add_action('customize_controls_print_footer_scripts', function () {
	?>
	<script>
		jQuery(document).ready(function($){
		var ac_repairing_services_banner = `
			<div class="vw-bundle-banner" style="padding:10px 12px;">
			<a href="https://www.vwthemes.com/products/wp-theme-bundle" target="_blank">
              <img src="<?php echo esc_url( get_template_directory_uri() . '/images/bundle-img.png' ); ?>"style="width:100%; border-radius:4px;">
            </a>
			</div>`;
		$('.customize-pane-parent').prepend(ac_repairing_services_banner);
		});
	</script>
	<?php
	});

add_action( 'admin_notices', 'classified_listings_activation_notice' );

if( ! function_exists( 'classified_listings_update_admin_notice' ) ) :
/**
 * Updating admin notice on dismiss
*/


// Admin Notice Update (PERMANENT DISMISS)
function classified_listings_update_admin_notice(){
    if ( isset( $_GET['classified_listings_activation_notice'] ) && $_GET['classified_listings_activation_notice'] == '1' ) {
        update_option( 'classified_listings_activation_notice', true );
    }
}
endif;

add_action( 'admin_init', 'classified_listings_update_admin_notice' );
