<template>
    <div class="content-loading-overlay" v-if="isLoading">
        <div class="loading-content">
            <div ref="animationContainer" class="animation-container"></div>
        </div>
    </div>
    <div v-else>
        <slot></slot>
    </div>
</template>

<script>
import lottie from 'lottie-web'
import { mapState } from 'vuex'
import defaultAnimation from '@/assets/loadingAni.json' // 导入JSON文件

export default {
    name: 'Loading',
    data() {
        return {
            anim: null
        }
    },
    computed: {
        ...mapState(['isLoading', 'loadingText'])
    },
    watch: {
        isLoading(newVal) {
            if (newVal) {
                this.$nextTick(()=>{
                    this.playAnimation()
                })
            } else {
                this.stopAnimation()
            }
        }
    },
    mounted() {
        if (this.isLoading) {
            this.playAnimation()
        }
    },
    beforeDestroy() {
        this.stopAnimation()
    },
    methods: {
        playAnimation() {
            this.anim = lottie.loadAnimation({
                container: this.$refs.animationContainer,
                renderer: 'svg',
                loop: true,
                autoplay: true,
                animationData: defaultAnimation // 你的JSON动画路径
            })
            if (this.anim) {
                this.anim.play()
            }
        },
        stopAnimation() {
            if (this.anim) {
                this.anim.stop()
            }
        }
    }
}
</script>

<style scoped>
.content-loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
    z-index: 1000;
    /* 确保高于内容 */
    display: flex;
    justify-content: center;
    align-items: center;
}

.loading-content {
    text-align: center;
}

.animation-container {
    width: 180px;
    height: 180px;
    margin: 0 auto;
}

.loading-text {
    margin-top: 20px;
    font-size: 16px;
    color: #333;
}
</style>