<template>
    <!-- 全局page 带tab切换和左右滑动加载-->
    <z-paging-swiper :swiper-style="getSwiperStyle">
        <!-- 头部 -->
        <template #top>
            <view :style="{ height: safeAreaInsets?.top + 'px' }" v-if="custom"></view>
            <!-- 自定义导航栏 -->
            <global-navbar :title="title" v-if="showNavbar">
                <template #left>
                    <up-icon name="arrow-left" size="16" @tap="goBack"></up-icon>
                </template>
            </global-navbar>
            <!-- tab切换 -->
            <global-tabs
                :current="current"
                :lineWidth="40"
                :list="tabList"
                :keyName="nameKey"
                @handleChange="tabsChange"
                inactiveStyle="color:#86909C"
                :width="250"
                :height="100"
            ></global-tabs>
            <slot name="top"></slot>
        </template>
        <!-- 滚动内容 -->
        <swiper
            :duration="100"
            class="swiper"
            :style="getStyle"
            :current="current"
            @transition="swiperTransition"
            @animationfinish="swiperAnimationfinish"
        >
            <swiper-item v-for="(_, index) in tabList" :key="index">
                <global-page-swiper-item
                    :ref="(vc) => (pagingArr[index] = vc)"
                    :auto="auto"
                    :tabValue="tabList[current][valueKey]"
                    :tabIndex="index"
                    :tabValueField="tabValueField"
                    :currentIndex="current"
                    :api="api"
                    :refresher-enabled="refresherEnabled"
                    :refresher-threshold="refresherThreshold"
                    :auto-show-back-to-top="autoShowBackToTop"
                    :loading-more-enabled="loadingMoreEnabled"
                    :back-to-top-bottom="backToTopBottom"
                >
                    <template #="{ dataList }">
                        <slot :dataList="dataList"></slot>
                    </template>
                </global-page-swiper-item>
            </swiper-item>
        </swiper>
        <!-- 消息提醒 -->
        <up-toast ref="uToastRef"></up-toast>
    </z-paging-swiper>
</template>

<script setup>
import { computed, nextTick, onUnmounted, ref } from 'vue'

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
const ps = defineProps({
    background: {
        type: String,
        default: '#f7f8fa'
    },
    padding: {
        type: [String, Number],
        default: 0
    },
    padding: {
        type: [String, Number],
        default: 0
    },
    margin: {
        type: [String, Number],
        default: 0
    },
    //是否自动加载
    auto: {
        type: Boolean,
        default: true
    },
    //tab数据
    tabList: {
        type: Array,
        default: () => []
    },
    //tab默认index
    current: {
        type: Number,
        default: 0
    },
    //tab数据搜索字段
    tabValueField: {
        type: String,
        default: 'status'
    },
    //tab数据name
    nameKey: {
        type: String,
        default: 'name'
    },
    //tab数据value
    valueKey: {
        type: String,
        default: 'value'
    },
    //是否展示自定义导航栏
    showNavbar: {
        type: Boolean,
        default: true
    },
    //页面标题
    title: {
        type: String,
        default: ''
    },
    //是否关闭原生导航栏
    custom: {
        type: Boolean,
        default: true
    },
    //查询接口
    api: {
        type: Function,
        default: null
    },
    //是否开启下拉刷新
    refresherEnabled: {
        type: Boolean,
        default: false
    },
    refresherThreshold: {
        type: [String, Boolean],
        default: '150rpx'
    },
    //是否启用加载更多数据
    loadingMoreEnabled: {
        type: Boolean,
        default: false
    },
    autoShowBackToTop: {
        type: Boolean,
        default: true
    },
    backToTopBottom: {
        type: [Number, String],
        default: '160rpx'
    }
})
// z-swiper样式
const getSwiperStyle = computed(() => {
    return {
        fontSize: '28rpx',
        background: ps.background
    }
})
//滚动区域样式
const getStyle = computed(() => {
    let padding = typeof ps.padding == 'number' ? ps.padding + 'rpx' : ps.padding
    let margin = typeof ps.margin == 'number' ? ps.margin + 'rpx' : ps.margin
    let style = {
        margin,
        padding
    }
    return style
})
const current = ref(ps.current)
//返回
const goBack = () => {
    uni.$mocpJump.navigateBack()
}
// tabs通知swiper切换
const tabsChange = (item) => {
    current.value = item.index
}
// swiper滑动中
const swiperTransition = (e) => {}
// swiper滑动结束
const swiperAnimationfinish = (e) => {
    current.value = e.detail.current
}
//消息提示
const uToastRef = ref()
const openToast = (params) => {
    nextTick(() => {
        uToastRef.value?.show(params)
    })
}
uni.$on('useMessage', (params) => openToast(params))
onUnmounted(() => {
    uni.$off('useMessage', openToast)
})
//获取列表刷新对象
const pagingArr = ref([])
defineExpose({
    reload: () => {
        pagingArr.value[current.value]?.reload()
    },
    updateCache: () => {
        pagingArr.value[current.value]?.updateCache()
    }
})
</script>
<style scoped>
.swiper {
    height: 100%;
}
</style>
