<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit;
$breadcrumbEnabled = isset($this->options->breadcrumbEnable) && $this->options->breadcrumbEnable === '1';
if (!$breadcrumbEnabled) {
    return;
}
$breadcrumbFormat = isset($this->options->breadcrumbFormat) ? $this->options->breadcrumbFormat : 'full';
?>
<nav class="gt-breadcrumb" aria-label="Breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">
    <span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
        <a href="<?php $this->options->siteUrl(); ?>" itemprop="item" title="首页">
            <span itemprop="name">首页</span>
        </a>
        <meta itemprop="position" content="1" />
    </span>
    <span aria-hidden="true">&gt;</span>

    <?php if ($this->is('index')): ?>
        <span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
            <span class="current" itemprop="name" aria-current="page">文章列表</span>
            <meta itemprop="position" content="2" />
        </span>

    <?php elseif ($this->is('category')):
        $categoryId = Mirai_resolveCategoryId($this->mid ?? 0, $this->categories ?? [], $this->request);
        if ($categoryId > 0) {
            $parentList = Mirai_getBreadcrumbListRecursive($categoryId, 2, 0, false);
            $position = 2;
            ob_start();
            $this->archiveTitle('', '', '');
            $currentCatName = trim(ob_get_clean());
            if (!empty($parentList) && !empty($currentCatName)) {
                $lastParent = end($parentList);
                reset($parentList);
                if (isset($lastParent['name']) && $lastParent['name'] === $currentCatName) {
                    array_pop($parentList);
                }
            }
            foreach ($parentList as $item) {
                echo '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">';
                echo '<a href="' . $item['item'] . '" itemprop="item" title="' . htmlspecialchars($item['name']) . '">';
                echo '<span itemprop="name">' . htmlspecialchars($item['name']) . '</span></a>';
                echo '<meta itemprop="position" content="' . $position . '" />';
                echo '</span>';
                $position++;
            }
            if ($currentCatName !== '') {
                if (!empty($parentList)) {
                    echo '<span aria-hidden="true">&gt;</span>';
                }
                echo '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">';
                echo '<span class="current" itemprop="name" aria-current="page">' . htmlspecialchars($currentCatName) . '</span>';
                echo '<meta itemprop="position" content="' . $position . '" />';
                echo '</span>';
            }
        } else {
            ob_start();
            $this->archiveTitle('', '', '');
            $fallbackTitle = trim(ob_get_clean());
            echo '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">';
            echo '<span class="current" itemprop="name" aria-current="page">' . htmlspecialchars($fallbackTitle) . '</span>';
            echo '<meta itemprop="position" content="2" />';
            echo '</span>';
        }
    ?>

    <?php elseif ($this->is('tag')): ?>
        <?php ob_start(); $this->archiveTitle('', '', ''); $tagTitle = trim(ob_get_clean()); ?>
        <span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
            <span class="current" itemprop="name" aria-current="page">标签：<?php echo htmlspecialchars($tagTitle); ?></span>
            <meta itemprop="position" content="2" />
        </span>

    <?php elseif ($this->is('search')): ?>
        <?php ob_start(); $this->archiveTitle('', '', ''); $searchTitle = trim(ob_get_clean()); ?>
        <span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
            <span class="current" itemprop="name" aria-current="page">搜索：<?php echo htmlspecialchars($searchTitle); ?></span>
            <meta itemprop="position" content="2" />
        </span>

    <?php elseif ($this->is('post') || $this->is('page')):
        $position = 2;
        if ($this->is('post') && !empty($this->categories)):
            $list = Mirai_getBreadcrumbListRecursive($this->categories[0]['mid']);
            foreach ($list as $item) {
                echo '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">';
                echo '<a href="' . $item['item'] . '" itemprop="item" title="' . htmlspecialchars($item['name']) . '">';
                echo '<span itemprop="name">' . htmlspecialchars($item['name']) . '</span></a>';
                echo '<meta itemprop="position" content="' . $position . '" />';
                echo '</span>';
                echo '<span aria-hidden="true">&gt;</span>';
                $position++;
            }
        endif;
        ?>
        <span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
            <span class="current" itemprop="name" aria-current="page"><?php
                if ($breadcrumbFormat === 'full' || $this->is('page')) {
                    $this->title();
                } else {
                    echo '正文';
                }
            ?></span>
            <meta itemprop="position" content="<?php echo $position; ?>" />
        </span>

    <?php else: ?>
        <?php ob_start(); $this->archiveTitle('', '', ''); $otherTitle = trim(ob_get_clean()); ?>
        <span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
            <span class="current" itemprop="name" aria-current="page"><?php echo htmlspecialchars($otherTitle); ?></span>
            <meta itemprop="position" content="2" />
        </span>
    <?php endif; ?>
</nav>
