<script setup>
import Live from '../components/Live.vue'
import LivePlayback from '../components/LivePlayback.vue'
import { getTest, getRecord } from '../api'
import { useCountdown } from '@/composables/useCountdown'

const props = defineProps({
  isView: { type: Boolean, default: false },
})
const route = useRoute()

const { timeLeft, formattedTime, stop, reset } = useCountdown({
  // 倒计时结束
  onEnd: () => {
    live.value?.stop()
  },
})
const live = ref(null)

const detail = ref(null)
provide('detail', detail)

const duration = computed(() => {
  return parseInt(detail.value?.duration) * 60 || 0
})
const isLocalUpload = computed(() => {
  return detail.value?.upload_way == 2
})

async function fetchInfo() {
  const res = await getTest({ id: route.query.id })
  detail.value = res.data.detail
  timeLeft.value = duration.value
}
watchEffect(() => {
  !props.isView && fetchInfo()
})

// 商品卖点
const hotList = computed(() => {
  return detail.value?.live_speech.selling_point.split(/;|；/)
})
// 营销活动
const actList = computed(() => {
  return detail.value?.live_speech.marketing_campaign.split(/;|；/)
})

// 直播记录数据
const record = ref(null)
provide('record', record)

// 直播数据
const stats = computed(() => {
  const result = { totalViewers: 0, peakViewers: 0, totalGifts: 0, totalLikes: 0, totalGiftViewers: 0 }
  return Object.assign(result, record.value?.live_info.stats)
})

const fetchRecord = async () => {
  const res = await getRecord({ id: route.query.record_id })
  const resDetail = res.data.detail
  record.value = { ...resDetail, live_info: JSON.parse(resDetail.live_info) }
  detail.value = resDetail.live_practice_info
  timeLeft.value = duration.value
}
onMounted(() => {
  props.isView && fetchRecord()
})
</script>

<template>
  <AppCard :title="isView ? '查看历史直播详情' : '直播'" full>
    <div class="live-row">
      <div class="live-col">
        <template v-if="isView">
          <LivePlayback :record="record" v-if="record"></LivePlayback>
        </template>
        <template v-else>
          <Live
            ref="live"
            :isLocalUpload="isLocalUpload"
            :onStart="() => reset(duration)"
            :onStop="stop"
            v-if="detail" />
        </template>
      </div>
      <div class="live-col" style="flex: 1">
        <h2 class="h2-title">直播话术</h2>
        <div class="live-talk-content" v-html="detail?.live_speech.content"></div>
      </div>
      <div class="live-col" style="width: 350px">
        <div class="live-col-box" v-if="isView">
          <h2 class="h2-title">直播数据</h2>
          <div class="live-data">
            <dl>
              <dt>观众总人数：</dt>
              <dd>{{ stats.totalViewers }}</dd>
            </dl>
            <dl>
              <dt>最高峰人数：</dt>
              <dd>{{ stats.peakViewers }}</dd>
            </dl>
            <dl>
              <dt>点赞数：</dt>
              <dd>{{ stats.totalLikes }}</dd>
            </dl>
            <dl>
              <dt>刷礼物人数：</dt>
              <dd>{{ stats.totalGiftViewers }}</dd>
            </dl>
            <dl>
              <dt>刷礼物总数：</dt>
              <dd>{{ stats.totalGifts }}</dd>
            </dl>
          </div>
        </div>
        <div class="live-col-box" v-else>
          <h2 class="h2-title">倒计时</h2>
          <h3 class="live-time">{{ formattedTime }}</h3>
        </div>
        <div class="live-col-box">
          <h2 class="h2-title">主题卖点</h2>
          <ul class="live-tag live-tag__hot">
            <li v-for="item in hotList" :key="item">{{ item }}</li>
          </ul>
        </div>
        <div class="live-col-box">
          <h2 class="h2-title">营销活动</h2>
          <ul class="live-tag live-tag__act">
            <li v-for="item in actList" :key="item">{{ item }}</li>
          </ul>
        </div>
      </div>
    </div>
  </AppCard>
</template>

<style lang="scss">
.live-row {
  height: 100%;
  display: flex;
  gap: 20px;
}
.live-col {
  padding: 20px;
  border-radius: 10px;
  border: 1px solid #eee;
  .h2-title {
    margin-top: 0;
  }
}
.live-tag {
  margin: 20px 0;
  background-color: #eee;
  border-radius: 10px;
  padding: 20px;
  gap: 10px;
  li {
    line-height: 30px;
    background-color: #fff;
    text-align: center;
    border: 1px solid rgba(105, 113, 140, 0.12);
  }
}
.live-data {
  padding: 0 0 20px 10px;
  dl {
    margin: 10px 0;
    display: flex;
  }
  dt {
    font-weight: bold;
  }
}
.live-time {
  height: 140px;
  font-size: 72px;
  text-align: center;
  color: var(--main-color);
}
.live-tag__hot {
  display: flex;
  flex-wrap: wrap;
  li {
    flex: 0 0 calc(50% - 10px);
  }
}
.live-tag__act {
  li {
    margin: 10px 0;
  }
}
.live-talk-content {
  max-height: 660px;
  overflow-y: auto;
  line-height: 24px;
}
</style>
