<template>
  <el-dialog
    title="添加银行卡"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="onClose"
    :append-to-body="true"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="160px"
    >
      <el-form-item label="银行名称：" prop="bankName">
        <el-input v-model="dataForm.bankName" placeholder="请输入银行名称"></el-input>
      </el-form-item>

      <el-form-item label="银行开户支行：" prop="openingBank">
        <el-input v-model="dataForm.openingBank" placeholder="请输入银行开户支行"></el-input>
      </el-form-item>

      <el-form-item label="银行卡账号：" prop="cardNo">
        <el-input v-model="dataForm.cardNo" placeholder="请输入银行卡账号"></el-input>
      </el-form-item>

      <el-form-item label="银行卡户主：" prop="recipientName">
        <el-input v-model="dataForm.recipientName" placeholder="请输入银行卡账号持有人姓名"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        openingBank: '',  // 银行开户支行
        cardNo: '',  // 银行卡账号
        bankName: '',
        recipientName: '',  // 账号持有人
        ahPhoneNum: '' // 账号持有人手机号
      },
      dataRule: {
        bankName: [
          { required: true, message: '请输入银行名称', trigger: 'blur' }
        ],
        cardNo: [
          { required: true, message: '请输入银行卡账号', trigger: 'blur' }
        ],
        recipientName: [
          { required: true, message: '请输入银行卡账号持有人姓名', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
    },

    onClose () {
      this.visible = false
      this.$emit('refreshCardList')
      console.log(this.visible)
    },

    /**
     * 提交表单
     */
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/shop/shopBankCard'),
            method: 'post',
            data: this.$http.adornData(this.dataForm)
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshCardList')
              }
            })
            this.dataForm.openingBank = ''
            this.dataForm.cardNo = ''
            this.dataForm.bankName = ''
            this.dataForm.recipientName = ''
            this.dataForm.ahPhoneNum = ''
          })
        }
      })
    }
  }

}
</script>

<style>
.el-input {
  width: 80%;
}
</style>