<template>
  <div class="display-card" v-if="cardShow">
    <span @click="cardClose" v-if="cardShow" class="cardClose"><img src="/images/all/close.png"></span>
     <div class="content" v-if="cardShow">
       <div>
         <ul class="list-unstyled">
           <li ref="pingdanListWrapper" v-for="item in title"  :style="'top: -' + 30 * timerIndex + 'px'" :class="{'top': isTop}">
             <span>{{item}}</span>
           </li>
         </ul>
         <ul class="list-unstyled">
           <li ref="pingdanListWrapper" v-for="c in counts" v-if="c"  :style="'top: -' + 30 * timerIndex + 'px'" :class="{'top': isTop}">
             <span v-for="(item, index) in formatScrollNumber(c.count, c.logo, c)" class="count-distance" :class="'count' + index">{{item}}</span>
           </li>
         </ul>
       </div>
       <div v-for="item in itemCounts">
         <p>
           <span v-html="item.count"></span>
           <span style="color: #333" v-if="item.type === 3">家</span>
           <span style="color: #333" v-if="item.type === 2">条</span>
         </p>
       </div>
     </div>
  </div>
</template>
<script>
  export default {
    name: 'display-card',
    data () {
      return {
        cardShow: true,
        timerIndex: 0,
        isTop: false, // 判断是否滚动至顶,
        timer: {}, // 定时器实体
        title: ['品  牌', '现  货', '规格书', '店  铺']
      }
    },
    mounted () {
      this.$nextTick(() => {
        this.changeInterval(true)
      })
    },
    methods: {
      changeInterval: function (flag) {
        if (flag) {
          this.timer = setInterval(() => {
            this.isTop = false
            let isChange = true
            this.timerIndex++
            if (this.$refs.pingdanListWrapper[0]) {
              let _transitionEvent = this.baseUtils.whichTransitionEvent()
              _transitionEvent && this.$refs.pingdanListWrapper[0].addEventListener(
                _transitionEvent, () => {
                  if (isChange) {
                    let title = this.title.shift()
                    let count = this.counts.shift()
                    this.title.push(title)
                    this.counts.push(count)
                    this.timerIndex = 0
                    isChange = false
                    this.isTop = true
                  }
                })
            }
          }, 3000)
        } else {
          clearInterval(this.timer)
        }
      },
      cardClose () {
        this.cardShow = false
      },
      formatScrollNumber (num, logo, name) {
        if (num) {
          // console.log(num, logo, name)
          if (num > 99999999) {
            let str2 = num.toString()
            num = Math.floor(num / 100000000)
            if (parseInt(str2.charAt(str2.length - 8)) > 8) {
              num = num + 1
            }
            let length = String(num).length
            this.len = length > 3 ? length + 1 : length
            num = (Array(7 - length).join(0) + num)
            num = num.toString() + '亿'
          } else if (num > 9999) {
            let str = num.toString()
            num = Math.floor(num / 10000)
            if (parseInt(str.charAt(str.length - 4)) > 4) {
              num = num + 1
            }
            let length = String(num).length
            this.len = length > 3 ? length + 1 : length
            num = (Array(7 - length).join(0) + num)
            num = num.toString() + '万'
          } else {
            let length = String(num).length
            this.len = length > 3 ? length + 1 : length
            num = (Array(7 - length).join(0) + num)
            if (logo === 0) {
              num = num.toString() + '家'
            } else {
              num = num.toString() + '个'
            }
          }
        }
        let _arr = []
        for (let i = 0; i < num.length; i++) {
          _arr.push(num[i])
        }
        return _arr
      },
      formatNumber (num, type) {
        if (num) {
          if (num.toString().indexOf('E') !== -1) {
            let arr = num.toString().split('E')
            num = arr[0] * Math.pow(10, arr[1])
          }
          if (num > 99999999) {
            let str2 = num.toString()
            num = Math.floor(num / 100000000)
            if (parseInt(str2.charAt(str2.length - 8)) > 8) {
              num = num + 1
            }
            num += '<span style="color: #333">亿</span>'
          } else if (num > 9999) {
            let str = num.toString()
            num = Math.floor(num / 10000)
            if (parseInt(str.charAt(str.length - 4)) > 4) {
              num = num + 1
            }
            num += '<span style="color: #333">万</span>'
          } else {
            if (type === 1) {
              num += '<span style="color: #333">元</span>'
            } else {
              num += ''
            }
          }
        }
        return num || 0
      },
      formatDouble (num) {
        if (num) {
          if (num.toString().indexOf('E') !== -1) {
            let arr = num.toString().split('E')
            num = arr[0] * Math.pow(10, arr[1])
          }
          if (num > 99999999) {
            num = (num / 100000000).toFixed(2).slice(num.length - 1, 4) + '<span style="color: #333">亿</span>'
          } else if (num > 9999) {
            num = (num / 10000).toFixed(2).slice(num.length - 1, 4) + '<span style="color: #333">万</span>'
          } else {
            num += ''
          }
        }
        return num || 0
      }
    },
    computed: {
      counter () {
        return this.$store.state.option.counter.data
      },
      inquirySheet () {
        let sheetNum = this.$store.state.count.inquirySheet.data
        return sheetNum ? this.formatDouble(sheetNum.count) : 0
      },
      inquirySheetLast () {
        let lastSheetNum = this.$store.state.count.inquirySheetLast.data
        return lastSheetNum ? this.formatDouble(lastSheetNum.count) : 0
      },
      itemCounts () {
        let arr = []
        let count = this.baseUtils.deepCopy(this.counter)
        if(count.length){
          count.forEach((value) => {
            if (value.item === '供应商') {
              arr[0] = {id: value.item, count: this.formatNumber(value.count), type: 3}
            } else if (value.item === '本年交易金额') {
              arr[1] = {id: value.item, count: this.formatNumber(value.count), type: 1}
            } else if (value.item === '上年交易金额') {
              arr[2] = {id: value.item, count: this.formatNumber(value.count), type: 1}
            }
          })
          arr[3] = {id: '本月求购数', count: this.inquirySheet ? this.inquirySheet : 0, type: 2}
          arr[4] = {id: '上月求购数', count: this.inquirySheetLast ? this.inquirySheetLast : 0, type: 2}
        }
        return arr
      },
      counts () {
        let arr = []
        let count = this.baseUtils.deepCopy(this.counter)
        if(count.length) {
          count.forEach((value) => {
            if (value.item === '品牌') {
              arr[0] = {id: value.item, count: value.count, logo: 1}
            } else if (value.item === '现货') {
              arr[1] = {id: value.item, count: value.count, logo: 1}
            } else if (value.item === '规格书') {
              arr[2] = {id: value.item, count: value.count, logo: 1}
            } else if (value.item === '店铺') {
              arr[3] = {id: value.item, count: value.count, logo: 0}
            }
          })
        }
        return arr
      }
    }
  }
</script>
<style lang="scss" scoped>
  .display-card{
    position: fixed;
    right: 80px;
    top: 182px;
    z-index: 10000;
    .cardClose{
      position: absolute;
      right: -5px;
      top: 12px;
      opacity: 0.8;
      &:hover{
        cursor: pointer;
      }
    }
    .content{
      margin-top: 10px;
      /*width: 178px;*/
      /*height: 536px;*/
      background: url('/images/all/countBg.png') no-repeat;
      div{
        height: 56px;
        width: 158px;
        padding-top: 19px;
        margin-left: 16px;
        &:first-child{
          height: 109px;
          padding-top: 1px;
          ul{
            &:first-child{
                max-height: 30px;
                width: 100%;
                text-align: center;
                overflow: hidden;
                position: relative;
                top: 16px;
                li{
                  height: 30px;
                  line-height: 30px;
                  font-weight: bold;
                  font-size: 21px;
                  letter-spacing: 0px;
                  color: #ff3600;
                  position: relative;
                  top: 0;
                  transition: top 1s;
                  -moz-transition: top 1s; /* Firefox 4 */
                  -webkit-transition: top 1s; /* Safari and Chrome */
                  -o-transition: top 1s; /* Opera */
                  &.top {
                    transition: top 0s;
                    -moz-transition: top 0s; /* Firefox 4 */
                    -webkit-transition: top 0s; /* Safari and Chrome */
                    -o-transition: top 0s; /* Opera */
                  }
                }
              }
            &:last-child{
              max-height: 30px;
              width: 100%;
              overflow: hidden;
              position: relative;
              top: 21px;
              text-align: right;
              li{
                position: relative;
                top: 0;
                left: 0;
                margin: 0 auto;
                width: 141px;
                height: 30px;
                line-height: 28px;
                font-weight: bold;
                font-size: 21px;
                text-align: left;
                color: #bc1b1c;
                transition: top 1s;
                -moz-transition: top 1s;
                -webkit-transition: top 1s;
                -o-transition: top 1s;
                &.top {
                  transition: top 0s;
                  -moz-transition: top 0s; /* Firefox 4 */
                  -webkit-transition: top 0s; /* Safari and Chrome */
                  -o-transition: top 0s; /* Opera */
                }
                span.count-distance{
                  display: inline-block;
                  padding-left: 3.3px;
                  margin-left: 2px;
                  font-size: 18px;
                  color: #bc1b1c;
                  width: 17px;
                }
                span.count-distance.count6{
                  position:relative;
                  top: -2px;
                  padding-left: 0;
                  font-weight: bold;
                  color: #fff;
                }
              }
            }
          }
        }
        p{
          width: 100%;
          text-align: center;
          font-size: 20px;
          color: #f14e4e;
        }
      }
      .enter{
        width: 100%;
        display: inline-block;
        position: absolute;
        bottom: 35px;
        left: -5px;
        text-align: center;
      }
    }
  }
</style>
