<?php

namespace core_next;
class calendar extends \WP_Widget
{
    function __construct()
    {
        $img_html = Theme::getWidgetIcon();
        parent::__construct(
            'core_next_calendar_widget',
            $img_html . 'CoreNext炫彩日历',
            array(
                'description' => '多功能日历组件'
            )
        );
    }

    function form($instance)
    {
        $theme = $instance['theme'] ?? 'default';
        $title = $instance['title'] ?? '日历';

        ?>
        <div class="widget-item">
            <div>工具标题</div>
            <input class="widefat" name="<?php echo esc_attr($this->get_field_name('title')); ?>"
                   type="text" value="<?php echo esc_attr($title); ?>"/>
        </div>
        <div class="widget-item">
            <div>主题色</div>
            <select name="<?php echo $this->get_field_name('theme'); ?>">
                <option value="default" <?php if ($theme == 'default') echo 'selected' ?>>跟随主题色</option>
                <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'] ?? 'default';
        $title = $instance['title'] ?? '日历';

        $weekday = Tools::getWeekdayName();
        $currentMonth = date('n');
        $currentDay = date('d');


        $daysInMonth = date('t');
        $firstDayOfWeek = date('N', strtotime(date('Y-m-01')));
        // 如果第一天不是周一，则在数组开头添加空字符串元素
        if ($firstDayOfWeek != 1) {
            $days = array_fill(0, $firstDayOfWeek - 1, '');
        } else {
            $days = [];
        }
        // 生成当月的日期数组
        for ($i = 1; $i <= $daysInMonth; $i++) {
            $days[] = $i;
        }

        ?>
        <div class="core-next-calendar <?php echo $theme; ?>">
            <div class="calendar-header">
                <div class="calendar-title">
                    <?php echo $title; ?>
                </div>
                <div class="calendar-header-right">
                    <div class="calendar-month-week">
                        <div><?php echo $currentMonth; ?>月</div>
                        <div><?php echo $weekday; ?></div>
                    </div>
                    <div class="calendar-current-day">
                        <div><?php echo $currentDay; ?></div>
                    </div>
                    <img class="img-calendar-header-1"
                         src="<?php echo Config::$img_url . '/widget/calendar-header-1.svg'; ?>">
                    <img class="img-calendar-header-2"
                         src="<?php echo Config::$img_url . '/widget/calendar-header-2.svg'; ?>">
                    <img class="img-calendar-header-3"
                         src="<?php echo Config::$img_url . '/widget/calendar-header-3.svg'; ?>">

                </div>
            </div>
            <div class="calendar-main">
                <div class="calendar-body-header">
                    <ul>
                        <li>一</li>
                        <li>二</li>
                        <li>三</li>
                        <li>四</li>
                        <li>五</li>
                        <li>六</li>
                        <li>日</li>
                    </ul>
                    <ul>
                        <?php
                        foreach ($days as $day) {
                            if ($day == $currentDay) {
                                echo '<li class="calendar-current-day">' . $day . '</li>';
                            } else {
                                echo "<li>$day</li>";
                            }

                        }
                        ?>
                    </ul>
                </div>
            </div>
        </div>
        <?php
    }
}