<template>
  <div class="input-ver">
    <el-input v-model="code" @change="$emit('change', code)" placeholder="请输入验证码"></el-input>
    <el-button type="text" @click="run">{{text}}</el-button>
  </div>
</template>

<script>
let running = false
let time = 60
let text = '获取验证码'
let interval

export default {
  data() {
    return {
      // 验证码
      code: '',
      time: time + 1,
    }
  },
  computed: {
    text() {
      if(this.time < 1 || this.time > time) {
        running = false
        if(this.time < 1) {
          clearInterval(interval)
          this.time = time + 1
          running = false
        }
        return text
      }
      return `重新获取(${this.time}s)`
    }
  },
  mounted() {

  },
  methods: {
    run() {
      if(running) return
      this.$emit('click', () => {
        running = true
        this.time--
        interval = setInterval(() => {
          this.time--
        }, 1000)
      })
    },
  }
}
</script>

<style scoped>
.input-ver {
  position: relative;
}
.el-button {
  position: absolute;
  top: 0;
  right: 10px;
}
</style>
