<template>
	<view
    v-if="gStyleData !== null"
    class="uni-numbox"
    :style="[{width: width + 'rpx'},numberColorComp]"
    :class="{circle:circle}"
  >
		<view @click="_calcValue('minus')" class="uni-numbox__minus uni-cursor-point" :class="{'numbox--disabled': inputValue <= uiMin || disabled }">
      <view class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue <= uiMin || disabled }">
        <xd-font-icon
          icon="iconjian"
          size="24"
          :color="inputValue <= uiMin || disabled?'#c0c0c0':`${circle?gStyleData.$mainColor:'#333'}`"
        ></xd-font-icon>
      </view>
		</view>
		<input
      :disabled="disabled || circle"
      @focus="_onFocus"
      @blur="_onBlur"
      class="uni-numbox__value"
      :class="{disabled: (disabled || circle), circle: circle}"

      type="number"
			v-model="inputValue" />
		<view @click="_calcValue('plus')" class="uni-numbox__plus uni-cursor-point" :class="{'numbox--disabled': inputValue >= uiMax || disabled }">
      <view class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue >= uiMax || disabled }">
        <xd-font-icon
          icon="iconjia"
          size="24"
          :color="inputValue >= uiMax || disabled?'#c0c0c0':`${circle?'#fff':'#333'}`"
        ></xd-font-icon>
      </view>
		</view>
	</view>
</template>
<script>/**
	 * XdNumber 数字输入框
	 * @description 带加减按钮的数字输入框
	 * @property {Number} value 输入框当前值
	 * @property {Number} min 最小值
	 * @property {Number} max 最大值
	 * @property {Number} step 每次点击改变的间隔大小
	 * @property {Boolean} disabled = [true|false] 是否为禁用状态
	 * @event {Function} change 输入框值改变时触发的事件，参数为输入框当前的 value
	 */
	import {getParentsStyles} from "@/utils/xd.base";
  import XdFontIcon from "@/components/XdFontIcon/XdFontIcon.vue";

export default {
		name: "XdNumber",
    components:{
      XdFontIcon
    },
		props: {
			value: {
				type: [Number, String],
				default: 1
			},
      width: {
        type: [Number, String],
        default: 150
      },
			min: {
				type: Number | String,
				default: 0
			},
			max: {
				type: Number | String,
				default: 100
			},
			step: {
				type: Number,
				default: 1
			},
			disabled: {
				type: Boolean,
				default: false
			},
      circle:{
        type: Boolean,
        default: false
      },
      color: {
        type: [ String],
        default: ''
      }
		},
		data() {
			return {
				inputValue: 0,
        uiMin: 0,
        uiMax: 0,
        gStyleData:null,
			};
		},
    computed:{
      numberColorComp(){
        if(this.gStyleData === null) return {}
        return {
          '--i-num-main-color': this.color || this.gStyleData.$mainColor
        }
      },
    },
		watch: {
			value(val) {
				this.inputValue = +val;
			},
      min(val){
			  if(this.$xdUniHelper.checkVarType(val) === 'string') {
          this.uiMin = Number(val);
        }else{
          this.uiMin = val;
        }
      },
      max(val) {
        if (this.$xdUniHelper.checkVarType(val) === 'string') {
          this.uiMax = Number(val);
        } else {
          this.uiMax = val;
        }
      }
		},
		async created() {
      let pageStyle = this.systemStyle || {};
      let style = await getParentsStyles(this.$parent);
      this.gStyleData = Object.assign({}, JSON.parse(JSON.stringify(style)) , pageStyle);

      if (this.$xdUniHelper.checkVarType(this.min) === 'string') {
        this.uiMin = Number(this.min);
      }
      else {
        this.uiMin = this.min;
      }

      if (this.$xdUniHelper.checkVarType(this.max) === 'string') {
        this.uiMax = Number(this.max);
      }
      else {
        this.uiMax = this.max;
      }
      this.inputValue = +this.value;

      if (this.disabled === true) this.inputValue = ''
		},
		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) {
          this.$emit("input", '');
          this.$emit("change", '');
					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: 48rpx;
	/* #ifdef APP-NVUE */
	$box-line-height: 35px;
	/* #endif */
	$box-line-height: 36rpx;
	$box-width: 48rpx;

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

  .circle {
    .uni-cursor-point {
      border-radius: 50%;

    }

    .uni-numbox__minus {
      border: 2rpx solid var(--i-num-main-color) !important;
      & > text {
        color: var(--i-num-main-color)!important;
      }
    }
    .uni-numbox__plus {
      border: 2rpx solid var(--i-num-main-color) !important;
      background-color: var(--i-num-main-color) !important;
      & > text {
        color: #fff!important;
      }
    }

  }

	.uni-cursor-point {
		/* #ifdef H5 */
		cursor: pointer;
		/* #endif */
	}

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

    &.disabled {
      color: #bbb;
    }

    &.circle {
      color: #333!important;
    }

	}

	.uni-numbox__minus {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: $box-width;
		height: $box-height;
		// line-height: $box-line-height;
		// text-align: center;
		font-size: 28rpx;
		color: $uni-text-color;
		background-color: $uni-bg-color-grey;
		border-width: 1rpx;
		border-style: solid;
		border-color: $uni-border-color;
		border-top-left-radius: $uni-border-radius-base;
		border-bottom-left-radius: $uni-border-radius-base;
		border-right-width: 0;
    box-sizing: border-box;
    flex-shrink: 0;
	}

	.uni-numbox__plus {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: $box-width;
		height: $box-height;
		border-width: 2rpx;
		border-style: solid;
		border-color: $uni-border-color;
		border-top-right-radius: $uni-border-radius-base;
		border-bottom-right-radius: $uni-border-radius-base;
		background-color: $uni-bg-color-grey;
		border-left-width: 0;
    box-sizing: border-box;
    flex-shrink: 0;
	}

	.uni-numbox--text {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
		color: $uni-text-color;
	}

	.uni-numbox--disabled {
		color: $uni-text-color-disable;
		/* #ifdef H5 */
		cursor: not-allowed;
		/* #endif */
	}
</style>
