<template>
  <!-- 数字选择器 -->
  <div class="home">
    <input @blur="blur" type="number" :disabled="disabled" :min="min" v-model="num" />
  </div>
</template>

<script>
  export default {
    name: 'from-number-select',
    props: ['value', 'min', 'text', 'obj', "setSortFun",'disabled',
    'isBlurSave','max','isDecimal','limitMax','limitMin','isNeedErrorNum'],
    data() {
      return {
        num: null,
        msg: '',
      }
    },
    created() {
      this.num = this.value
    },
    watch:{
      value(val){
        this.num = val
      },
    },
    methods: {
      blur() {
        this.alertMsg()
      },

      verification() {
        if(this.isBlurSave){
          // if (!/^[^\u4e00-\u9fa5]{1,10}$/g.test(this.num)) {
          //   this.msg = '请输入正确的' + this.text
          //   return false;
          // }

          if(this.isDecimal){
            var reg = /^(\-)*(\d+)\.(\d\d\d).*$/
            if (reg.test(this.num)) {
              this.msg = '请输入两位小数'
              return false;
            }
          }else{
            if (!(/^([1-9]\d*)(\d*[1-9])?$/.test(this.num))) {
              this.msg = '请输入正确的' + this.text
              return false;
            }
          }
          if(this.num < 0){
            this.msg = '请输入正确的' + this.text
            return false;
          }
          if(this.limitMax){
            if(this.num > this.limitMax){
              this.msg = this.text + '超出设置范围'
              return false;
            }

          }
          if(this.limitMin){
            if(this.num < this.limitMin){
              this.msg = this.text + '最小为' + this.limitMin
              return false;
            }
          }
          return true
        }else{
          if (this.num == '') {
            this.msg = '请输入' + this.text
            return false;
          }
          if (!(/^[0-9]*[1-9][0-9]*$/.test(this.num))) {
            this.msg = '请输入正确的' + this.text
            return false;
          }
          return true
        }
      },
      alertMsg() {
        if (!this.verification()) {
          this.$notify.error({
            title: '提示',
            message: this.msg
          });
          if(this.isNeedErrorNum){
            this.$emit('input', this.num)
          }
        } else {
          if(this.isBlurSave){
            this.$emit('input', this.num)
          }else{
            if (this.num != this.value) {
              if (this.setSortFun(this.obj, this.num)) {
                this.$emit('input', this.num)
              }
            }
          }
        }
      },
    },
  }
</script>

<style scoped lang="scss">
  input {
    width: 93px;
    height: 40px;
    background: rgba(245, 247, 253, 0.39);
    border: 1px solid #ABB6C3;
    opacity: 1;
    border-radius: 5px;
    padding-left: 10px;
  }

  input:focus {
    outline: none;
    border: 1px solid #409eff;
  }
</style>
