<script setup>
  import { ref } from '../../../../adapter-vue'
  import custom from '../../../../assets/icon/live-stream.svg'
  import ToolbarItemContainer from '../toolbar-item-container/index.vue'
  import { isUniFrameWork } from '../../../../utils/env'
  import { navigateTo } from '../../../../../utils/router'
  import { getAnchorDetail } from '../../../../../api/tui-kit'
  import { useUI } from '../../../../../utils/use-ui'

  const { showDialog } = useUI()
  const evaluateIcon = custom

  const props = defineProps({
    groupId: {
      type: String,
      default: ''
    },
    userData: {
      type: Object,
      default: () => ({})
    }
  })

  const container = ref()
  /**
   * 主播申请状态
   * 0 待审核 1 审核通过 2 审核拒绝 3 已封禁 9 新增
   */
  const stateData = ref(0)
  const onDialogShow = async () => {
    const res = await getAnchorDetail()
    if (res?.data) {
      stateData.value = res.data.status
    } else {
      stateData.value = 9
    }

    container?.value?.toggleDialogDisplay(false)
    if ([0, 2, 9].includes(stateData.value)) {
      navigateTo(
        '/TUIKit/components/TUIChat/message-input-toolbar/live-stream/apply',
        { type: stateData.value }
      )
      return
    }
    if (stateData.value === 1) {
      // 跳转到开播页面
      uni.navigateTo({
        url: `/pages/anchor/index?groupId=${encodeURIComponent(
          props.groupId
        )}&creatorType=1`
      })
      return
    }
    if (stateData.value === 3) {
      showDialog('提示', '您已被封禁，请联系管理员解封', false)
      return
    }
    return
    if (
      ['Owner', 'Admin'].includes(
        props.userData.groupProfile.selfInfo.role
      )
    ) {
      // 管理员跟群主可直接开直播
      uni.navigateTo({
        url: `/pages/anchor/index?groupId=${encodeURIComponent(
          props.groupId
        )}&creatorType=2`
      })
    } else {
      const res = await getAnchorDetail()
      if (res?.data) {
        stateData.value = res.data.status
      } else {
        stateData.value = 9
      }

      container?.value?.toggleDialogDisplay(false)
      if ([0, 2, 9].includes(stateData.value)) {
        navigateTo(
          '/TUIKit/components/TUIChat/message-input-toolbar/live-stream/apply',
          { type: stateData.value }
        )
        return
      }
      if (stateData.value === 1) {
        // 跳转到开播页面
        uni.navigateTo({
          url: `/pages/anchor/index?groupId=${encodeURIComponent(
            props.groupId
          )}&creatorType=1`
        })
        return
      }
      if (stateData.value === 3) {
        showDialog('提示', '您已被封禁，请联系管理员解封', false)
        return
      }
    }
  }
</script>

<template>
  <ToolbarItemContainer
    ref="container"
    :iconFile="evaluateIcon"
    :iconWidth="isUniFrameWork ? '34px' : '20px'"
    :iconHeight="isUniFrameWork ? '34px' : '20px'"
    title="直播"
    @onDialogShow="onDialogShow"
  ></ToolbarItemContainer>
</template>

<style scoped lang="scss"></style>
