<template>
    <ul>
        <template
            v-for="(item, index) in postsList"
            :key="index + sortPostsByDateSymbol.length"
        >
            <li
                v-if="(year = getYear(index)) !== getYear(index - 1)"
                class="year"
            >
                <h2> <img :src="getBadge()"> {{ year }} <span> <i>{{ countByYear[year] }}</i> 篇 </span> </h2>
            </li>
            <li>
                <router-link :to="item.path">
                    <span class="date">{{ getDate(item) }}</span>
                    {{ item.title }}
                    <!-- 标题标签 -->
                    <span
                        v-if="item.frontmatter.titleTag"
                        class="title-tag"
                    >
                        {{ item.frontmatter.titleTag }}
                    </span>
                </router-link>
            </li>
        </template>
    </ul>
</template>
<script>
import { useEventListener, useDebounceFn } from '@vueuse/core'
import { reactive, ref, onMounted, inject, watch } from 'vue'
import { useRoute } from 'vue-router'
import { typeOf } from '../utils'
import { useThemeData } from '../composables/index.js'
export default {
    setup(props) {
        let postsList = ref([]) // 当前页面的文章数据
        let countByYear = reactive({}) // 根据年份统计的文章数
        let perPage = ref(80) // 每页长度
        let currentPage = ref(1) // 当前页面
        const sortPostsByDateSymbol = inject('sortPostsByDateSymbol').value
        const themeData = useThemeData().value
        const route = useRoute()

        getPageData()

        for (let i = 0; i < sortPostsByDateSymbol.length; i++) {
            const {
                frontmatter: { date }
            } = sortPostsByDateSymbol[i]
            if (date && typeOf(date) === 'string') {
                const year = date.slice(0, 4)
                if (!countByYear[year]) {
                    countByYear[year] = 0
                }
                countByYear[year] = countByYear[year] + 1
            }
        }
        countByYear = countByYear

        onMounted(() => {
            useEventListener(
                'scroll',
                useDebounceFn(() => {
                    if (postsList.value.length < sortPostsByDateSymbol.length) {
                        const docEl = document.documentElement
                        const docBody = document.body
                        const scrollTop = docEl.scrollTop || docBody.scrollTop
                        const clientHeight = docEl.clientHeight || docBody.clientHeight
                        const scrollHeight = docEl.scrollHeight || docBody.scrollHeight

                        if (scrollHeight > clientHeight && scrollTop + clientHeight >= scrollHeight - 250) {
                            loadmore()
                        }
                    }
                }, 100)
            )
        })

        function getPageData() {
            postsList.value = postsList.value.concat(sortPostsByDateSymbol.slice((currentPage.value - 1) * perPage.value, currentPage.value * perPage.value))
        }

        function loadmore() {
            currentPage.value += 1
            getPageData()
        }
        function getYear(index) {
            const item = postsList.value[index]
            if (!item) return
            const { frontmatter: { date } } = item
            if (date && typeOf(date) === 'string') {
                return date.slice(0, 4)
            }
        }

        function getDate(item) {
            const {
                frontmatter: { date }
            } = item
            if (date && typeOf(date) === 'string') {
                return date.slice(5, 10)
            }
        }
        // 标题图标
        let badges = ref([
            'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAAAXNSR0IArs4c6QAABGpJREFUSA3tVVtoXFUU3fvOI53UlmCaKIFmwEhsE7QK0ipFEdHEKpXaZGrp15SINsXUWvBDpBgQRKi0+KKoFeJHfZA+ED9KKoIU2gYD9UejTW4rVIzm0VSTziPzuNu1z507dibTTjL4U/DAzLn3nL3X2o91ziX6f9wMFdh6Jvbm9nNSV0msViVO6tN1Rm7NMu2OpeJ9lWBUTDxrJbYTS0hInuwciu9eLHlFxCLCZEk3MegsJmZ5K/JD6t7FkFdEvGUo1g7qJoG3MHImqRIn8/nzY1K9UPKKiJmtnUqHVE3Gbuay6vJE/N2FEmuxFjW2nUuE0yQXRRxLiTUAzs36zhZvOXJPdX850EVnnLZkB8prodQoM5JGj7Xk2mvC7JB8tG04Ef5PiXtG0UtxupRQSfTnBoCy554x18yJHI6I+G5Eru4LHmPJZEQsrvPUbMiA8G/WgMK7w7I+ez7++o2ANfbrjvaOl1tFMs+htG3IrZH9/hDX1Pr8Tc0UvH8tcX29KzAgIGcEkINyW5BF9x891hw6VYqgJHEk0huccS7vh3C6gTiODL+26huuBtbct8eZnqLML8PkxGYpuPZBqtqwkSjgc4mB5gbgig5i+y0UDK35LMxXisn9xQtK+nd26gTIHsHe/oblK/b29fUmN/8Y+9jAQrnBp56m1LcDlDp9irKTExSKduXJVWSqdBMA08pEJnEIOB3FPPMybu/oeV8zFeYN3xx576Q6RH+Vm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P34wlR2FZVQWU1q0Cw8Tr7p+hgLLNL0FPxx/Q35mA8aEUrH6nCgwEl0tn7wUiZYJnNRh6DK4UH/k0lfyrsBKdPVv/AriGIQcEDQZ65LBAGe2Rzui9Ybjz7XUppz1/uKBbyVPGkN3ZAeC6hr0x7Nr38N5+EqkoOm17xpoqR9ohQF55ERSvr4Dkr3chNfC3DMzGJlNBElW8w9nsGQvhNGIzDkXzCg8cLK951xHsFBlTJspJNi3ZFIMF2AeDV3q8DNOB+YHi6QTrChDIWDBRi5U5f+ZMfJLu3ccrqxtdxk4SKH336LFxSmkqefwU5T8fhdSdQf9IVKD6aNiwI/hnmcAZ91isYMJIaCUCx9W098+LgruikeTqzqqxKPUwqJyCPJiyemVVZBOijDGjD38Os0jOiSPL1z3SPjXNANbiNPXAdzTfukjjuknNBbyz3nwgTd3AVFqUJ5hpHlq9MveLnWwttUfoygBmvVjuikxND3znrhsELnZk7k+OjIGxeNEkomyLVta0xxn+HZhjBc4YZ/AFjHjz9u3xRZl2BN4aq9nFwWh16IrQ1aHHEd3j1+4/dB9OtH4e29A2H1DyHQRmOSfQZ1Fy7MHBTGB6J/Djq6p3OxyO2cB+4Car7v/o3GXgfAkj23+x9ID1Teoamo/SXcbvSf2PX7Vc8DdCmE1vN9di+32P9/5YR3vLnhCVGUWBjEkr3yh4H8v9CzmsbdhzOKzsJKM90iFdaTMjRPhGVsakRvOaRidljo6H6G7j+ctrJpsP+4COhDIl0La2+FS4+5mlocBaXY5QnGZysIBYoeSsl5qQzrSj/cgNrfuEzlWBfwA+EjrZyWUvpAAAAABJRU5ErkJggg=='
        ])
        let currentBadge = ref('')
        currentBadge.value = getBadge()
        watch(() => route.path, () => {
            currentBadge.value = getBadge()
        }
        )
        function getBadge() {
            return badges.value[Math.floor(Math.random() * badges.value.length)]
        }

        return {
            postsList,
            countByYear,
            getYear,
            getDate,
            getBadge,
            sortPostsByDateSymbol
        }
    }
}
</script>
<style lang="scss" scoped>
@import '../styles/_variables';

li {
    list-style: none;
    &.year {
        position: sticky;
        top: var(--navbar-height);
        background: var(--c-bg);
        z-index: 1;
    }
    &.year:not(:first-child) {
        margin-top: 3.5rem;
    }
    h2 {
        margin-bottom: 0.8rem;
        font-weight: 400;
        padding: 0.5rem 0;
        span {
            font-size: 0.85rem;
            font-weight: 300;
            float: right;
            margin-top: 1rem;
        }
    }
    a {
        display: block;
        color: var(--c-text);
        transition: padding 0.3s;
        padding: 0.5rem 0;
        line-height: 1.2rem;
        &:hover {
            padding-left: 1.2rem;
            color: var(--c-brand);
            background: #f9f9f9;
        }
        span.date {
            opacity: 0.6;
            font-size: 0.85rem;
            font-weight: 400;
            margin-right: 0.3rem;
        }
        .title-tag {
            border: 1px solid var(--c-brand-light);
            color: var(--c-brand-light);
            font-size: 0.8rem;
            padding: 0 0.35rem;
            border-radius: 0.2rem;
            margin-left: 0.3rem;
            transform: translate(0, -0.05rem);
            display: inline-block;
        }
    }
}
.loadmore {
    text-align: center;
    margin-top: 1rem;
    opacity: 0.5;
}
</style>
