<template>
    <div class="index">
        <div class="flex">
            <div class="w-[750px] h-[340px] flex-none mr-5">
                <ElCarousel
                    v-if="getSwiperData.enabled"
                    class="w-full"
                    trigger="click"
                    height="340px"
                >
                    <ElCarouselItem v-for="item in showList" :key="item">
                        <NuxtLink :to="item.link.path" target="_blank">
                            <ElImage
                                class="w-full h-full rounded-[8px] bg-white overflow-hidden"
                                :src="appStore.getImageUrl(item.image)"
                                fit="contain"
                            />
                        </NuxtLink>
                    </ElCarouselItem>
                </ElCarousel>
            </div>
            <InformationCard
                link="/information/new"
                class="flex-1 min-w-0"
                header="最新资讯"
                :data="pageData.new"
                :show-time="false"
            />
        </div>
        <div class="mt-5 flex">
            <InformationCard
                link="/information"
                class="w-[750px] flex-none mr-5"
                header="全部资讯"
                :data="pageData.all"
                :only-title="false"
            />
            <InformationCard
                link="/information/hot"
                class="flex-1"
                header="热门资讯"
                :data="pageData.hot"
                :only-title="false"
                image-size="mini"
                :show-author="false"
                :show-desc="false"
                :show-click="false"
                :border="false"
                :title-line="2"
            />
        </div>
    </div>
</template>
<script lang="ts" setup>
import { ElCarousel, ElCarouselItem, ElImage, ElMessage } from 'element-plus'
import { getIndex } from '@/api/shop'
import { useAppStore } from '~~/stores/app'
import { resolveShareCode } from '@/api/share'
const appStore = useAppStore()
const router = useRouter()
const route = useRoute()
const { data: pageData } = await useAsyncData(() => getIndex(), {
    default: () => ({
        all: [],
        hot: [],
        new: [],
        page: {}
    })
})

const getSwiperData = computed(() => {
    try {
        const data = JSON.parse(pageData.value.page.data)
        console.log(data)
        return data.find((item) => item.name === 'pc-banner')?.content
    } catch (error) {
        return {}
    }
})
const showList = computed(() => {
    return (
        getSwiperData.value?.data ||
        []
    )
})

onMounted(async () => {
    const slc = route.query.slc as string
    if (!slc) return

    try {
        const fingerprint = `${Date.now()}${Math.random().toString(36).slice(2, 8)}`
        const result = await resolveShareCode({
            code: slc,
            fingerprint,
            referer: window.location.href
        })

        if (result.params?.invite_code) {
            localStorage.setItem('share_invite_code', result.params.invite_code)
        }

        if (result.page_type === 'news' && result.page_id) {
            router.push(`/information/detail/${result.page_id}`)
        } else {
            ElMessage.success('分享页面已加载')
        }
    } catch (e) {
        console.error('解析分享码失败', e)
    }
})

</script>
<style lang="scss" scoped></style>
