<template>
  <ul class="view-item" :class="{ invert }">
    <template v-if="!invert">
      <li class="text">{{ text }}</li>
      <li class="num" :class="{ blue: numBlue }">
        <template v-if="!run">{{ num | famount }}</template>
        <count-to v-else :startVal="0" :endVal="num" :duration="duration" :decimals="decimals" />
      </li>
    </template>
    <template v-else>
      <li class="num" :class="{ blue: numBlue }">
        <template v-if="!run">{{ num | famount }}</template>
        <count-to v-else :startVal="0" :endVal="num" :duration="duration" :decimals="decimals" />
      </li>
      <li class="text">{{ text }}</li>
    </template>
  </ul>
</template>

<script>
import countTo from '@/components/count-to';
export default {
  name: 'item',
  props: {
    text: String,
    num: [String, Number],
    numBlue: {
      default: false,
      type: Boolean
    },
    invert: {
      // 是否颠倒
      type: Boolean,
      default: false
    },
    run: {
      // 是否数字转动变化
      type: Boolean,
      default: false
    },
    decimals: {
      // 小数位
      type: Number,
      default: 0
    },
    duration: {
      type: Number,
      default: 2000
    }
  },
  filters: {
    famount(s) {
      var str = s ? s.toString() : '0';
      var reg = str.indexOf('.') > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g;
      return str.replace(reg, '$1,');
    }
  },
  components: {
    countTo
  }
};
</script>

<style lang="scss">
.view-item {
  display: block;
  position: relative;
  overflow: hidden;
  &:hover .num {
    transform: scale(1.2);
  }
  &.invert {
    .text {
      margin-bottom: 0px;
    }
    .num {
      margin-bottom: 10px;
    }
  }
  .num,
  .text {
    padding: 0 20px 0;
    text-align: center;
    text-overflow: ellipsis;
    overflow: hidden;
    max-width: 100%;
    white-space: nowrap;
    word-wrap: normal;
    width: auto;
    &.blue {
      color: #2f54eb;
    }
  }
  .text {
    margin-bottom: 10px;
    height: 20px;
    font-size: 14px;
    font-weight: 400;
    color: rgba(96, 98, 102, 1);
    line-height: 20px;
  }
  .num {
    height: 37px;
    font-size: 32px;
    font-family: 'din';
    font-weight: bold;
    color: rgba(48, 49, 51, 1);
    line-height: 37px;
    transition: all 0.25s;
    cursor: pointer;
  }
}
</style>
