<script>
import util from 'eoss-ui/src/utils/util';
export default {
  name: 'EsInputNumber',
  componentName: 'EsInputNumber',
  inheritAttrs: false,
  props: {
    // 显示隐藏组件
    display: {
      type: Boolean,
      default: true
    },
    // 	是否不渲染组件
    hide: {
      type: Boolean,
      default: false
    },
    readonly: Boolean,
    value: [Number, String],
    plain: Boolean,
    precision: Number,
    min: Number,
    default: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    model: {
      get() {
        if (typeof this.value === 'number') {
          return this.value;
        }
        return this.value
          ? Number(this.value)
          : this.default
          ? this.min === undefined
            ? 0
            : this.min
          : '';
      },
      set(val) {
        return val;
      }
    }
  },
  mounted() {},
  methods: {},
  render(h) {
    if (!this.hide) {
      let doms = [];
      if (this.$slots.prefix) {
        doms.push(h('template', { slot: 'prefix' }, this.$slots.prefix));
      }
      if (this.$slots.suffix) {
        doms.push(h('template', { slot: 'suffix' }, this.$slots.suffix));
      }
      if (this.$slots.prepend) {
        doms.push(h('template', { slot: 'prepend' }, this.$slots.prepend));
      }
      if (this.$slots.append) {
        doms.push(h('template', { slot: 'append' }, this.$slots.append));
      }
      if (this.$slots.default) {
        doms.push(this.$slots.default);
      }
      if (this.readonly) {
        return h(
          'div',
          { class: ['es-input__inner', { 'es-plain': this.plain }] },
          [util.replenish({ data: this.model, precision: this.precision })]
        );
      }
      return h(
        'el-input-number',
        {
          props: {
            label: this.label
              ? typeof this.label === 'string'
                ? this.label
                : this.label.text
              : '',
            precision: this.precision
          },
          attrs: {
            ...this.$attrs,
            min: this.min,
            value: this.model,
            default: this.default
          },
          on: this.$listeners,
          directives: [
            {
              name: 'show',
              value: this.display
            }
          ]
        },
        doms
      );
    } else {
      return '';
    }
  }
};
</script>
