<template>
    <cwg-page-wrapper class="create-page" :isHeaderFixed="true">


        <view class="container">
            <view
                class="app-page-head card-header d-flex gap-3 flex-wrap align-items-center justify-content-between border-0">
                <view class="app-page-head mb-0">
                    <h1 class="app-page-title" v-t="'News.Announcement'"></h1>
                </view>
            </view>
            <view class="col-row">
                <view class="card">
                    <view class="card-header">
                        <view class="nav nav-underline card-header-tabs" id="myTab" role="tablist">
                            <view class="nav-item" v-for="item in tabsConfig" :key="item">
                                <view class="nav-link cursor-pointer" @click="activeTab = item.value"
                                    :class="{ 'active': item.value == activeTab }" :data-tooltip="item.text">{{
                                        item.text }}</view>
                            </view>
                        </view>
                    </view>
                    <view class="card-body">
                        <List ref="newsList" :fetchData="fetchNewsList" :queryParams="queryParams" :type="type" />
                    </view>
                </view>
            </view>
        </view>
    </cwg-page-wrapper>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted, onUnmounted, watch, nextTick } from 'vue'
import { onLoad, onReachBottom } from '@dcloudio/uni-app'
import { useI18n } from 'vue-i18n' // uni-app 中已集成，但需配置
import { newsApi } from '@/service/news'
import List from './components/List.vue'
const { t, locale } = useI18n()
const isZh = computed(() => ['cn', 'zhHant'].includes(locale.value))
const newsList = ref(null)
const fetchNewsList = ref(null)
const queryParams = ref({
    tag: 4,
    endDate: "",
    startDate: "",
    id: null
})
const activeTab = ref(1)
const tabsConfig = computed(() => [
    { text: t('News.Announcement1'), value: 1, type: 3 },
    { text: t('News.CompanyNews'), value: 2, type: 5 },
])


onReachBottom(() => {
    newsList.value?.loadMore()
})
// const lang = computed(() => uni.getLocale())
const type = ref(0)
// watch(lang, (val) => {
//     if (activeTab.value == 1) {
//         const langMap = {
//             'zh-Hans': 'cn',
//             'zh-Hant': 'zhHant',
//             'vn': 'vi'
//         }
//         queryParams.value = {
//             tag: null,
//             endDate: '',
//             startDate: '',
//             id: null,
//             pageSize: 18,
//             lang: langMap[val] || val
//         }
//     }
// })
watch(activeTab, (val) => {
    if (val == 1) {
        fetchNewsList.value = newsApi.newsInformationList
        queryParams.value = {
            tag: null,
            endDate: '',
            startDate: '',
            id: null,
            pageSize: 100
        }
    } else {
        fetchNewsList.value = newsApi.newsInformationNewsletterList
        queryParams.value = {
            tag: 4,
            endDate: '',
            startDate: '',
            id: null,
            pageSize: 100
        }
    }
    type.value = tabsConfig.value.find(item => item.value == val).type
    nextTick(() => {
        newsList.value.load()
    })
}, { immediate: true })
</script>
<style lang="scss" scoped>
@import "@/uni.scss";
</style>