<template>

  <el-button type="primary" style="margin-left: 10px;" :loading="sending" :disabled="count != 60" @click="sendSms">{{sendTxt}}</el-button>

</template>

<script>

  import { sendSms } from '@/api/financial/wallet.js'

  export default {
    props: {
      phone: {
        type: String,
        default: ''
      },
      type: String
    },
    data() {
      return {
        sent: false,
        loading: false,
        sending: false,
        count: 60
      }
    },
    computed: {
      sendTxt() {
        if(this.sending) {
          return '发送中'
        }
        let counting = this.count != 60
        return counting ? ('重发' + this.count + '秒') : '发送验证码'
      },
    },
    methods: {
      sendSms() {
        if(this.count == 60 && !this.sending) {
          this.sending = true
          sendSms({type: this.type})
          .then(r => {
            this.sending = false
            this.sent = true
            this.$emit('sent')
            this.doTick()
          })
          .catch(e => {
            this.sending = false
            this.error('发送失败!')
          })
        }
      },
      doTick() {
        if(this.count == 0) {
          this.count = 60
        } else {
          this.count--;
          setTimeout(this.doTick, 1000)
        }
      },
    }
  }

</script>

<style scoped>

</style>
