<template>
    <view class="app-custom-root">
        <view class="app-container">
            <!-- 主体内容区域 -->
            <view class="container-main">
                <swiper
                    class="swiper"
                    :disable-touch="disableTouch"
                    :current="state.current"
                    @change="changeSwiperType"
                >
                    <!-- 绑定首页和尾页 -->
                    <swiper-item
                        @touchstart.passive="handleTouchStart"
                        @touchmove.passive="handleTouchMove"
                        @touchend="disableTouch = false"
                        class="swiper-item"
                        v-for="(_, index) in 2"
                        :key="index"
                    >
                         <component
                            :is="components[index]"
                            @onShowTabbar="changeShowTabbar"
                            :ref="(el) => handelComponentsRef(el, index)"
                        />
                    </swiper-item>
                </swiper>
            </view>

            <Tabbar v-show="showTabbar" :currentpage="0"></Tabbar>

            <!-- maskFristEntry -->
            <view class="maskFristEntry" v-if="maskFristEntry">
                <view class="entry-content">
                    <text class="text1">左滑查看视频</text>
                    <text class="text2">快去体验吧～</text>
                    <view class="goExperience" @click="goExperience">去体验</view>
                    <view class="maskFristEntry-Close" @click="closeFristEntry"></view>
                </view>
            </view>
        </view>
    </view>
</template>

<script setup>
import { reactive, inject, watch, ref, onMounted } from 'vue';
import Tabbar from '@/components/tabbar/midell-box.vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import IndexRefactor from './components/index-refactor.vue';
import IndexTwo from './components/index-two.vue';
import useScreenStore from '@/stores/useScreenStore';

const screenStore = useScreenStore();
const { isWideScreen } = screenStore;

const loadedMap = reactive([false, false]);
const swiperRefs = [ref(null), ref(null)];
const components = [IndexRefactor, IndexTwo];
import { storeToRefs } from 'pinia';
import { useReadMsg } from '@/stores/useReadMsg';
const { unreadCount } = storeToRefs(useReadMsg());
const showTabbar = ref(false);
const maskFristEntry = ref(false);

const disableTouch = ref(false);
const startPointX = ref(0);
const totalPage = 2;
const THRESHOLD = 5;

watch(
    () => screenStore.isWideScreen,
    (newVal) => {
        showTabbar.value = newVal;
    },
    { immediate: true }
);

onLoad(() => {
    // 判断浏览器是否有 fristEntry 第一次进入
    let fristEntry = uni.getStorageSync('fristEntry') === false ? false : true; // 默认未读
    maskFristEntry.value = fristEntry;
    if (fristEntry) {
        if (!isWideScreen) uni.hideTabBar();
        else showTabbar.value = false;
    } else {
        if (isWideScreen) showTabbar.value = true;
    }
    // 预加载较重页面
    setTimeout(() => {
        uni.preloadPage({ url: '/packageA/pages/post/post' });
        uni.preloadPage({ url: '/pages/nearby/nearby' });
        uni.preloadPage({ url: '/pages/chat/chat' });
        uni.preloadPage({ url: '/packageA/pages/choiceness/choiceness' });
        uni.preloadPage({ url: '/packageA/pages/reservation/reservation' });
        uni.preloadPage({ url: '/packageA/pages/Intendedposition/Intendedposition' });
    }, 1000);
});

onShow(() => {
    // 获取消息列表
    // useReadMsg().fetchMessages();
});

const state = reactive({
    current: 0,
    all: [{}],
});

onMounted(() => {
    handleTabChange(state.current);
});

const handelComponentsRef = (el, index) => {
    if (el) {
        swiperRefs[index].value = el;
    }
};

function handleTouchStart(e) {
    startPointX.value = e.touches[0].clientX;
    disableTouch.value = false;
}
function handleTouchMove(e) {
    const currentX = e.touches[0].clientX;
    const diffX = currentX - startPointX.value;

    if (state.current === 0) {
        if (diffX > THRESHOLD) {
            disableTouch.value = true;
        } else {
            disableTouch.value = false;
        }
        return;
    }
    if (state.current === totalPage - 1) {
        if (diffX < -THRESHOLD) {
            disableTouch.value = true;
        } else {
            disableTouch.value = false;
        }
        return;
    }

    disableTouch.value = false;
}

function changeShowTabbar(val) {
    if (isWideScreen) {
        showTabbar.value = val;
    } else {
        if (val) uni.showTabBar();
        else uni.hideTabBar();
    }
}

//1  查看消息类型
function changeSwiperType(e) {
    const newIndex = e.detail.current;
    const lastIndex = state.current;

    const isSwipingRight = newIndex < lastIndex;
    const isSwipingLeft = newIndex > lastIndex;

    if (lastIndex === 0 && isSwipingRight) {
        disableTouch.value = true;
        state.current = 0;
        setTimeout(() => {
            disableTouch.value = false;
        }, 50);
        return;
    }

    if (lastIndex === totalPage - 1 && isSwipingLeft) {
        disableTouch.value = true;
        state.current = lastIndex;

        setTimeout(() => {
            disableTouch.value = false;
        }, 50);
        return;
    }

    const index = e.detail.current;
    state.current = index;
    handleTabChange(index);
    disableTouch.value = false;
}
function changeType(index) {
    state.current = index;
    handleTabChange(index);
}

function handleTabChange(index) {
    if (!loadedMap[index]) {
        swiperRefs[index].value?.loadData();
        loadedMap[index] = true;
    }
}

function changeSwiperMsgType(e) {
    const currented = e.detail.current;
    state.current = currented;
}
// mask
function closeFristEntry() {
    uni.setStorageSync('fristEntry', false);
    maskFristEntry.value = false;
    if (!isWideScreen) uni.showTabBar();
    else showTabbar.value = true;
}

function goExperience() {
    closeFristEntry();
    if (!isWideScreen) uni.showTabBar();
    else showTabbar.value = true;
    state.current = 1;
}
</script>

<style lang="stylus" scoped>
.app-custom-root {
    position: fixed;
    z-index: 10;
    width: 100vw;
    height: calc(100% - var(--window-bottom));
    overflow: hidden;
}
.app-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    .container-header {
        height: calc(88rpx - 14rpx);
        text-align: center;
        line-height: calc(88rpx - 14rpx);
        font-size: 32rpx;
        display: flex;
        flex-direction: row;
        align-items: center;
        padding: 16rpx 44rpx 36rpx 44rpx;
        background: url('@/static/icon/msgTopbg.png') 0 0 no-repeat;
        background-size: 100% 100%;
        .header-title {
            color: #000000;
            font-weight: bold;
        }
        .header-btnLf {
            display: flex;
            justify-content: flex-start;
            align-items: center;
            width: calc(60rpx * 3);
            font-weight: 500;
            font-size: 40rpx;
            color: #696969;
            margin-right: 44rpx;
            position: relative;
            .btns-wd{
                position: absolute
                top: 2rpx;
                right: 2rpx
                width: 16rpx;
                height: 16rpx;
                background: #F73636;
                border-radius: 50%;
                border: 4rpx solid #EEEEFF;
            }
        }
        .active {
            font-weight: 600;
            font-size: 40rpx;
            color: #000000;
        }
    }
}

.container-main {
    flex: 1;
    overflow: hidden;
    background-color: #f4f4f4;
}
.main-scroll {
    width: 100%
    height: 100%;
}
.scrollmain{
    padding: 28rpx
}
.swiper
    height: 100%;
    width: 100%
    .list
        width: 100%
        display: flex;
        flex-direction: column;

// mask:
.maskFristEntry
    position: fixed;
    // right: 20rpx;
    // bottom: calc(50% - 200rpx);
    height: 100vh
    width: 100vw
    background: rgba(0,0,0,0.3)
    .entry-content
        display: flex;
        align-items: center
        position: absolute
        left: 50%
        top: 35%
        transform: translate(-50%, -50%)
        flex-direction: column
        background: url('@/static/imgs/fristEntry.png') 0 0 no-repeat;
        background-size: 100% 100%;
        width: 480rpx
        height: 584rpx
        // padding-left: 80rpx
        .text1
            font-family: 'PingFangSC-Medium', 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, 'Microsoft YaHei', sans-serif;
            margin-top: 370rpx
            font-size: 36rpx
            background: linear-gradient(273.34deg, #356CFA 3.58%, #A47FFD 85.84%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text; /* 有些浏览器兼容用 */
            text-fill-color: transparent;
            padding-left: 28rpx
        .text2
            padding-left: 28rpx
            margin-top: 8rpx
            font-size: 20rpx;
            color: #666666;
            text-align: center;
        .indicateArrow
            height: 76rpx
            width: 68rpx
        .indicatefristEntry
            width: 244rpx
            height: 244rpx
        .goExperience
            font-family: 'PingFangSC-Medium', 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, 'Microsoft YaHei', sans-serif;
            margin-left: 28rpx
            margin-top: 28rpx
            width: 160rpx;
            height: 60rpx;
            background: linear-gradient( 180deg, #9974FD 0%, #286BFA 100%);
            border-radius: 12rpx 12rpx 12rpx 12rpx;
            font-size: 28rpx;
            color: #FFFFFF;
            text-align: center;
            line-height: 60rpx
        .maskFristEntry-Close
            position: absolute;
            left: calc(50% - 10rpx);
            bottom: -130rpx
            width: 42rpx
            height: 42rpx
            background: linear-gradient(273.34deg, #356CFA 3.58%, #A47FFD 85.84%);
            border-radius: 50%;
        .maskFristEntry-Close::before
            position: absolute;
            left: calc( 50% - 2rpx)
            top: calc( 50% - 10rpx)
            transform: rotate(45deg);
            content: ''
            background: #FFFFFF
            width: 4rpx
            height: 20rpx
        .maskFristEntry-Close::after
            position: absolute;
            left: calc( 50% - 2rpx)
            top: calc( 50% - 10rpx)
            transform: rotate(-45deg);
            content: ''
            background: #FFFFFF
            width: 4rpx
            height: 20rpx
</style>