<template>
	<div class="uni-numbox" :style="{width: width + 'px'}">
		<div @click="_calcValue('minus')" class="uni-numbox__minus uni-cursor-point">
			<span class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue <= uiMin || disabled }">-</span>
		</div>
		<input
      :disabled="disabled"
      @focus="_onFocus"
      @blur="_onBlur"
      class="uni-numbox__value"
      type="number"
			v-model="inputValue"
    />
		<div @click="_calcValue('plus')" class="uni-numbox__plus uni-cursor-point">
			<span class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue >= uiMax || disabled }">+</span>
		</div>
	</div>
</template>
<script>
	/**
	 * XdNumberUni 数字输入框
	 * @description 带加减按钮的数字输入框
	 * @property {Number} value 输入框当前值
	 * @property {Number} min 最小值
	 * @property {Number} max 最大值
	 * @property {Number} step 每次点击改变的间隔大小
	 * @property {Boolean} disabled = [true|false] 是否为禁用状态
	 * @event {Function} change 输入框值改变时触发的事件，参数为输入框当前的 value
	 */

	export default {
		name: "XdNumber",
		props: {
			value: {
				type: [Number, String],
				default: 1
			},
      width: {
        type: [Number, String],
        default: 180
      },
			min: {
				type: Number | String,
				default: 0
			},
			max: {
				type: Number | String,
				default: 100
			},
			step: {
				type: Number,
				default: 1
			},
			disabled: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				inputValue: 0,
        uiMin: 0,
        uiMax: 0,
			};
		},
		watch: {
			value(val) {
				this.inputValue = +val;
			},
      min(val){
			  if(this.$xdHelper.checkVarType(val) === 'string') {
          this.uiMin = Number(val);
        }else{
          this.uiMin = val;
        }
      },
      max(val) {
        if (this.$xdHelper.checkVarType(val) === 'string') {
          this.uiMax = Number(val);
        } else {
          this.uiMax = val;
        }
      }
		},
		created() {
      if (this.$xdHelper.checkVarType(this.min) === 'string') {
        this.uiMin = Number(this.min);
      }
      else {
        this.uiMin = this.min;
      }

      if (this.$xdHelper.checkVarType(this.max) === 'string') {
        this.uiMax = Number(this.max);
      }
      else {
        this.uiMax = this.max;
      }
      this.inputValue = +this.value;
		},
		methods: {
			_calcValue(type) {
			  console.log('_calcValue',type)
				if (this.disabled) {
					return;
				}
				const scale = this._getDecimalScale();
				let value = this.inputValue * scale;
				let step = this.step * scale;
				if (type === "minus") {
					value -= step;
					if (value < (this.uiMin * scale)) {
						return;
					}
					if (value > (this.uiMax * scale)) {
						value = this.uiMax * scale
					}
				}

				if (type === "plus") {
					value += step;
					if (value > (this.uiMax * scale)) {
						return;
					}
					if (value < (this.uiMin * scale)) {
						value = this.uiMin * scale
					}
				}

				this.inputValue = (value / scale).toFixed(String(scale).length - 1);
				this.$emit("input", +this.inputValue);
				this.$emit("change", +this.inputValue);
			},
			_getDecimalScale() {

				let scale = 1;
				// 浮点型
				if (~~this.step !== this.step) {
					scale = Math.pow(10, String(this.step).split(".")[1].length);
				}
				return scale;
			},
			_onBlur(event) {
				this.$emit('blur', event)
				let value = event.detail.value;
				if (!value) {
					return;
				}
				value = +value;
				if (value > this.uiMax) {
					value = this.uiMax;
				} else if (value < this.uiMin) {
					value = this.uiMin;
				}
				const scale = this._getDecimalScale();
				this.inputValue = value.toFixed(String(scale).length - 1);
				this.$emit("input", +this.inputValue);
				this.$emit("change", +this.inputValue);
			},
			_onFocus(event) {
				this.$emit('focus', event)
			}
		}
	};
</script>
<style lang="scss" scoped>
	$box-height: 25px;
	$box-line-height: 25px;
	$box-width: 25px;

	.uni-numbox {
    display: flex;
    justify-content: space-between;
    align-items: center;
		flex-direction: row;
		height: $box-height;
		line-height: $box-height;
	}

	.uni-cursor-point {
		cursor: pointer;
	}

	.uni-numbox__value {
		background-color: #eee;
		height: $box-height;
		text-align: center;
		font-size: 12px;
		border-width: 1px;
		border-style: solid;
		border-color: #c8c7cc;
		border-left-width: 0;
		border-right-width: 0;
    box-sizing: border-box;
    flex: 1;
    width: 0;

	}

	.uni-numbox__minus {
		display: flex;
		align-items: center;
		justify-content: center;
		width: $box-width;
		height: $box-height;
		font-size: 14px;
		color: #333;
		background-color: #f8f8f8;
		border-width: 1px;
		border-style: solid;
		border-color: #c8c7cc;
		border-top-left-radius: 6px;
		border-bottom-left-radius: 6px;
		border-right-width: 0;
    box-sizing: border-box;
    flex-shrink: 0;

    & .uni-numbox--text {
      font-size: 16px;
      color: #333;
      line-height: 20px;
    }

    & .uni-numbox--disabled {
      color: #c0c0c0;
      cursor: not-allowed;

    }
	}

	.uni-numbox__plus {
		display: flex;
		align-items: center;
		justify-content: center;
		width: $box-width;
		height: $box-height;
		border-width: 1px;
		border-style: solid;
		border-color: #c8c7cc;
		border-top-right-radius: 6px;
		border-bottom-right-radius:6px;
		background-color: #f8f8f8;
		border-left-width: 0;
    box-sizing: border-box;
    flex-shrink: 0;

    & .uni-numbox--text {
      font-size: 16px;
      color: #333;
      line-height: 20px;
    }

    & .uni-numbox--disabled {
      color: #c0c0c0;
      cursor: not-allowed;

    }

	}


</style>
