<?php

namespace core_next;
class timeProgress extends \WP_Widget
{
    function __construct()
    {
        $img_html = Theme::getWidgetIcon();
        parent::__construct(
            'core_next_time_progress',
            $img_html . 'CoreNext时间进度',
            array(
                'description' => '显示已经过去多久啦。目前有当月'
            )
        );
    }

    function form($instance)
    {
        $theme = $instance['theme'] ?? 'blue';
        ?>
        <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)
    {
        date_default_timezone_set('Asia/Shanghai');
        $month_count = date('t');
        $day_use = date('j');
        $month = date('n');
        $percentage = ceil(($day_use / $month_count) * 100);
        $chinese_month = Tools::convertToChineseNumber($month);
        $theme = $instance['theme'] ?? 'blue';
        ?>
        <div class="core-next-time-progress <?php echo $theme;?>">
            <div class="progress-warp">
                <div class="progress-progress" style="width: <?php echo $percentage; ?>%;"></div>
                <div class="progress-text"><?php echo $percentage; ?>%</div>
            </div>
            <div class="progress-note">
                <div class="progress-time-title"><?php echo $chinese_month; ?>月</div>
                <div class="progress-time-sub-title">
                    <?php echo $day_use; ?>天已过
                </div>
            </div>
        </div>
        <?php
    }
}