<template>
  <view class="page-wx-player">
    <web-view
      :key="webviewKey"
      :src="webUrl"
      @message="onMessage"
    />
  </view>
</template>

<script setup>

let roomId = 0
let liveUserId = 0
onLoad(options => {
  roomId = options.roomId
  liveUserId = options.liveUserId
})

const webUrl = ref('')
const webviewKey = ref(1)
onShow(() => {
  const userId = uni.getStorageSync('cloudUserInfo')?.userId
  const liveType = userId === liveUserId ? 1 : 2
  if (liveType === 1) {
    onHandleModal($t('startLiveTips'))
    return
  }
  // 设置webUrl
  const liveWebViewInfo = JSON.stringify({
    token: uni.getStorageSync('cloudToken') || null,
    userInfo: uni.getStorageSync('cloudUserInfo') || null
  })
  webUrl.value = `${import.meta.env.VITE_APP_DOMAIN_ADDRESS}/package-activities/pages/live-room/live-room?liveType=2&roomId=${roomId}&liveUserId=${liveUserId}&isWebview=1&liveWebViewInfo=${liveWebViewInfo}`

  if (uni.getStorageSync('cloudLiveRoomReload')) {
    // 刷新web-view页面
    uni.removeStorageSync('cloudLiveRoomReload')
    webviewKey.value = Date.now()
  }
})

const onMessage = () => {}

const onHandleModal = (content) => {
  uni.showModal({
    title: '',
    showCancel: false,
    content,
    confirmText: $t('confirm'),
    success: () => {
      uni.switchTab({
        url: '/pages/live-broadcast/live-broadcast'
      })
    }
  })
}

</script>
