<?php

namespace core_next;

class webmaster extends \WP_Widget
{

    function __construct()
    {
        $img_html = Theme::getWidgetIcon();
        parent::__construct(
            'core_next_webmaster_widget',
            $img_html . 'CoreNext炫彩站长小工具',
            array(
                'description' => '展示个性化站长信息'
            )
        );
    }

    function form($instance)
    {
        $webmaster_name = $instance['webmaster_name'] ?? '站长一枚';
        $webmaster_description = $instance['webmaster_description'] ?? '这是一个奇怪的站长，白天睡大觉，晚上魂飘飘~~~';
        $webmaster_bg_default = Config::$img_url . "/widget/webmaster-bg.webp";
        $webmaster_bg = $instance['webmaster_bg'] ?? $webmaster_bg_default;


        $avatar_id = $this->get_field_id('avatar');
        $avatar_img_id = $this->get_field_id('avatar_img');

        $avatar_name = $this->get_field_name('avatar');
        $avatar_default = Config::$img_url . "/widget/avatar-default.svg";
        $avatar = $instance['avatar'] ?? $avatar_default;

        $webmaster_bg_id = $this->get_field_id('background_image');
        $webmaster_bg_img_id = $this->get_field_id('background_image_show');
        $block_introduce_id = $this->get_field_id('block_introduce');
        $block_introduce_name = $this->get_field_name('block_introduce');
        $block_introduce = $instance['block_introduce'] ?? '[instance_card name="B站" url="https://space.bilibili.com/313573880"]@果核剥壳[/instance_card]';
        $item_introduce_id = $this->get_field_id('item_introduce');
        $item_introduce_name = $this->get_field_name('item_introduce');
        $item_introduce = $instance['item_introduce'] ?? '[instance_card name="QQ"]123456[/instance_card]';


        ?>
        <div class="widget-item">
            <div>站长名</div>
            <input class="widefat" name="<?php echo esc_attr($this->get_field_name('webmaster_name')); ?>"
                   type="text" value="<?php echo esc_attr($webmaster_name); ?>">
        </div>
        <div class="widget-item">
            <div>头像</div>
            <input id="<?php echo $avatar_id; ?>" class="widefat"
                   name="<?php echo $avatar_name ?>"
                   type="text" value="<?php echo esc_attr($avatar); ?>">
            <img src="<?php echo esc_attr($avatar); ?>" style="width: 40px;height: 40px"
                 id="<?php echo $avatar_img_id; ?>">
            <div style="display: flex;gap: 20px;justify-content: space-between">
                <button class="button" type="button" style=""
                        onclick="jQuery('#<?php echo $avatar_id ?>').val('<?php echo $avatar_default; ?>').trigger('change');jQuery('#<?php echo $avatar_img_id; ?>').attr('src', '<?php echo $avatar_default; ?>');">
                    使用默认
                </button>
                <button class="button button-primary" type="button" style=""
                        onclick="coreNextUploadImg('<?php echo $avatar_id; ?>','<?php echo $avatar_img_id; ?>')">
                    上传
                </button>
            </div>
        </div>
        <div class="widget-item">
            <div>站长描述</div>
            <input class="widefat" name="<?php echo esc_attr($this->get_field_name('webmaster_description')); ?>"
                   type="text" value="<?php echo esc_attr($webmaster_description); ?>">
        </div>
        <div class="widget-item">
            <div>顶部背景图</div>
            <input id="<?php echo $webmaster_bg_id ?>" class="widefat"
                   name="<?php echo $this->get_field_name('webmaster_bg'); ?>"
                   type="text" value="<?php echo esc_attr($webmaster_bg); ?>">
            <img src="<?php echo esc_attr($webmaster_bg); ?>" style="width: 100%;height: 78px;" width="100%"
                 id="<?php echo $webmaster_bg_img_id; ?>">
            <div style="display: flex;gap: 20px;justify-content: space-between">
                <button class="button" type="button" style=""
                        onclick="jQuery('#<?php echo $webmaster_bg_id ?>').val('<?php echo $webmaster_bg_default; ?>').trigger('change');jQuery('#<?php echo $webmaster_bg_img_id; ?>').attr('src', '<?php echo $webmaster_bg_default; ?>');">
                    使用默认
                </button>
                <button class="button button-primary" type="button" style=""
                        onclick="coreNextUploadImg('<?php echo $webmaster_bg_id; ?>','<?php echo $webmaster_bg_img_id; ?>')">
                    上传
                </button>
            </div>
        </div>
        <div class="widget-item">
            <div>长块介绍</div>
            <textarea class="widefat" id="<?php echo $block_introduce_id ?>" rows="8"
                      name="<?php echo $block_introduce_name; ?>"><?php echo esc_attr($block_introduce); ?></textarea>
        </div>
        <div class="widget-item">
            <div>短块介绍</div>
            <textarea class="widefat" id="<?php echo $item_introduce_id ?>" rows="8"
                      name="<?php echo $item_introduce_name; ?>"><?php echo esc_attr($item_introduce); ?></textarea>
        </div>
        <div class="widget-item">
            <a href="https://www.yuque.com/applek/corenext/ax1qx7ob5rw4gxzc" target="_blank">站长卡片配置教程</a>
        </div>

        <?php
    }

    public function widget_start($args, $instance)
    {
        if (isset($args['before_widget'])) {
            $html = $args['before_widget'];
            echo $html;
        }
    }

    public function widget_end($args)
    {
        if (isset($args['after_widget'])) {
            $html = $args['after_widget'];
            echo $html;
        }
    }

    function widget($args, $instance)
    {

        $webmaster_name = $instance['webmaster_name'] ?? '站长一枚';
        $webmaster_description = $instance['webmaster_description'] ?? '这是一个奇怪的站长，白天睡大觉，晚上魂飘飘~~~';
        $webmaster_bg_default = Config::$img_url . "/widget/webmaster-bg.webp";
        $webmaster_bg = $instance['webmaster_bg'] ?? $webmaster_bg_default;
        $avatar_default = Config::$img_url . "/widget/avatar-default.svg";
        $avatar = $instance['avatar'] ?? $avatar_default;
        $block_introduce = $instance['block_introduce'] ?? '[instance_card name="B站" url="https://space.bilibili.com/313573880"]@果核剥壳[/instance_card]';
        $item_introduce = $instance['item_introduce'] ?? '[instance_card name="QQ"]123456[/instance_card]';

        $current_date = date('Y-m-d');
        // 获取本月第一天的日期
        $first_day_of_month = date('Y-m-01');
        // 构建查询参数
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'date_query' => array(
                array(
                    'after' => $first_day_of_month,
                    'before' => $current_date,
                    'inclusive' => true,
                ),
            ),
            'no_found_rows' => true,
            'suppress_filters' => true,
            'fields' => 'ids',
            'posts_per_page' => -1,
        );
        $query = new \WP_Query($args);
        $month_post_count = $query->post_count;

        $this_week_start = strtotime('this week monday');
        $this_week_end = strtotime('this week sunday');
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'date_query' => array(
                array(
                    'after' => date('Y-m-d H:i:s', $this_week_start),
                    'before' => date('Y-m-d H:i:s', $this_week_end),
                    'inclusive' => true,
                ),
            ),
            'no_found_rows' => true,
            'suppress_filters' => true,
            'fields' => 'ids',
            'posts_per_page' => -1,
        );
        $query = new \WP_Query($args);
        $week_post_count = $query->post_count;
        $user_count = count_users();
        $this->widget_start($args, $instance);
        ?>
        <div class="webmaster-widget">
            <div class="webmaster-header"
                 style="background-image: url(<?php echo $webmaster_bg; ?>)">
                <img class="webmaster-avatar" src="<?php echo $avatar; ?>">
                <div>
                    <div><?php echo $webmaster_name; ?></div>
                    <div class="webmaster-description"><?php echo $webmaster_description; ?></div>
                </div>
            </div>
            <div class="webmaster-site-info">
                <div class="webmaster-title">数据统计</div>
                <div class="site-info-list">
                    <div class="data-item">
                        <div class="data-title">本周更新</div>
                        <div>
                            <span><?php echo $week_post_count; ?></span>篇
                        </div>
                    </div>
                    <div class="data-item">
                        <div class="data-title">本月更新</div>
                        <div><span><?php echo $month_post_count; ?></span>篇</div>
                    </div>
                    <div class="data-item">
                        <div class="data-title">用户数量</div>
                        <div><span><?php echo $user_count['total_users']; ?></span>位</div>
                    </div>
                </div>
            </div>
            <div class="introduce-block-list">
                <?php echo do_shortcode($block_introduce); ?>
            </div>
            <div class="introduce-item-list">
                <?php echo do_shortcode($item_introduce); ?>
            </div>
        </div>
        <?php
        $this->widget_end($args);
    }

}