<?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_taxi_booking_activation_notice' );
    }
});

// Notice after Theme Activation
add_action('admin_notices', 'vw_taxi_booking_activation_notice');

function vw_taxi_booking_activation_notice() {
    // Hide permanently if dismissed
    if ( get_option('vw_taxi_booking_admin_notice') == 1 ) {
        return;
    }
    // Hide ONLY on Get Started page
    if ( isset($_GET['page']) && $_GET['page'] === 'vw_taxi_booking_guide' ) {
        return;
    }
    ?>
    <div id="vw-taxi-booking-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-taxi-booking' ); ?>
                </p>

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

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

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

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

   <script>
        jQuery(document).on('click', '#install-activate-button', function () {

            const button = jQuery(this);
            const redirectUrl = '<?php echo esc_url( admin_url( 'admin.php?page=vw-taxi-booking-info' ) ); ?>';

            button.prop('disabled', true);

            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 () {
                        window.location.href = redirectUrl;

                    });

                }
            });
});
</script>
    <?php
}
// Add bundle image in customizer
add_action('customize_controls_print_footer_scripts', function () {
    ?>
    <script>
        jQuery(document).ready(function($) {
            var vw_taxi_booking_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(vw_taxi_booking_banner);
        });
    </script>
    <?php
});
