<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
    append-to-body
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="formRules"
      size="mini"
      label-suffix=":"
      label-width="150px"
    >
      <el-form-item label="注册手机号" prop="phone">
        <el-input v-model="formData.phone" maxlength="11" placeholder="注册手机号" disabled style="width: 120px;" />
        <el-button style="margin-left: 10px;" type="primary" :loading="codeLoading" @click="handleSendCode(formData.phone)">
          <span v-if="!codeLoading">获取验证码</span>
          <span v-else>{{ codeCount }} s</span>
        </el-button>
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input
          v-model="formData.code" oninput="value=value.replace(/[^\d]/g,'')" maxlength="6"
          style="width: 120px;"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSubmit">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { getCode, bankDelete } from '@/api/account'

export default {
  name: 'UnBindBank',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '620px',
        title: ''
      },
      visible: false,
      formData: {
        phone: '',
        code: ''
      },
      formRules: {
        phone: [ { required: true, message: '缺少注册手机号' } ],
        code: [ { required: true, message: '请输入验证码' } ]
      },
      codeTimer: '',
      codeLoading: false,
      codeCount: ''
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    // 获取验证码
    async handleSendCode(phone) {
      if (phone === '') {
        this.$message.error('请填写电话号码')
        return
      } else if (!/^1[3-9]\d{9}$/.test(phone)) {
        this.$message({
          message: '请输入正确手机号',
          type: 'warning'
        })
        return false
      }
      if (!this.codeTimer) {
        const res = await getCode({ phone })
        if (res.code === '') {
          this.$message({
            message: '发送成功，请注意查看手机短信',
            type: 'success'
          })
        }
        this.codeLoading = true
        this.codeCount = 60
        this.codeTimer = setInterval(() => {
          if (this.codeCount > 0 && this.codeCount <= 60) {
            this.codeCount--
          } else {
            clearInterval(this.codeTimer) // 清除定时器
            this.codeTimer = null
            this.codeLoading = false
          }
        }, 1000)
      }
    },
    initList() {
    },
    handleOpen(params = {}) {
      this.modalOptions.title = '解绑银行卡'
      this.formData.phone = params.phone
      // this.formData = Object.assign(this.$options.data().formData, params)
      this.visible = true
      this.initList()
      this.$refs.formData && this.$refs.formData.resetFields()
    },
    handleSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          const loading = this.$loading({ text: '加载中' })
          try {
            const { ...otps } = this.formData
            const params = {
              ...otps
            }
            await bankDelete(params)
            loading.close()
            this.$message({ message: `解绑成功!`, type: 'success' })
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    }
  }
}
</script>

