<template>
    <view class="videoPlayer">
        <video :id="videoId" ref="myVideo" class="video" height="100%" autoplay :loop="true" :src="videoUrl" :show-center-play-btn="false" :enable-play-gesture="true" :controls="controls" :vslide-gesture="true" :show-play-btn="false" :show-fullscreen-btn="false" @controlstoggle="controlstoggle($event)" @timeupdate="updateProgress" @play="play" @pause="pause" @fullscreenchange="onFullscreenChange" @click="togglePlay"></video>
        <view v-if="isPlay" class="play" @click="togglePlay"><image :src="imgsrc"></image></view>
    </view>
</template>

<script>
	import defaultConfig from '@/config/default.js'
export default {
    props:{
        videoUrl: String,
        videoId: String
    },
    data(){
        return{
            progress: 0, // 进度条进度
            isPlaying:null,
            isPlay:null,
            controls: true,
            isFullscreen: null,
            imgsrc:defaultConfig.ossImgUrl + '/playVideo/video/ic_play.png',
        }
    },
    onReady: function(res){
        console.log(res,8989);
    },
    watch:{
        isPlaying(newVal,oldVal){
            if(newVal){
                this.isPlay = true
            }else{
                this.isPlay = false
            }
        }
    },
    methods: {
        // 播放 &暂停
        togglePlay() {
            
            var video = this.$refs.myVideo;
            
            if (video.playing) {
                this.isPlaying = true
                video.pause();
            } else {
                this.isPlaying = false
                video.play();
            }

        },
        controlstoggle(event){
            console.log(event,'event');
            this.controls = !this.controls
        },
        updateProgress() {  
            // const video = this.$refs.video;  
            // const duration = video.duration;  
            // const currentTime = video.currentTime;  
            // this.progress = (currentTime / duration) * 100;  
        },  
        // 当开始/继续播放时触发play事件
        play() {  
            // this.isPlaying = false;
            // uni.createVideoContext('myVideo',this).play()
            // console.log(uni.createVideoContext('videoId').pageVm,'pla2y');
            // this.$refs.progressBar.style.display = 'block';  2
        },  
        // 当暂停播放时触发 pause 事件
        pause() {  
            // this.isPlaying = true;
            // uni.createVideoContext('myVideo',this).pause()
            // console.log(uni.createVideoContext('videoId').pageVm,'pease');
            // const video = this.$refs.video;  
            // if (video.currentTime === video.duration) {  
            //     this.$refs.progressBar.style.display = 'none';  
            // }  
        },  
        onFullscreenChange(event){
            this.isFullscreen = event.detail.fullScreen;
            console.log(event,this.isFullscreen,'ff你看');
        }
    },
    mounted(){
        setTimeout(() => {
            console.log(uni.createVideoContext('myVideo'),'video');
        })
        this.isFullscreen = uni.createVideoContext('myVideo')
        
        // this.$refs.video.addEventListener('loadedmetadata', () => {
        //     this.updateProgress(); // 初始化时设置进度为0%  
        // });  
    }
}
</script>

<style lang="scss">
    .videoPlayer{
        width: 100%;
        height: 100vh;
        position: relative;
        .video{
            width: 100%;
            height: 100%;
        }
        video::-webkit-media-controls-timeline{
            display: none;
        }
        .uni-video-bar .uni-video-bar-full{
            margin-bottom: calc(100rpx + constant(safe-area-inset-bottom)) !important;
            margin-bottom: calc(100rpx + env(safe-area-inset-bottom)) !important;
        }
        .play{
            width: 204rpx;
            height: 204rpx;
            position: absolute;
            left: calc(50% - 102rpx);
            bottom: 204rpx;
            image{
                width: 100%;
                height: 100%;
            }
        }
    }
    
</style>