<template>
    <u-popup v-model="showNotice" mode="center" border-radius="24" zIndex="10">
        <view class="w-[90vw] px-[24rpx]">
            <view class="p-[30rpx] text-lg text-center font-medium">
                {{ richTextTitle }}
            </view>
            <scroll-view class="max-h-[768rpx]" scroll-y>
                <mp-html :content="richTextContent"></mp-html>
            </scroll-view>
            <view class="py-[30rpx] bg-white">
                <button
                    :style="{
                        height: '82rpx',
                        lineHeight: '82rpx',
                        fontSize: '30rpx',
                        border: 'none',
                        borderRadius: '60px',
                        background: 'var(--color-primary)'
                    }"
                    @click="showNotice = false"
                >
                    我知道了
                </button>
            </view>
        </view>
    </u-popup>
</template>

<script lang="ts" setup>
import { ref, computed, watch } from 'vue'
import { useAppStore } from '@/stores/app'
import cache from '@/utils/cache'
import { NOTICE } from '@/enums/constantEnums'

const appStore = useAppStore()
const showNotice = ref<boolean>(false)
const richTextTitle = computed(
    () => appStore.getBulletinConfig.title
)
const richTextContent = computed(
    () => appStore.getBulletinConfig.content
)
const isBulletin = computed(
    () => appStore.getBulletinConfig.status == 1
)

const shouldShowNotice = (value: boolean) => {
    const lastVisitTime = cache.get(NOTICE)
    const currentTime = new Date().toDateString()
    const isNewDay = !lastVisitTime || lastVisitTime !== currentTime
    if (isNewDay && value) {
        cache.set(NOTICE, currentTime)
    }
    return isNewDay
}

watch(
    () => [isBulletin.value],
    () => {
        if (
            isBulletin.value &&
            shouldShowNotice(isBulletin.value)
        ) {
            showNotice.value = true
        }
    },
    {
        deep: true,
        immediate: true
    }
)
</script>
