<?php

namespace core_next;
class weather extends \WP_Widget
{

    function __construct()
    {
        $img_html = Theme::getWidgetIcon();
        parent::__construct(
            'core_next_weather_widget',
            $img_html . 'CoreNext天气模块',
            array(
                'description' => '显示访问者天气'
            )
        );
    }

    function form($instance)
    {

        $title = isset($instance['title']) ? $instance['title'] : '实时天气';
        $displaytype = isset($instance['displaytype']) ? $instance['displaytype'] : 'hf';
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
                   name="<?php echo $this->get_field_name('title'); ?>" type="text"
                   value="<?php echo esc_attr($title); ?>"/></p>
        <p>
            <label for="<?php echo $this->get_field_id('displaytype'); ?>">显示样式</label>
            <select name="<?php echo $this->get_field_name('displaytype'); ?>">
                <option value="hf" <?php if ($displaytype == 'hf') echo 'selected' ?>>和风天气</option>
                <!--              <option value="tqapi1" <?php /*if ($displaytype == 'tqapi1') echo 'selected' */ ?>>天气api1</option>
                <option value="tqapi2" <?php /*if ($displaytype == 'tqapi2') echo 'selected' */ ?>>天气api2</option>
                <option value="tqapi3" <?php /*if ($displaytype == 'tqapi3') echo 'selected' */ ?>>天气api3</option>
                <option value="tqapi4" <?php /*if ($displaytype == 'tqapi4') echo 'selected' */ ?>>天气api4</option>-->
            </select>
        </p>



        <?php
    }

    public function widget_start($args, $instance)
    {
        echo $args['before_widget'];
        if ($title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base)) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
    }

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


    function update($new_instance, $old_instance)
    {
        return $new_instance;
    }

    function widget($args, $instance)
    {

        $this->widget_start($args, $instance);
        $displaytype = (!empty($instance['displaytype'])) ? $instance['displaytype'] : 'hf';

        if ($displaytype == 'hf') {
            ?>
            <div class="cp-weather">
                <div id="he-plugin-standard"></div>
            </div>
            <script>
                WIDGET = {
                    "CONFIG": {
                        "layout": "2",
                        "height": 270,
                        "background": "2",
                        "dataColor": "434343",
                        "key": "4837d979e7114d26b28e328aa856d6f8"
                    }
                }
            </script>
            <script src="https://widget.qweather.net/standard/static/js/he-standard-common.js?v=2.0"></script>
            <?php
        } elseif ($displaytype == 'tqapi1') {
            ?>
            <iframe scrolling="no" src="https://tianqiapi.com/api.php?style=tp&skin=durian" frameborder="0" width="100%"
                    height="260" allowtransparency="true"></iframe>
            <?php
        } elseif ($displaytype == 'tqapi2') {
            ?>
            <iframe scrolling="no" src="https://tianqiapi.com/api.php?style=tr&skin=durian" frameborder="0" width="100%"
                    height="220" allowtransparency="true"></iframe>
            <?php
        } elseif ($displaytype == 'tqapi3') {
            ?>
            <iframe scrolling="no" src="https://tianqiapi.com/api.php?style=ti&skin=durian" frameborder="0" width="100%"
                    height="270" allowtransparency="true"></iframe>
            <?php
        } elseif ($displaytype == 'tqapi4') {
            ?>
            <iframe scrolling="no" src="https://tianqiapi.com/api.php?style=ya&skin=orange" frameborder="0" width="100%"
                    height="150" allowtransparency="true"></iframe>
            <?php
        }

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