<?php
/**
 * 【Mirai未来主题是一款简约优雅的Typecho主题】
 * 【作者QQ】：1461139506
 * 【作者博客】：https://www.sukuy.com
 * 【PHP版本】：PHP 7.4+，建议PHP 8.3或8.4
 * 【Typecho版本】：1.3.0及以上
 * 【主要提示】：请勿使用盗版、破解主题，这类主题存在严重的未知安全风险！！！

 * @package Mirai未来主题
 * @author 苏酷伊Sukuy
 * @version 1.0.6
 * @link https://www.sukuy.com
 */
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

$this->need('header.php');
if (!$this->is('index')) {
    echo '<div class="gt-breadcrumb-wrap">';
    $this->need('modules/breadcrumb.php');
    echo '</div>';
}
?>
<div class="article-list-main mirai-ajax-scope mirai-ajax-archive-scope" data-mirai-ajax-type="archive" data-archive-scope="index">
    <?php 
    $showRecommend = $this->is('index') && $this->options->recommendEnable === '1';
    if ($showRecommend && $this->options->recommendFirstPageOnly === '1') {
        $showRecommend = ($this->request->get('page', 1) <= 1);
    }
    ?>
    <?php if ($showRecommend): ?>
        <?php $this->need('modules/recommend.php'); ?>
    <?php endif; ?>
    <div class="gt-article-tabs-header" id="article-tabs" data-page-size="<?php echo intval($this->options->pageSize ?: 5); ?>">
        <h2 class="gt-tab-btn active" data-tab="latest">最新发布</h2>
        <button class="gt-tab-btn" data-tab="views">最多浏览</button>
        <button class="gt-tab-btn" data-tab="likes">最多点赞</button>
        <button class="gt-tab-btn" data-tab="comments">最多评论</button>
    </div>
    <?php
    $gridLayoutValue = Mirai_getFeatureValue($this->options->gridLayout ? $this->options->gridLayout : '3', '3', 'grid_layout');
    $singleColumnEnabled = ($gridLayoutValue === '1');
    ?>
    <section id="article-list-container" class="article-list <?php echo $singleColumnEnabled ? 'single-column' : ''; ?>" data-grid="<?php echo $gridLayoutValue; ?>">
    <?php 
    if ($this->is('index')) {
        $page = $this->request->get('page', 1);
        if ($page < 1) $page = 1;
        
        $pageSize = $this->options->pageSize ? intval($this->options->pageSize) : 5;
        if ($pageSize < 1) $pageSize = 5;
        
        $posts = Mirai_getHomePosts($page, $pageSize);
    } else {
        $posts = null;
    }
    
    if ($this->is('index') && !empty($posts)): 
        $articleIndex = 0;
        foreach ($posts as $post):
            $articleIndex++;
            $isFirst = ($articleIndex === 1 && $page === 1);
            if (!isset($post['author']) && isset($post['screenName'])) {
                $post['author'] = $post['screenName'];
            }
            
            Mirai_renderPostItem($post, ['isWidget' => false, 'isFirst' => $isFirst, 'isIndex' => true]);
        endforeach;
    elseif ($this->have()): 
        $articleIndex = 0;
        while($this->next()): 
            $articleIndex++;
            $isFirst = ($articleIndex === 1 && !isset($_GET['page']));
            Mirai_renderPostItem($this, ['isWidget' => true, 'isFirst' => $isFirst]);
        endwhile;
    else: ?>
        <?php Mirai_renderEmptyStateWithRecommend(
            Mirai_getThemeUrl() . '/assets/images/no.svg',
            '',
            '暂无文章',
            $this
        ); ?>
    <?php endif; ?>
    </section>
    <?php 
    if ($this->is('index') && !empty($posts)): 
        try {
            $db = \Typecho\Db::get();
            $select = $db->select('COUNT(*)')->from('table.contents')
                ->where('table.contents.status = ?', 'publish')
                ->where('table.contents.type = ?', 'post')
                ->where('table.contents.created <= ?', time())
                ->where('table.contents.password IS NULL OR table.contents.password = ?', '');
            
            $excludedIds = Mirai_getRecommendExcludedIds();
            if (!empty($excludedIds)) {
                $select->where('table.contents.cid NOT IN ?', $excludedIds);
            }
            
            $totalPosts = $db->fetchObject($select)->{'COUNT(*)'};
            $totalPages = $pageSize > 0 ? ceil($totalPosts / $pageSize) : 1;
        } catch (Exception $e) {
            $totalPages = 1;
        }
        
        $that = $this;
        echo Mirai_customPagination($page, $totalPages, function($p) use ($that) {
            return Mirai_getPageUrl($that, $p);
        }, [
            'mode' => $this->options->archivePaginationMode === 'ajax' ? 'ajax' : 'default',
            'scopeType' => 'archive',
            'triggerMode' => $this->options->archiveAjaxPaginationTrigger ?? 'button'
        ]);
        
    elseif ($this->have()): 
        $current = $this->getCurrentPage();
        $total = ceil($this->getTotal() / $this->parameter->pageSize);
        
        $that = $this;
        echo Mirai_customPagination($current, $total, function($p) use ($that) {
            return Mirai_getPageUrl($that, $p);
        }, [
            'mode' => $this->options->archivePaginationMode === 'ajax' ? 'ajax' : 'default',
            'scopeType' => 'archive',
            'triggerMode' => $this->options->archiveAjaxPaginationTrigger ?? 'button'
        ]);
    endif; ?>
</div>
<?php if (function_exists('Mirai_isSkeletonEnabled') && Mirai_isSkeletonEnabled($this)): ?>
<template id="mirai-home-tab-skeleton-template"><?php echo Mirai_renderHomeTabSkeletonTemplate($this); ?></template>
<?php endif; ?>
<?php if ($this->is('index')): ?>
    <?php $this->need('modules/catrec.php'); ?>
    <?php Mirai_renderHomeLinks(); ?>
<?php endif; ?>
<?php $this->need('footer.php'); ?>