<template>
    <div class="chartNum">
                <div class="box-item">
                    <li :class="{'number-item': !isNaN(item), 'mark-item': isNaN(item) }"
                        v-for="(item,index) in orderNum"
                
                        :key="index">
                        <span v-if="!isNaN(item)">
                          <i ref="numberItem">0123456789</i>
                        </span>
                        <span class="comma" v-else >{{item}}</span>
                    </li>
                </div>
            </div>
</template>
<script>
    export default {
        name: 'CountTo',
        props: {
            number: Number
        },
        data() {
            return {
                orderNum: ['0', '0', ',', '0', '0', '0', ',', '0', '0', '0'], // 默认订单总数
            }
        },
        watch: {
            number: {
                handler () {
                   
                },
                immediate: true
            }
        },
        mounted () {
        //      // 这里输入数字即可调用
        //    this.setNumberTransform()
        //             // this.toOrderNum(Math.floor(this.number))
                    
        //             let a = 1212
        //             this.time = setInterval( () => {
        //                 a += 223
        //                 this.setNumberTransform()
        //                 // Math.floor(this.number)
        //                 this.toOrderNum(a)
        //        
        //             }, 1000)
            
        },
        methods: {

            // 调用
            dispatch (data) {
                this.setNumberTransform()
                this.toOrderNum(Math.floor(data))
            },
               // 设置文字滚动
            setNumberTransform () {
              const numberItems = this.$refs['numberItem'] // 拿到数字的ref，计算元素数量
              const numberArr = this.orderNum.filter(item => !isNaN(item))
              console.log(numberArr, 'numberArr', numberItems);
              // 结合CSS 对数字字符进行滚动,显示订单数量
              for (let index = 0; index < numberItems.length; index++) {
                const elem = numberItems[index]
                elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`
              }
            },
            // 处理总订单数字
            toOrderNum(num) {
              num = num.toString()
              console.log(num, '处理总订单数字');
              let str = ''
                for(let i = 0; i< num.length; i += 2) {
                    str += num.slice(i, i +2)
                }
          
                // num = num.slice(0, 2) + ',' + num.slice(2, 5) + ',' + num.slice(5, 8)  + ',' + num.slice(8, 11) + ',' + num.slice(11, 14)
                this.orderNum = this.numFormat(str).split('') 
                      console.log(  this.orderNum , '  this.orderNum ');
                this.$forceUpdate()

            },
                numFormat (num) {
                    num=num.toString().split(".");  // 分隔小数点
                        var arr=num[0].split("").reverse();  // 转换成字符数组并且倒序排列
                        var res=[];
                        for(var i=0,len=arr.length;i<len;i++){
                        if(i%3===0&&i!==0){
                            res.push(",");   // 添加分隔符
                        }
                        res.push(arr[i]);
                        }
                        res.reverse(); // 再次倒序成为正确的顺序
                        if(num[1]){  // 如果有小数的话添加小数部分
                        res=res.join("").concat("."+num[1]);
                        }else{
                        res=res.join("");
                        }
                    return res;
                },
        }
    }
</script>
<style scoped lang='less'>
     /*订单总量滚动数字设置*/
    .box-item {
        position: relative;
        height: 1.25rem;
        font-size: 0.875rem;
        line-height: 41px;
        text-align: center;
        list-style: none;
        color: rgb(107 191 209);
        writing-mode: vertical-lr;
        text-orientation: upright;
        /*文字禁止编辑*/
        -moz-user-select: none; /*火狐*/
        -webkit-user-select: none; /*webkit浏览器*/
        -ms-user-select: none; /*IE10*/
        -khtml-user-select: none; /*早期浏览器*/
        user-select: none;
        /* overflow: hidden; */
        font-family: Italiana, sans-serif;
    }
    /* 默认逗号设置 */
    .mark-item {
        width: 10px;
        height: 100px;
        margin-right: 0.5vw;
        // margin-left: 0.5vw;
        line-height: 0.5vw;
        font-size: 0.675rem;
        position: relative;
        & > span {
            position: absolute;
            width: 100%;
            bottom: 0;
            writing-mode: vertical-rl;
            text-orientation: upright;
        }
    }
    /*滚动数字设置*/
    .number-item {
        width: 3vw;
        height: 9vh;
        background: #ffffff;;
        list-style: none;
        margin-right: 5px;
        background:rgb(10, 20, 71);
        border-radius:4px;
        overflow: hidden;
        & > span {
            position: relative;
            display: inline-block;
            margin-right: 2vw;
            width: 100%;
            height: 100%;
            writing-mode: vertical-rl;
            text-orientation: upright;
            overflow: hidden;
            & > i {
                font-style: normal;
                position: absolute;
                top: 11%;
                left: 50%;
                transform: translate(-50%,0);
                transition: transform 1s ease-in-out;
                letter-spacing: 10vw;
            }
        }
    }
    .number-item:last-child {
        margin-right: 0;
    }

    .comma {
        margin-top: -2vh;
    }
</style>