<?php
// Add Getstart admin notice
function vw_furniture_shop_admin_notice() { 
   $vw_furniture_shop_meta = get_option( 'vw_furniture_shop_admin_notice' );

       if( !$vw_furniture_shop_meta ){
       if( is_network_admin() ){
            return;
        } if( ! current_user_can( 'manage_options' ) ){
            return;
        }if ( isset($_GET['page']) && $_GET['page'] === 'vw-furniture-shop-info' ) {
            return;
        } ?>
        <div class="notice notice-success is-dismissible welcome-notice">
            <div class="notice-row">
                <div class="notice-text">
                    <p class="welcome-text1"><?php esc_html_e( 'Thanks For Installing the "VW Furniture Shop Theme"!', 'vw-furniture-shop' ); ?></p>
                    
                    <p class="welcome-text3"><?php esc_html_e( 'We’re excited to help you get started with your new theme! Set up your website quickly and easily by importing our demo content and customizing it to suit your needs.', 'vw-furniture-shop' ); ?></p>

                    <span class="import-btn">
                        <a href="javascript:void(0);" id="install-activate-button" class="button admin-button info-button">
                           <?php echo __('Get Started', 'vw-furniture-shop'); ?>
                        </a>
                        <script type="text/javascript">
                            document.getElementById('install-activate-button').addEventListener('click', function () {
                                const vw_furniture_shop_button = this;
                                const vw_furniture_shop_redirectUrl = '<?php echo esc_url(admin_url("themes.php?page=vw-furniture-shop-info")); ?>';
                                // First, check if plugin is already active
                                jQuery.post(ajaxurl, { action: 'check_plugin_activation' }, function (response) {
                                    if (response.success && response.data.active) {
                                        // Plugin already active — just redirect
                                        window.location.href = vw_furniture_shop_redirectUrl;
                                    } else {
                                        // Show Installing & Activating only if not already active
                                        vw_furniture_shop_button.textContent = '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) {

                                            vw_furniture_shop_button.textContent = 'Importing Demo...';

                                            jQuery.post(ajaxurl, {
                                                action: 'vw_furniture_shop_run_demo_import',
                                                nonce: '<?php echo wp_create_nonce("vw_furniture_shop_demo_nonce"); ?>'
                                            }, function (importResponse) {

                                                if (importResponse.success) {
                                                    window.location.href = vw_furniture_shop_redirectUrl;
                                                } else {
                                                    vw_furniture_shop_button.textContent = 'Try Again';
                                                }

                                            });
                                        }
                                        else {
                                                alert('Failed to activate the plugin.');
                                                vw_furniture_shop_button.textContent = 'Try Again';
                                            }
                                        });
                                    }
                                });
                            });
                        </script>
                    </span>

                    <span class="demo-btn">
                        <a href="https://preview.themescaliber.com/doc/free-furniture/" class="button button-primary" target="_blank">
                            <?php esc_html_e( 'Documentation', 'vw-furniture-shop' ); ?>
                        </a>
                    </span>

                    <span class="upgrade-btn">
                        <a href="https://www.themescaliber.com/products/furniture-wordpress-theme" class="button button-primary" target="_blank">
                            <?php esc_html_e( 'Get Premium', 'vw-furniture-shop' ); ?>
                        </a>
                    </span>

                    <span class="bundle-btn">
                        <a href="https://www.themescaliber.com/products/wordpress-theme-bundle" class="button button-primary" target="_blank">
                            <?php esc_html_e( 'Bundle Of 220+ Themes at $99', 'vw-furniture-shop' ); ?>
                        </a>
                    </span>
                </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 Furniture Shop', 'vw-furniture-shop' ); ?>" />
                </div>
            </div>
        </div>
        <?php

    }?>
        <?php

}

add_action( 'admin_notices', 'vw_furniture_shop_admin_notice' );

if( ! function_exists( 'vw_furniture_shop_update_admin_notice' ) ) :
/**
 * Updating admin notice on dismiss
*/
function vw_furniture_shop_update_admin_notice(){
   if ( isset( $_GET['vw_furniture_shop_admin_notice'] ) && $_GET['vw_furniture_shop_admin_notice'] == '1' ) {
        update_option( 'vw_furniture_shop_admin_notice', true );
    }
}
endif;
add_action( 'admin_init', 'vw_furniture_shop_update_admin_notice' );

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