<template>
  <Poptip :transfer="true" @on-popper-hide="popperHide" @on-popper-show="popperShow">
    <Input v-model="currentValue" :placeholder="placeholder" @on-change="onChange" />
    <div class="content" slot="content">
      <div class="row">
        <div class="col" @click="handle(1)">1</div>
        <div class="col" @click="handle(2)">2</div>
        <div class="col" @click="handle(3)">3</div>
      </div>
      <div class="row">
        <div class="col" @click="handle(4)">4</div>
        <div class="col" @click="handle(5)">5</div>
        <div class="col" @click="handle(6)">6</div>
      </div>
      <div class="row">
        <div class="col" @click="handle(7)">7</div>
        <div class="col" @click="handle(8)">8</div>
        <div class="col" @click="handle(9)">9</div>
      </div>
      <div class="row">
        <div class="col" @click="handle(0)">0</div>
        <div class="col" @click="handle('.')">.</div>
        <div class="col" @click="handle('del')">
          <Icon type="ios-backspace" />
        </div>
      </div>
    </div>
  </Poptip>
</template>

<script>
export default {
  name: 'my-number-board-input',
  data () {
    return {
      currentValue: this.value
    }
  },
  props: {
    placeholder: {
      type: String,
      default: ''
    },
    min: {
      type: Number,
      default: 1
    },
    value: {
      type: Number | String
    }
  },
  computed: {
    bindNum () {
      console.log(this.value + '--------')
      return this.value
    }
  },
  methods: {
    popperShow () {
      if (this.currentValue === 1 || this.currentValue === 0) {
        this.currentValue = ''
      }
    },
    popperHide () {
      if (!this.currentValue) {
        this.currentValue = 1
      }
    },
    handle (value) {
      if (value === 'del') {
        this.currentValue = this.currentValue.substring(0, this.currentValue.length - 1)
      } else if (value === '.') {
        if (this.currentValue.length < 1 || this.currentValue.indexOf('.') !== -1) {
          return false
        } else {
          this.currentValue = this.currentValue + '' + value
        }
      } else {
        this.currentValue = this.currentValue + '' + value
      }
      this.$emit('input', Number.parseFloat(this.currentValue))
      this.$emit('on-change', Number.parseFloat(this.currentValue))
    },
    onChange (number) {
      this.$emit('input', number)
      this.$emit('on-change', number)
    }
  }
}
</script>

<style lang="less" scoped>
  .content {
    padding: 10px;
    .row {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 300px;
      margin-bottom: 10px;
      &:last-child {
        margin-bottom: 0;
      }
      .col {
        flex: 1;
        overflow: hidden;
        margin-right: 10px;
        height: 60px;
        line-height: 60px;
        text-align: center;
        background: #555555;
        color: #FFFFFF;
        border-radius: 5px;
        font-size: 30px;
        font-weight: bold;
        cursor: pointer;
        &:last-child {
          margin-right: 0;
        }
        &:active {
          background: #d7d7d7;
          color: #555555;
        }
      }
    }
  }
</style>
