import throttle from "../../../utils/tool/throttle";

export default {

    data(){
        return {
            showValue: 1
        }
    },

    watch:{
        value:function (value) {
            if (this.showValue !== value)  {
                return this.setValue(value,true);
            }
        },
        maxValue:function (value) {
            return this.changeValue(this.showValue,true);
        }
    },

    methods:{

        // 设置value
        setValue:function (value,draw = false,change=false) {

            if (typeof value === 'number') {

                if ( value !== this.showValue) {
                    return this.changeValue(value,draw,change);
                } else {
                    return undefined;
                }

            } else {

                let newValue = Number(value);

                if(!newValue){
                    newValue = parseInt(value);
                }

                if (newValue && newValue !== this.showValue) {
                    return this.changeValue(parseInt(newValue),draw,change);
                } else {
                    return this.changeValue(this.minValue, draw, change);
                }
            }

        },

        // change
        changeValue:function(value,draw,change){

            value = this.getValue(value);

            if (this.showValue !== value){this.showValue = value}
            if (this.autoChange && !change && this.showValue !== this.value) {
                change = true;
            }


            if(change) {
                this.changeEvent(this.showValue);
                this.$emit('input',{
                    value: this.showValue
                });
            }
        },

        changeEvent:function(){
            return this.$emit('change',{
                value: this.showValue
            });
        },

        // 获取value
        getValue: function (value) {

            if (value < this.minValue) {
                return this.minValue;
            } else if (value > this.maxValue) {
                return this.maxValue;
            } else {
                return value;
            }
        },

        // 添加
        add:function () {
            if (this.showValue < this.maxValue && !this.disabled) {
                return this.setValue(this.showValue+this.step,true,true);
            }
        },

        // 减少
        reduce:function () {
            if (this.showValue > this.minValue && !this.disabled) {
                return this.setValue(this.showValue - this.step,true,true);
            }
        },

        // 失去焦点的时候触发
        blur:function (e) {
            return this.setValue(Number(e.detail.value),true,true);
        }

    },

    mounted(){
        if (this.throttle) {
            this.createThrottle = new throttle({
                handle: this.changeEvent,
                call: this,
                delay: this.throttle,
                first:false
            });
            this.changeEvent = this.createThrottle.supper;
        }

        return this.setValue(this.value,true);
    },

    beforeDestroy(){
        this.createThrottle && this.createThrottle.destroy();
        this.createThrottle = null;
    }

}