<template>
  <div class="page-content">
    <div class="stock-detail" ref="pageContent">
      <van-steps direction="vertical" :active="-1" v-if="eventList.length > 0" @click-step="showInfo">
        <van-step v-for="item in eventList" :key="item.id" :id="item.id">
          <template v-slot:inactive-icon>
            <div style="background-color: #000" class="slotIcon"></div>
          </template>
          <div class="stepItem">
            <van-row class="mb-16">
              <van-col span="12">
                <van-tag size="medium" :stlye="{ backgroundColor: item.eventTypeColor }">
                  {{ item.eventTypeName }}
                </van-tag>
              </van-col>
              <van-col span="12">
                <div class="text-end tip">{{ item.sysDate }}</div>
              </van-col>
            </van-row>
            <div v-if="item.eventType === 'CHDP'">
              <div v-if="item.eventNote">
                <div v-if="!item.playFlag" class="audioDiv d-flex align-items-center">
                  <van-icon name="play-circle" @click="playAudio(item)" />
                </div>
                <audio v-else :id="`auto${item.id}`" :src="item.eventNote" controls @play="playAudio(item)"></audio>
              </div>
            </div>
            <div class="eventNote" v-else>{{ item.eventNote }}</div>
          </div>
        </van-step>
      </van-steps>
      <van-empty v-else />
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, nextTick } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import emitter from '@/utils/mitt'
const route = useRoute()
const router = useRouter()
import stockInfoStore from '@/stores/stockInfo'
const stockInfo = stockInfoStore()
let stockCode: any = ref('')
const init = () => {
  if (route.query && route.query.stockCode) {
    stockCode.value = route.query.stockCode
    emitter.emit('showLoading', '')
    getStockEventList()
  } else {
    router.push({ name: '404' })
  }
}
let eventList: any = ref([])
import { stockStockinfoEventList } from '@/utils/api'
const getStockEventList = async () => {
  const { data }: any = await stockStockinfoEventList({
    stockCode: stockCode.value
  })
  data.map((ele: any) => {
    ele.playFlag = false
  })
  eventList.value = data
  nextTick(() => {
    if (stockInfo.id) {
      const el = document.getElementById(stockInfo.id)
      if (el) {
        el.scrollIntoView({ behavior: 'smooth' })
      }
      stockInfo.id = ''
      emitter.emit('hiddenLoading', '')
    } else {
      emitter.emit('hiddenLoading', '')
    }
  })
}
init()

import { showFile } from '@/mixins/show-file'
const { loadingFile } = showFile()
const showInfo = (e: any) => {
  if (eventList.value[e].eventType === 'DP') {
    router.push({ name: 'comment-detail', query: { inboxId: eventList.value[e].inboxId } })
  } else if (eventList.value[e].eventType === 'UPDATE' || eventList.value[e].eventType === 'FIRST') {
    stockInfo.id = eventList.value[e].id
    loadingFile(`${import.meta.env.VITE_BASE_URL}/api/file/${eventList.value[e].reportFileId}`)
  }
}

import { scrollList } from '@/mixins/scroll-list'
import { onBeforeRouteLeave } from 'vue-router'

let pageContent = ref(null)
const scrollPosition = ref(0)

const refresh = () => {
  init()
}

const { setScrollTop, setScrollPositionAndRefreshFlag } = scrollList(pageContent, scrollPosition, refresh)

setScrollTop()
onBeforeRouteLeave((to, from) => {
  setScrollPositionAndRefreshFlag(to, 'comment-detail')
})

const playAudio = (item: any) => {
  eventList.value.map((ele: any) => {
    if (ele.eventType === 'CHDP' && ele.eventNote) {
      if (item.id === ele.id) {
        ele.playFlag = true
        nextTick(() => {
          const el = document.getElementById(`auto${item.id}`)
          if (el) {
            el.play()
          }
        })
      } else {
        if (ele.playFlag) {
          const el = document.getElementById(`auto${item.id}`)
          if (el) {
            el.pause()
          }
          ele.playFlag = false
        }
      }
    }
  })
}
</script>

<style lang='scss' scoped>
.stock-detail {
  height: 100vh;
  overflow-y: auto;
}

:deep(.van-step__circle-container) {
  top: 29px
}

.van-steps--vertical {
  padding-left: 48px;
}

.van-steps__items {
  padding-right: 32px
}

.slotIcon {
  height: 14px;
  width: 14px;
  border-radius: 100%;
}

.stepItem {
  padding: 8px 16px;
}

.van-step--vertical:not(:last-child):after {
  border-color: #b2b2b2;
  margin-right: 46px;
  margin-left: 16px;
}

.eventNote {
  font-weight: 400;
  font-size: 14px;
  line-height: 20px;
}

.audioDiv {
  height: 28px;
  width: 300px;
  border-radius: 32px;
  background-color: #f1f3f4;
  padding-left: 10px;
  margin: 4px 0;

  .van-icon {
    color: #000;
  }
}

audio {
  height: 28px;
  margin: 4px 0;
}
</style>
