<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-form-item>
      <el-form-item label="验证码" prop="code">
        <GraphicVerificationCode
          :phone="formData.phone"
          @input="(e) => formData.code = e"
        ></GraphicVerificationCode>
      </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 GraphicVerificationCode from '@/components/GraphicVerificationCode/index.vue'
import { bankDelete } from '@/api/account'

export default {
  name: 'UnBindBank',
  components: {
    GraphicVerificationCode
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '620px',
        title: ''
      },
      visible: false,
      formData: {
        phone: '',
        code: ''
      },
      formRules: {
        phone: [ { required: true, message: '缺少注册手机号' } ],
        code: [ { required: true, message: '请输入验证码' } ]
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    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>

