<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="账户号" prop="cardAccount">
        <el-input v-model="form.cardAccount" placeholder="请输入账户号" />
      </el-form-item>
      <el-form-item label="账户名称" prop="cardName">
        <el-input v-model="form.cardName" placeholder="请输入账户名称" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAccountBlack, getAccountBlack, updateAccountBlack} from "@/api/cai/accountBlack";

export default {
  components: {
  },
  data () {
    return {
      open: false,
      title: '',
      form:{
        id: undefined,
        cardAccount: undefined,
        cardName: undefined,
      },
      // 表单校验
      rules: {
        cardAccount: [
          { required: true, message: "银行名称不能为空", trigger: "blur" }
        ],
        cardName: [
          { required: true, message: "账户名称不能为空", trigger: "blur" }
        ],
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init (id) {
      this.form.id = id || undefined;
      this.title = (id ? "修改" : "新增") + "账户黑名单";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        if(this.form.id){
          getAccountBlack(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateAccountBlack(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.$modal.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else{
            addAccountBlack(this.form).then(data => {
              this.$modal.msgSuccess("新增成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }

        }
      })
    },
  }
}
</script>
