<template>
  <div class="display-card">
    <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 v-for="(item, index) in title" :style="'top: -' + 30 * timerIndex + 'px'" :class="{'top': isTop}">
             <span>{{item}}</span>
           </li>
         </ul>
         <ul class="list-unstyled">
           <li v-for="(c, index) in counts.data" :style="'top: -' + 30 * timerIndex + 'px'" :class="{'top': isTop}">
             <count-item :value ="c.count"></count-item>
           </li>
         </ul>
       </div>
       <div>
         <p><span v-html="all"></span><span style="color: #333">家</span>
         </p>
       </div>
       <div>
         <p v-if="payMoney">
           <span v-html="payMoney"></span>
         </p>
         <p v-else><span>0元</span></p>
       </div>
       <div>
         <p v-if="payMoneyLast">
           <span v-html="payMoneyLast"></span>
         </p>
         <p v-else><span>0元</span></p>
       </div>
       <div>
         <p v-if="inquirySheet">
           <span v-html="inquirySheet"></span><span style="color: #333">条</span>
         </p>
         <p v-else><span>0条</span></p>
       </div>
       <div>
         <p v-if="inquirySheetLast">
           <span v-html="inquirySheetLast"></span><span style="color: #333">条</span>
         </p>
         <p v-else><span>0条</span></p>
       </div>
       <a class="enter" @click="goStoreApply()">
         <img src="/images/all/enter2.png">
       </a>
     </div>
  </div>
</template>
<script>
  import CountItem from './countItem.vue'
  export default {
    name: 'display-card',
    data () {
      return {
        cardShow: true,
        timerIndex: 0,
        isTop: false, // 判断是否滚动至顶,
        timer: {}, // 定时器实体
        title: [ '品牌', '现货', '规格书' ]
      }
    },
    components: {
      CountItem
    },
    mounted () {
      this.$nextTick(() => {
        this.changeInterval(true)
      })
    },
    methods: {
      changeInterval: function (flag) {
        if (flag) {
          this.timer = setInterval(() => {
            this.timerIndex ++
            this.isTop = (this.timerIndex % 3 === 0)
            if (this.isTop) {
              this.timerIndex = 0
            }
          }, 3000)
        } else {
          clearInterval(this.timer)
        }
      },
      cardClose () {
        this.cardShow = false
      },
      formatNumber (num, type) {
        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 || type === 2) {
            num += '<span style="color: #333">元</span>'
          } else {
            num += ''
          }
        }
        return num
      },
      goStoreApply: function () {
        if (this.user.logged) {
          if (this.enterprise && this.enterprise.isVendor === 313) {
            window.location.href = '/vendor#/index'
          } else {
            this.$router.push('/register-saler')
          }
        } else {
          this.$router.push('/auth/login')
        }
      }
    },
    computed: {
      allCount () {
        return this.$store.state.count.allCount.data
      },
      payMoneyLast () {
        return this.allCount[0] ? this.formatNumber(this.allCount[0].count, 2) : 0
      },
      payMoney () {
        return this.allCount[1] ? this.formatNumber(this.allCount[1].count, 1) : 0
      },
      inquirySheet () {
        let sheetNum = this.$store.state.count.inquirySheet.data.count
        return this.formatNumber(sheetNum, 3)
      },
      inquirySheetLast () {
        let lastSheetNum = this.$store.state.count.inquirySheetLast.data.count
        return this.formatNumber(lastSheetNum, 4)
      },
      all () {
        let count = this.$store.state.supplier.merchant.merchantAll.data
        let supplierCount = count.content ? count.totalElements + '' : '0'
        return this.formatNumber(supplierCount, 0)
      },
      counts () {
        return this.$store.state.product.common.counts
      },
      enterprise () {
        return this.user.data.enterprise
      }
    }
  }
</script>
<style lang="scss" scoped>
  .display-card{
    position: fixed;
    right: 100px;
    top: 135px;
    width: 180px;
    height: 540px;
    z-index: 100;
    .cardClose{
      position: absolute;
      right: 3px;
      top: 1px;
      opacity: 0.8;
      &:hover{
        cursor: pointer;
      }
    }
    .content{
      margin-top: 10px;
      width: 178px;
      height: 536px;
      background: url('/images/all/countBackground2.png') no-repeat;
      div{
        height: 72px;
        width: 158px;
        padding-top: 30px;
        margin-left: 6px;
        &: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: #fff000;
                  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{
                height: 30px;
                line-height: 30px;
                font-weight: bold;
                font-size: 21px;
                color: #fff;
                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 */
                }
              }
            }
          }
        }
        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>
