<?php

namespace core_next;
class search extends \WP_Widget
{
    function __construct()
    {
        $img_html = Theme::getWidgetIcon();
        parent::__construct(
            'core_next_search',
            $img_html . 'CoreNext炫彩搜索框',
            array(
                'description' => '多功能搜索框，搜本站，搜全网'
            )
        );
    }

    function form($instance)
    {
        $search_site = isset($instance['search_site']) ? $instance['search_site'] : 'baidu';
        $only_search_site = !empty($instance['only_search_site']) ? 'checked' : '';
        $theme = isset($instance['theme']) ? $instance['theme'] : 'blue';
        ?>
        <div class="widget-item">
            <div>搜全网引擎</div>
            <select name="<?php echo $this->get_field_name('search_site'); ?>">
                <option value="baidu" <?php if ($search_site == 'baidu') echo 'selected' ?>>百度</option>
                <option value="google" <?php if ($search_site == 'google') echo 'selected' ?>>Google</option>
                <option value="bing" <?php if ($search_site == 'bing') echo 'selected' ?>>必应</option>
            </select>
        </div>
        <div class="widget-item">
            <div>搜全网引擎</div>
            <select name="<?php echo $this->get_field_name('search_site'); ?>">
                <option value="baidu" <?php if ($search_site == 'baidu') echo 'selected' ?>>百度</option>
                <option value="google" <?php if ($search_site == 'google') echo 'selected' ?>>Google</option>
                <option value="bing" <?php if ($search_site == 'bing') echo 'selected' ?>>必应</option>
            </select>
        </div>
        <div class="widget-item">
            <span><input class="widefat" type="checkbox"
                         name="<?php echo $this->get_field_name('only_search_site'); ?>"
                <?php echo $only_search_site; ?>>搜全网限制只搜本站</span>
        </div>
        <div class="widget-item">
            <div>主题色</div>
            <select name="<?php echo $this->get_field_name('theme'); ?>">
                <option value="blue" <?php if ($theme == 'blue') echo 'selected' ?>>蓝色</option>
                <option value="grey" <?php if ($theme == 'grey') echo 'selected' ?>>灰色</option>
                <option value="red" <?php if ($theme == 'red') echo 'selected' ?>>红色</option>
                <option value="green" <?php if ($theme == 'green') echo 'selected' ?>>绿色</option>
                <option value="orange" <?php if ($theme == 'orange') echo 'selected' ?>>橘色</option>
                <option value="purple" <?php if ($theme == 'purple') echo 'selected' ?>>紫色</option>
            </select>
        </div>
        <?php
    }

    public function widget_start($args, $instance)
    {
        $on_top = !empty($instance['on_top']) ? 'checked' : '';
        $html = $args['before_widget'];
        if ($on_top !== '') {
            $html = str_replace('<div', Theme::getWidgetStickyStyle(), $html);
        }
        echo $html;
    }

    public function widget_end($args)
    {
        echo $args['after_widget'];
    }

    function widget($args, $instance)
    {
        $theme = $instance['theme'] ?? 'blue';
        $search_site = $instance['search_site'] ?? 'baidu';
        $domain = $_SERVER['HTTP_HOST'];
        $only_search_site = !empty($instance['only_search_site']) ? 'checked' : '';
        if ($only_search_site === 'checked') {
            $word = urlencode("site:$domain ");
        } else {
            $word = '';
        }

        if ($search_site == 'baidu') {
            $search_url = "https://www.baidu.com/s?wd=$word";
        } elseif ($search_site == 'google') {
            $search_url = "https://www.google.com/search?q=$word";
        } else {
            $search_url = "https://www.bing.com/search?q=$word";
        }

        $site_url = esc_url(home_url('/'));
        ?>
        <div class="core-next-search-widget <?php echo $theme; ?>">
            <input type="text" placeholder="在此，搜你想搜的"
                   onkeydown="if(event.key==='Enter'){ window.open('<?php echo $site_url . '?s=' ?>'+this.value); }">
            <div class="btn-list">
                <div class="search-site"
                     onclick="window.open('<?php echo $site_url . '?s=' ?>'+this.parentElement.parentElement.querySelector('input').value)">
                    <img src="<?php echo Config::$img_url . '/icon/search.svg' ?>">
                    搜站内
                </div>
                <div class="search-web"
                     onclick="window.open('<?php echo $search_url ?>'+encodeURI(this.parentElement.parentElement.querySelector('input').value))">
                    <img src="<?php echo Config::$img_url . '/icon/search.svg' ?>">
                    搜全网
                </div>
            </div>
        </div>
        <?php
    }
}