import { useEventTracking } from '@/helpers/hooks'
import request from '@/helpers/request'
import dayjs from 'dayjs'
import { ImagePreview } from 'vant'
import { defineComponent } from 'vue'
import styles from './help-center-detail.module.less'

export default defineComponent({
  name: 'special-detail',
  data() {
    return {
      detail: {
        title: '',
        createTime: '',
        content: ''
      }
    }
  },
  async mounted() {
    try {
      const query = this.$route.query
      const res = await request.get('/api-cms/news/query/' + query.id)
      let { title, createTime, content } = res.data
      this.detail = {
        title,
        createTime: dayjs(createTime).format('YYYY-MM-DD HH:mm:ss'),
        content
      }
    } catch {}
    useEventTracking('热门资讯')
  },
  methods: {
    onShowImg(target: any) {
      const { localName } = target.srcElement
      if (localName !== 'img') {
        return
      }
      let startPosition = 0
      const domList = document.querySelectorAll('.msgWrap img')
      let imgList = Array.from(domList).map((item: any, index: number) => {
        if (target.srcElement == item) {
          startPosition = index
        }
        return item.src
      })

      ImagePreview({
        images: imgList,
        startPosition: startPosition,
        closeable: true
      })
    }
  },
  render() {
    return (
      <div class={styles['help-center-detail']}>
        <h2>{this.detail.title}</h2>
        <div class={styles.titleInfo}>
          <p>酷乐秀</p>
          <p>{this.detail.createTime}</p>
        </div>
        <div
          class={[styles.imgContent, 'msgWrap']}
          // onClick={this.onShowImg}
          v-html={this.detail.content}
        ></div>
      </div>
    )
  }
})
