<?php
define('__ADMIN_DIR__', __TYPECHO_ROOT_DIR__ . __TYPECHO_ADMIN_DIR__);
/** 初始化组件 */
\Widget\Init::alloc();

/** 注册一个初始化插件 */
\Typecho\Plugin::factory('admin/common.php')->begin($k=1);

\Widget\Options::alloc()->to($options);
\Widget\User::alloc()->to($user);
\Widget\Security::alloc()->to($security);
\Widget\Menu::alloc()->to($menu);

/** 初始化上下文 */
$request = $options->request;
$response = $options->response;

$icons=$options->pluginUrl.'/NewAdmin/assets/img/icons.svg?1'; 
$menu->title = _t('网站概要');
include __ADMIN_DIR__ .'header.php';
include __ADMIN_DIR__ .'menu.php';
$stat = Typecho_Widget::widget('Widget_Stat');
$url=Helper::options()->pluginUrl.'/NewAdmin/assets/'; 
$plugin = $options->plugin('NewAdmin');
?>
<div class="main">
    
    <div class="body container mb-10">
<div class="typecho-page-title updateTag">
    <h2>控制台</h2>
</div>
        <div class="row" role="main">
            <?php if($user->pass('administrator', true)): ?>
                    <div class="col-mb-12 adminlink" role="complementary">
                    <a href="<?php $options->adminUrl('themes.php'); ?>"><?php _e('更换外观'); ?></a>
                    <a href="<?php $options->adminUrl('plugins.php'); ?>"><?php _e('插件管理'); ?></a>
                    <a href="<?php $options->adminUrl('options-general.php'); ?>"><?php _e('系统设置'); ?></a>
                    </div>
                    <?php endif; ?>
                    
            <div class="col-mb-6 col-tb-3">
                <div class="kuai bg1">
                <div class="flex-1">
                <div class="stitle"><span>文章统计</span><span><svg class="icon" aria-hidden="true"><use xlink:href="<?php echo $icons;?>#icon-pingtai"></use></svg></span></div>
                <div class="sbody"><span x-data="{ shown: false }" x-intersect="shown = true" :class="{'ani-num':shown}" style="--num: <?php $stat->myPublishedPostsNum(); ?>"><?php $stat->myPublishedPostsNum(); ?></span>篇文章</div>
                </div>
                <div class="sfooter">
                <?php if($user->pass('contributor', true)): ?>
                <a href="<?php $options->adminUrl('write-post.php'); ?>"><?php _e('撰写文章'); ?></a>
                <?php else: ?>撰写文章
                <?php endif; ?>
                </div>
                
                </div>
            </div>
            <div class="col-mb-6 col-tb-3">
                <div class="kuai bg2">
                 <div class="flex-1">
                <div class="stitle"><span>分类统计</span><span><svg class="icon" aria-hidden="true"><use xlink:href="<?php echo $icons;?>#icon-daichuli"></use></svg></span></div>
                <div class="sbody"><span x-data="{ shown: false }" x-intersect="shown = true" :class="{'ani-num':shown}"  style="--num: <?php $stat->categoriesNum(); ?>"><?php $stat->categoriesNum(); ?></span>个分类</div>
                </div>
                <div class="sfooter">
                <?php if($user->pass('editor', true)): ?>
                    <a href="<?php $options->adminUrl('manage-categories.php'); ?>"><?php _e('管理分类'); ?></a>
                <?php else: ?>管理分类
                <?php endif; ?>
                    </div>
                </div>
            </div>
            <div class="col-mb-6 col-tb-3">
                <div class="kuai bg3">
                 <div class="flex-1">
                <div class="stitle"><span>评论统计</span><span><svg class="icon" aria-hidden="true"><use xlink:href="<?php echo $icons;?>#icon-jiankong"></use></svg></span></div>
                <div class="sbody"><span x-data="{ shown: false }" x-intersect="shown = true" :class="{'ani-num':shown}"  style="--num: <?php $stat->myPublishedCommentsNum(); ?>"><?php $stat->myPublishedCommentsNum(); ?></span>条评论</div>
                </div>
                <div class="sfooter">
                <?php $n=0; if($user->pass('editor', true) && 'on' == $request->get('__typecho_all_comments') && $stat->waitingCommentsNum > 0): ?>
                        <a href="<?php $n++; $options->adminUrl('manage-comments.php?status=waiting'); ?>"><span class="balloon"><?php $stat->waitingCommentsNum(); ?></span> <?php _e('待审核的评论'); ?></a>
                    <?php elseif($stat->myWaitingCommentsNum > 0): ?>
                        <a href="<?php $n++; $options->adminUrl('manage-comments.php?status=waiting'); ?>"><span class="balloon"><?php $stat->myWaitingCommentsNum(); ?></span> <?php _e('待审核评论'); ?></a>
                    <?php endif; ?>
                    <?php if($user->pass('editor', true) && 'on' == $request->get('__typecho_all_comments') && $stat->spamCommentsNum > 0): ?>
                        <a href="<?php $n++; $options->adminUrl('manage-comments.php?status=spam'); ?>"><span class="balloon"><?php $stat->spamCommentsNum(); ?></span> <?php _e('垃圾评论'); ?></a>
                    <?php elseif($stat->mySpamCommentsNum > 0): ?>
                    <a href="<?php $n++; $options->adminUrl('manage-comments.php?status=spam'); ?>"><span class="balloon"><?php $stat->mySpamCommentsNum(); ?></span> <?php _e('垃圾评论'); ?></a>
                    <?php endif; ?>
                    <?php if($user->pass('editor', true) && $n==0): ?>
                        <a href="<?php $n++; $options->adminUrl('manage-comments.php'); ?>"><?php _e('管理评论'); ?></a>
                    <?php else: ?>管理评论
                    <?php endif; ?>
                </div>
                </div>
            </div>
            <div class="col-mb-6 col-tb-3">
                <div class="kuai bg4">
                 <div class="flex-1">
                <div class="stitle"><span>基础设置</span><span><svg class="icon" aria-hidden="true"><use xlink:href="<?php echo $icons;?>#icon-faxian"></use></svg></span></div>
                <div class="sbody"><?php $user->name(); ?></div>
                </div>
                <div class="sfooter">
                <a href="<?php $options->adminUrl('profile.php'); ?>"><?php _e('更新我的资料'); ?></a>
                    </div>
                </div>
            </div>

            
        </div>
    </div>
    
    
    
    
    
    <div class="container mb-10">
<div class="typecho-page-title">
    <h2>图表</h2>
</div>   
       
 <div class="row">
            <div class="col-mb-12 col-tb-6 mb-10" role="complementary">
                <section class="latest-link typecho-page-main">
                    <h3><?php _e('更新频率'); ?></h3>
                    <canvas id="myChart1" height="300"></canvas>
                </section>
            </div>

            <div class="col-mb-12 col-tb-6 mb-10" role="complementary">
                <section class="latest-link typecho-page-main">
                    <h3><?php _e('分类饼图'); ?></h3>
                    <canvas id="myChart2" height="300"></canvas>
                </section>
            </div>


        </div>
    </div>
    
    
    <div class="container">
        
       
<div class="typecho-page-title">
    <h2>最近动态</h2>
</div>    
 <div class="row">
            <div class="col-mb-12 col-tb-6" role="complementary">
                <section class="latest-link typecho-page-main">
                    <h3><?php _e('文章分布'); ?></h3>
                    <?php Typecho_Widget::widget('Widget_Contents_Post_Recent', 'pageSize=10')->to($posts); ?>
                    <ul>
                    <?php if($posts->have()): ?>
                    <?php while($posts->next()): ?>
                        <li>
                            <span><?php $posts->date('Y-m-d'); ?></span>
                            <a href="<?php $posts->permalink(); ?>" class="title"><?php $posts->title(); ?></a>
                        </li>
                    <?php endwhile; ?>
                    <?php else: ?>
                        <li><em><?php _e('暂时没有文章'); ?></em></li>
                    <?php endif; ?>
                    </ul>
                </section>
            </div>

            <div class="col-mb-12 col-tb-6" role="complementary">
                <section class="latest-link typecho-page-main">
                    <h3><?php _e('近期评论'); ?></h3>
                    <ul>
                        <?php Typecho_Widget::widget('Widget_Comments_Recent', 'pageSize=10')->to($comments); ?>
                        <?php if($comments->have()): ?>
                        <?php while($comments->next()): ?>
                        <li>
                            <span><?php $comments->date('Y-m-d'); ?></span>
                            <a href="<?php $comments->permalink(); ?>" class="title"><?php $comments->author(true); ?></a>:
                            <?php $comments->excerpt(35, '...'); ?>
                        </li>
                        <?php endwhile; ?>
                        <?php else: ?>
                        <li><?php _e('暂时没有回复'); ?></li>
                        <?php endif; ?>
                    </ul>
                </section>
            </div>


        </div>
    </div>
</div>


<script src="<?php echo $url; ?>chart.umd.min.js"></script>
 
<script>
  const ctx = document.getElementById('myChart1');
  const ctx2 = document.getElementById('myChart2');

<?php \Widget\Contents\Post\Date::alloc('format=Y-m&type=month&limit=0')->to($articles);
$adata=[];$k=0;
while ($articles->next()&&$k<9){
$k++;
    $adata[]=array(
"date"=>$articles->date,
"count"=>$articles->count,
);
}
$articlesdate="";$articlesnum="";
foreach($adata as $value){ 
    $articlesdate=$articlesdate."'".$value['date']."',";
    $articlesnum=$articlesnum.$value['count'].',';}
 ?>
  new Chart(ctx, {
    type: 'line',
    data: {
      labels: [<?php echo $articlesdate; ?>],
      datasets: [{
        label: '文章数量',
        data: [<?php echo $articlesnum; ?>],
        borderWidth: 2,
        fill: false,
        borderColor: 'rgb(154,197,229)',
        backgroundColor: 'rgba(154,197,229,.3)',
        pointBackgroundColor: 'rgb(237, 206, 122)',
        pointBorderColor: 'rgb(237, 206, 122)',
        tension:0.5,
      }]
    },
    options: {
      scales: {
        y: {
          beginAtZero: true
        },
      },
        plugins: {
            legend: {
                display: true,
                labels: {
                    color: 'rgb(255, 99, 132)',
                },
            }
        }
    }
  });  



<?php \Widget\Metas\Category\Rows::alloc()->to($cates); ?>
<?php 
$data=[];
while ($cates->next()){
$data[]=array(
"name"=>$cates->name,
"count"=>$cates->count,
);
}

$catename="";$catenum="";
foreach($data as $value){ 
    $catename=$catename."`".$value['name']."(".$value['count'].")`,";
    $catenum=$catenum.$value['count'].',';}
?>

  new Chart(ctx2, {
    type: 'polarArea',
    data: {
      labels: [
<?php echo $catename ?>
  ],
  datasets: [{
    label: '文章数量',
    data: [
<?php echo $catenum; ?>],
    backgroundColor: [
      'rgb(154,197,229)',
      'rgb(79,177,157)',
      'rgb(237,206,122)',
      'rgb(201,140,154)',
      'rgb(229,198,195)',
      'rgb(255, 99, 132)',
      'rgb(75, 192, 192)',
      'rgb(255, 205, 86)',
      'rgb(201, 203, 207)',
      'rgb(54, 162, 235)'
    ]
  }]
    },
    options: {
      scales: {
        y: {
          beginAtZero: true
        }
      },
        plugins: {
            legend: {
                display: true,
                labels: {
                    color: 'rgb(255, 99, 132)',
                },
            title: {
                    color: 'rgb(255, 99, 132)',
            },
            }
        }
    }
  });
</script>

<?php
include __ADMIN_DIR__ .'copyright.php';
include __ADMIN_DIR__ .'common-js.php';
?>

<?php if(!$plugin->updateNotice): ?>
<script>
    $(document).ready(function () {
        var ul = $('#typecho-message ul'), cache = window.sessionStorage,
            update = cache ? cache.getItem('update') : '';

        function applyUpdate(update) {
            if (update.available) {
                $('<div class="update-check message error mb-10" style="border-radius: 8px;"><p>'
                    + '<?php _e('您当前使用的版本是 %s'); ?>'.replace('%s', update.current) + '<br />'
                    + '<strong><a href="' + update.link + '" target="_blank">'
                    + '<?php _e('官方最新版本是 %s'); ?>'.replace('%s', update.latest) + '</a></strong></p></div>')
                    .insertAfter('.typecho-page-title.updateTag').effect('highlight');
            }
        }

        if (!!update) {
            applyUpdate($.parseJSON(update));
        } else {
            $.get('<?php $options->index('/action/ajax?do=checkVersion'); ?>', function (o, status, resp) {
                applyUpdate(o);
                cache.setItem('update', resp.responseText);
            }, 'json');
        }
    });
</script>
<?php endif;?>
<?php
include __ADMIN_DIR__ . 'footer.php'; 
?>
