<template>
  <view class="page">
		<navbar leftClick @leftClick="$utils.navigateBack" />

    <view class="content">
      <video class="video"
        id="video"
        :src="detail.vio" 
        autoplay
        play-btn-position="center"
        :controls="!timeIsUp"
        :show-fullscreen-btn="false"
        :show-center-play-btn="true"
        @timeupdate="onTimeupdate"
      ></video>
      <view class="info">
        <view class="author">{{ detail.author || '' }}</view>
        <view class="title">{{ detail.headTitle || '' }}</view>
        <view class="desc">{{ detail.textDetails || '' }}</view>
      </view>
    </view>

    <uv-overlay :show="timeIsUp" @click="onPlay" zIndex="998">
      <popupUnlock ref="popupUnlock" src="../static/sharing/unlock-video.png"></popupUnlock>
      <popupQrCode ref="popupQrCode" :src="detail.wxCodeImage"></popupQrCode>
      <loginPopup ref="loginPopup" @login="initData"/>
    </uv-overlay>
  </view>
</template>

<script>
  import { mapState } from 'vuex'
  import loginPopup from '@/components/config/loginPopup.vue'
  import popupUnlock from '../components/popupUnlock.vue'
  import popupQrCode from '../components/popupQrCode.vue'

	export default {
    components: {
      popupUnlock,
      popupQrCode,
      loginPopup,
    },
		data() {
			return {
        id: null,
        detail: {
          id: null,
          headTitle: null,
          indexImage: null,
          vio: null,
          timeNum: 0,
          num: 0,
          wxCodeImage: null,
          textDetails: null,
        },
        timeIsUp: false,
        isLocked: true,
        videoContext: null
      }
    },
		computed: {
			...mapState(['userInfo']),
		},
    onShow() {
      if (this.id && uni.getStorageSync('token')) {
        this.initData()
      }
    },
    async onLoad(option) {
      const { id, state, shareId } = option

			if (shareId) {
				uni.setStorageSync('shareId', shareId)
			}
      
			if (state) {
				uni.setStorageSync('state', state)
			}
      
			if (id) {
				uni.setStorageSync('id', id)
			}

      this.id = id

      if(uni.getStorageSync('token')){
        this.initData()
      }else{
        this.$refs.loginPopup.open()
      }
      
    },
    onShareAppMessage(res) {
      const {
        headTitle,
        indexImage,
      } = this.detail

      let o = {
        title : headTitle,
        imageUrl: indexImage,
        query: `id=${this.id}&state=1&shareId=${this.userInfo.id}`,
      }

      this.refreshLockStatus()

      return o
    },
    methods: {
      async fetchDetails(id) {
        try {
          this.detail = await this.$fetch('getVideoShareInfo', { id })
        } catch (err) {
          
        }
      },
      async initData() {
        await this.fetchDetails(this.id)

        this.videoContext = uni.createVideoContext('video');
      },
      async fetchCheckShare() {
        try {
          return await this.$fetch('checkVideoShare', { id: this.id })
        } catch (err) {
          return {}
        }
      },
      async refreshLockStatus() {

        try {

        console.log('--refreshLockStatus')
        const result = await this.fetchCheckShare()
        const { title, open } = result

        console.log('--open', open)
        this.$refs.popupUnlock.close();
        console.log('--close')

        if (open) {
          this.isLocked = false
          this.$refs.popupQrCode.open()
          return
        }

        title && uni.showToast({
          title,
          icon: 'none'
        })

      } catch (err) {
        console.log('--err', err)
      }

      },
      async onPlay() {
        if (!this.isLocked) {
          this.$refs.popupQrCode.open()
          return
        }
        
        const result = await this.fetchCheckShare()
        const { open, need_num, num } = result

        if (open) { // 转发已达标
          this.isLocked = false
          this.$refs.popupQrCode.open()
        } else {
          uni.showToast({
            title: `还需转发${need_num - num}次`,
            icon: 'none',
          })
          this.$refs.popupUnlock.open();
        }
      },
      async onTimeupdate(e) {
        const { currentTime } = e.target

        // todo: check
        if (currentTime >= this.detail.timeNum) {
          this.videoContext.pause()
          this.timeIsUp = true
          this.onPlay()
        }
      },
    },
  }
</script>

<style scoped lang="scss">
.video {
  width: 100%;
  height: calc(100vh - #{$navbar-height} - var(--status-bar-height) - 20rpx);
}

.info {
  color: #FFFFFF;
  font-size: 28rpx;

  position: fixed;
  left: 40rpx;
  bottom: 100rpx;

  .title {
    font-size: 32rpx;
    margin: 5rpx 0;
  }
}

</style>