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

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

// Admin Notice Function
function vw_shopping_mall_activation_notice() {
  $vw_shopping_mall_meta = get_option( 'vw_shopping_mall_activation_notice' );

       if( !$vw_shopping_mall_meta ){
       if( is_network_admin() ){
            return;
        } if( ! current_user_can( 'manage_options' ) ){
            return;
        }if ( isset($_GET['page']) && $_GET['page'] === 'vw-shopping-mall-info' ) {
            return;
        } ?>
    <div id="vw-shopping-mall-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,', 'vw-shopping-mall' ); ?>
                </p>

                <p class="welcome-text2">
                    <?php esc_html_e(
                        'You are now using the VW Shopping Mall, a beautifully designed theme to kickstart your website.',
                        'vw-shopping-mall'
                    ); ?>
                </p>

                <p class="welcome-text3">
                    <?php esc_html_e(
                        'To help you get started quickly, use the options below:',
                        'vw-shopping-mall'
                    ); ?>
                </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', 'vw-shopping-mall' ); ?>
                    </a>
                </span>

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

                <span class="upgrade-btn">
                    <a href="https://www.vwthemes.com/products/shopping-mall-wordpress-theme"
                       class="button button-primary" target="_blank">
                        <?php esc_html_e( 'UPGRADE TO PRO', 'vw-shopping-mall' ); ?>
                    </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 485+ THEMES', 'vw-shopping-mall' ); ?>
                    </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( 'VW Shopping Mall', 'vw-shopping-mall' ); ?>" />
            </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( 'VW Shopping Mall', 'vw-shopping-mall' ); ?>" />
            </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=vw-shopping-mall-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_action( 'admin_notices', 'vw_shopping_mall_activation_notice' );

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


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

add_action( 'admin_init', 'vw_shopping_mall_update_admin_notice' );

//After Switch theme function
add_action('after_switch_theme', 'vw_shopping_mall_getstart_setup_options');
function vw_shopping_mall_getstart_setup_options () {
    update_option('vw_shopping_mall_activation_notice', FALSE );
 }