<template>
    <div>
        <el-input :disabled="disabled" :class="{'is-right': place === 'right'}" :value="formatValue"  v-bind="$attrs" v-on="inputListeners"  />
    </div>
</template>
<script>

export default {
    props: {
        value: {
            type: [String, Number],
            default: '',
            desc: '数值'
        },
        symbol: {
            type: String,
            default: '',
            desc: '货币标识符'
        },
        decimal: {
            type: Number,
            default: 2,
            desc: '小数位'
        },
        place: {
            type:String,
            default: 'left'
        },
        disabled: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            focused: false,
        }
    },
    computed: {
        formatValue() {
            if (this.focused) {
                if(this.value===''||this.value==null||this.value===0){
                    return ''
                }else{
                    // console.log(this.value);
                    // console.log(this.value.indexOf('.'));
                    // if(this.value.indexOf('.')>0){this.value=this.value.slice(0,2)}
                    // return accounting.unformat(this.value);
                    return this.value
                }
            } else {
                return this.value
                    ? accounting.formatMoney(this.value, this.symbol, this.decimal)
                    : "";
            }
        },
        inputListeners: function() {
            var vm = this;
            // `Object.assign` 将所有的对象合并为一个新对象
            return Object.assign(
            {},
            // 我们从父级添加所有的监听器
            this.$listeners,
            // // 然后我们添加自定义监听器，
            // // 或覆写一些监听器的行为
            {
                // 这里确保组件配合 `v-model` 的工作
                input: function(event) {
                    // var val = accounting.unformat(event);
                    // console.log(event)
                    if(event.indexOf('.')>0){event=event.slice(0,event.indexOf('.')+3)}
                    event = event.replace(/[^\d.]/g,"");
                    vm.$emit("input", event);
                },
                blur: function(event) {
                    vm.$emit("blur", event);
                    vm.focused = false;
                },
                focus: function(event) {
                    vm.focused = true;
                },
                // keyup:function(event){
                //     event=event.replace(/^\D*(\d*(?:\.\d{0,1})?).*$/g, '$1')
                //     vm.$emit("keyup", event);
                // }
            });
        }
    },
    methods: {
        dispatch(componentName, eventName, params) {
            var parent = this.$parent || this.$root;
            var name = parent.$options.componentName;

            while (parent && (!name || name !== componentName)) {
                parent = parent.$parent;
                if (parent) {
                    name = parent.$options.componentName;
                }
            }
            if (parent) {
                parent.$emit.apply(parent, [eventName].concat(params));
            }
        }
    },
}
</script>
<style lang="scss" scoped>
.is-right{
    color: #212121;
    ::v-deep .el-input__inner {
        text-align: right;
        padding: 0;
    }
}

</style>
