<template>
    <span :class="label?'input-span':''" style="position:relative">
        <span v-if="label">{{$dict.tranByPubMap(label)}} :</span>
        <el-input v-model.number="val" type="number"  v-bind="$attrs"  @change="onChange"  @keyup.native="onKeyUp"
            @mousewheel.native.prevent :style="$attrs.elstyle+inputStyle" clearable></el-input>
        <!-- {{realval}} -->
    </span>
    
</template>


<script>

export default {
    props:["label","width","value","bet","pointLen"],
    data() {
        var val = this.reTranval(this.value);
        if(isNaN(val)) val =0;
        if(!val && val!==0) val=""
        return {
            val : val,
            realval : "",
        }
    },
    watch:{
        value(newval,oldval){
            if(isNaN(newval)) newval =0;
            if(newval){
                this.val = this.reTranval(newval);
            }else{
                this.val = newval;
            }
            
            this.realval = this.tranval(this.val);
        }
    },

    mounted() {
    },
    computed: {
        inputStyle() {
            if(!this.$attrs.elstyle){
                if(this.width){
                    return ";width:"+this.width+"px;";
                }else {
                    return ";width:auto;";
                }
            }
        }
    },

    methods: {
        getValue(){
            var val = "";
            if(!isNaN(this.val)) val = this.tranval(this.val);
            this.val = this.reTranval(val);
            if(val===0) this.val = 0;
            this.realval = val;
            return val;
        },
        onKeyUp(){
            var val = this.getValue();
            this.$emit('input',val);
            this.$emit('onKeyup',val);
        },
        onChange(){
            var val = this.getValue();
            this.$emit('input',val);
            this.$emit('change',val);
        },
        tranval(val=0){
            try{
                var bet = this.bet || 100;
                if(isNaN(val) || bet==1) return val;
                var newval = (val * bet).toFixed(this.pointLen || 2)*1;
                if(isNaN(newval)){
                    return val
                }else{
                    return newval
                }
            }catch(ex){
                console.info(`tranval数值转换错误 参数:${val} 异常:${ex.message}`)
            }
            return val;
        },
        reTranval(val=0){
            try{
                var bet = this.bet || 100;
                if(isNaN(val) || bet==1) return val;
                var newval =(val / bet).toFixed(this.pointLen || 2)*1;
                if(isNaN(newval)){
                    return val
                }else{
                    return newval
                }
            }catch(ex){
                console.info(`reTranval数值转换错误 参数:${val} 异常:${ex.message}`)
            }
            return val;
        }
    }
}
</script>

<style lang="less" scoped>
    .input-span{
        display: inline-block;
        margin-left: 10px;
        margin-right: 10px;
        margin-top: 5px;
        position: relative;
        >span{
            display: inline-block;
            white-space: nowrap;
            color:gray;
            font-size: 15px;
        }
    }
</style>
<style lang="less">
    .el-form-item__content .input-span{
        margin-top: 0px;
    }
</style>