<?php
if (class_exists('WC_Widget')) {
    class TFL_MINI_BTN extends WC_Widget
    {
        function __construct()
        {
            $this->widget_cssclass = 'woocommerce widget_mini_btn';
            $this->widget_description = __("Display the button mini header", '');
            $this->widget_id = 'widget_mini_btn';
            $this->widget_name = __('Header Button', '');
            $this->settings = array(
                'tfl_btn' => array(
                    'type' => 'checkbox',
                    'std' => 1,
                    'label' => __('Show Search', '')
                ),
                'tfl_btn2' => array(
                    'type' => 'checkbox',
                    'std' => 1,
                    'label' => __('Show Cart', '')
                ),
                'tfl_btn3' => array(
                    'type' => 'checkbox',
                    'std' => 0,
                    'label' => __('Show Wishlist', '')
                ),
                'tfl_btn4' => array(
                    'type' => 'checkbox',
                    'std' => 0,
                    'label' => __('Show Account', '')
                ),
                'tfl_btn5' => array(
                    'type' => 'checkbox',
                    'std' => 0,
                    'label' => __('Show Hotline', '')
                ),
                'tfl_btn6' => array(
                    'type' => 'checkbox',
                    'std' => 0,
                    'label' => __('Show Chat', '')
                ),

                'tfl_btn7' => array(
                    'type' => 'checkbox',
                    'std' => 0,
                    'label' => __('Show Quote', '')
                ),
            );
            parent::__construct();
        }

        function widget($args, $instance)
        {
            $tfl_btn = empty($instance['tfl_btn']) ? 0 : 1;
            $tfl_btn2 = empty($instance['tfl_btn2']) ? 0 : 1;
            $tfl_btn3 = empty($instance['tfl_btn3']) ? 0 : 1;
            $tfl_btn4 = empty($instance['tfl_btn4']) ? 0 : 1;
            $tfl_btn5 = empty($instance['tfl_btn5']) ? 0 : 1;
            $tfl_btn6 = empty($instance['tfl_btn6']) ? 0 : 1;
            $tfl_btn7 = empty($instance['tfl_btn7']) ? 0 : 1;
            $this->widget_start($args, $instance);
            ?>


            <?php if (!$tfl_btn5 == 0): ?>
            <div class="mini-btn hotline-btn <?php if ($tfl_btn5) {
                echo esc_attr('show-hotline');
            } ?>">
                <?php
                $top_bar_hotline = cs_get_option('top_bar_hotline', tfl_default_options('top_bar_hotline'));
                echo $top_bar_hotline; ?>

            </div>
        <?php endif; ?>


            <?php if (!$tfl_btn == 0): ?>
            <div class="mini-btn search-btn <?php if ($tfl_btn) {
                echo esc_attr('show-search');
            } ?>">


                <?php echo $this->tfl_popup_shortcode(); ?>
            </div>
        <?php endif; ?>

            <?php if (!$tfl_btn2 == 0): ?>
            <div class="mini-btn cart-btn <?php if ($tfl_btn2) {
                echo esc_attr('show-cart');
            } ?>">

                <div class="wrap-cart-btn">
                    <div class="cart-btn tfl-wsc-basket">
                        <?php if (class_exists('WooCommerce')) : ?>
                            <span class="fas fa-shopping-cart ">
                            <span class="cart-count tfl-wsc-items-count"></span>
                        </span>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php endif; ?>


            <?php
            if (!$tfl_btn6 == 0):
                $chat_link = cs_get_option('link_chat', tfl_default_options('link_chat'));
                ?>
                <div class="mini-btn chat-btn">
                    <a href="<?php echo esc_url($chat_link); ?>">
                        <img src="<?php echo TFL_PLG_PATH . 'assets/images/svg/comment-alt-dots.svg'; ?>" alt="chat">
                    </a>
                </div>
            <?php endif; ?>


            <?php if (!$tfl_btn3 == 0): ?>
            <div class="mini-btn wishlist-btn">

                <?php if (yith_wcwl_count_all_products()): ?>
                    <a class="nav-wishlist-trigger"
                       href="<?php echo esc_url(htmlspecialchars(YITH_WCWL()->get_wishlist_url())); ?>">
                        <span class="fas fa-heart">
                            <span class="tfl-wsc-items-count-wishlist">
                                <?php echo yith_wcwl_count_all_products(); ?>
                            </span>
                        </span>
                    </a>
                <?php else: ?>
                    <span class="fas fa-heart">
                        <span class="tfl-wsc-items-count-wishlist">0</span>
                    </span>
                <?php endif ?>
            </div>
        <?php endif; ?>

        <?php if (!$tfl_btn4 == 0): ?>
            <div class="mini-btn account-btn">
                <a href="<?php echo get_permalink(get_option('woocommerce_myaccount_page_id')); ?>"
                   title="<?php _e('My Account', 'tfl'); ?>"><span class="fas fa-user"></span></a>
            </div>

        <?php endif; ?>

        <?php if (!$tfl_btn7 == 0):
            $header = tfl_meta_option('header_type');
            $quote_link = cs_get_option('quote_link','#');
            $quote_text = cs_get_option('quote_text', esc_html__('Get A Quote', 'gettree'));
            ?>
                <div class="wrap-get-quote">
                    <div class="get-quote">
                        <a href="<?php echo esc_url($quote_link); ?>"><?php echo esc_html($quote_text); ?>
                            <img src="<?php echo TFL_PLG_PATH . 'assets/images/svg/arrow-right-regular.svg';?>" alt=" <?php echo esc_attr__('quote', 'gettree'); ?>">
                        </a>
                    </div>
                    <div class="mini-btn grid-btn">
                        <img src="<?php echo TFL_PLG_PATH . 'assets/images/svg/bar.svg';?>" alt=" <?php echo esc_attr__('bar', 'gettree'); ?>">
                    </div>
                </div>
        <?php endif; ?>

            <?php
            $this->widget_end($args);
        }

        function tfl_popup_shortcode($content = null)
        {
            $popup_effect = 'fadein';
            $enable_loader = 'true';
            $full_screen = 'true';
            $speedin = 300;
            $speedout = 300;
            $delay = 300;
            wp_enqueue_style('tfl-public-style');
            wp_enqueue_script('tfl-popup-js');
            $unique = tfl_get_unique();
            ob_start();
            $data_tfl_str = '{"content":';
            $data_tfl_str .= '{"effect": "' . $popup_effect . '", "fullscreen": ' . $full_screen . ', "speedIn": ' . $speedin . ', "speedOut": ' . $speedout . ', "delay": ' . $delay . '},';
            $data_tfl_str .= '"loader":';
            $data_tfl_str .= '{"active": ' . $enable_loader . '}';
            $data_tfl_str .= '}';
            ?>

            <div class="wrap-search-btn">
                <span class="fas fa-search"></span>
            </div>

            <div id="tfl-modal-<?php echo $unique; ?>" class="tfl-modal">
                <a href="javascript:void(0);" onclick="Custombox.modal.close();" class="tfl-close"></a>
                <div class="tfl-search-box">
                    <form role="search" method="get" class="searchform" action="<?php echo esc_url(home_url('/')); ?>">
                        <input placeholder="<?php _e('Type search term and press enter', 'wp-clean-responsive'); ?>"
                               type="search" value="<?php echo get_search_query(); ?>" name="s"/>
                        <button type="submit"
                                class="search-btn"><?php _e('Search', 'search-and-navigation-popup'); ?></button>
                    </form><!-- end #searchform -->
                </div>
            </div>
            <script>
                jQuery(function () {
                    jQuery(document).on('click', '[data-tfl-<?php echo $unique;?>]', function () {
                        var options = jQuery(this).data('tfl-<?php echo $unique;?>');
                        if (!options.content.target) {
                            options.content.target = '#tfl-modal-<?php echo $unique;?>';
                        }
                        new Custombox.modal(options).open();
                    });
                });
            </script>
            <?php
            $content .= ob_get_clean();
            return $content;
        }
    }
}
function register_widget_1()
{
    if (class_exists('WC_Widget')) {
        register_widget('TFL_MINI_BTN');
    }
}

add_action('widgets_init', 'register_widget_1');