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

       if( !$vw_cloudport_meta ){
       if( is_network_admin() ){
            return;
        } if( ! current_user_can( 'manage_options' ) ){
            return;
        }if ( isset($_GET['page']) && $_GET['page'] === 'vw-cloudport-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( '🎉 Welcome to VW Themes,', 'vw-cloudport' ); ?></p>
                    <p class="welcome-text2"><?php esc_html_e( 'You are now using the VW CloudPort, a beautifully designed theme to kickstart your website.', 'vw-cloudport' ); ?></p>
                    <p class="welcome-text3"><?php esc_html_e( 'To help you get started quickly, use the options below:', 'vw-cloudport' ); ?></p>

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

                        <script type="text/javascript">
                            document.getElementById('install-activate-button').addEventListener('click', function () {

                                const vw_cloudport_button = this;

                                const vw_cloudport_redirectUrl = '<?php echo esc_url( admin_url( "themes.php?page=vw-cloudport-info" ) ); ?>';

                                // Check if plugin is already active
                                jQuery.post(
                                    ajaxurl,
                                    {
                                        action: 'check_plugin_activation'
                                    },
                                    function (response) {

                                        // Plugin already active
                                        if (response.success && response.data.active) {

                                            window.location.href = vw_cloudport_redirectUrl;

                                        } else {

                                            // Installing & Activating
                                            vw_cloudport_button.textContent = 'Installing & Activating...';

                                            vw_cloudport_button.style.pointerEvents = 'none';

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

                                                    if (response.success) {

                                                        vw_cloudport_button.textContent = 'Activated';

                                                        // Redirect after activation
                                                        window.location.href = vw_cloudport_redirectUrl;

                                                    } else {

                                                        alert(
                                                            response.data?.message ||
                                                            'Failed to install or activate the plugin.'
                                                        );

                                                        vw_cloudport_button.textContent = 'Try Again';

                                                        vw_cloudport_button.style.pointerEvents = 'auto';
                                                    }

                                                }
                                            );

                                        }

                                    }
                                );

                            });
                        </script>
                    </span>

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

                    <span class="upgrade-btn">
                        <a href="https://www.vwthemes.com/products/cloudport-computing-wordpress-theme" class="button button-primary" target="_blank">
                            <?php esc_html_e( 'UPGRADE TO PRO', 'vw-cloudport' ); ?>
                        </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', 'vw-cloudport' ); ?>
                        </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 CloudPort', 'vw-cloudport' ); ?>" />
                </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 CloudPort', 'vw-cloudport' ); ?>" />
                </div>
            </div>
        </div>
        <?php

    }?>
        <?php

    }

add_action( 'admin_notices', 'vw_cloudport_admin_notice' );

if( ! function_exists( 'vw_cloudport_update_admin_notice' ) ) :
/**
 * Updating admin notice on dismiss
*/
// Admin Notice Update (PERMANENT DISMISS)
function vw_cloudport_update_admin_notice(){
    if ( isset( $_GET['vw_cloudport_admin_notice'] ) && $_GET['vw_cloudport_admin_notice'] == '1' ) {
        update_option( 'vw_cloudport_admin_notice', true );
    }
}
endif;
add_action( 'admin_init', 'vw_cloudport_update_admin_notice' );

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