<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="100px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="订单编号" prop="tradeNo">
            <el-input v-model="form.tradeNo" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="初始开户行" prop="firstBankType">
            <el-input v-model="form.firstBankType" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="初始卡号" prop="firstBackCardNum">
            <el-input v-model="form.firstBackCardNum" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="开户行" prop="bankType">
            <el-input v-model="form.bankType" placeholder="请输入开户行" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="银行卡号" prop="backCardNum">
            <el-input v-model="form.backCardNum" placeholder="请输入银行卡号" />
          </el-form-item>
        </el-col>
      </el-row>
    </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 ImageUpload from '@/components/ImageUpload/index'
  import { getBorrow, updateBank, updateBorrow } from '@/api/dk/borrow'
  import { bankType } from '@/api/dk/BorrowStatus'

  export default {
    components: {
      ImageUpload,
    },
    data () {
      return {
        open: false,
        title: '',
        form:{
          id: undefined,
          tradeNo: undefined,
          firstBankType: undefined,
          firstBackCardNum: undefined,
          bankType: undefined,
          backCardNum: undefined,
        },
        bankTypeList:[],
        rules:{
          bankType: [
            { required: true, message: '不能为空', trigger: 'blur' },
          ],
          backCardNum: [
            { required: true, message: '不能为空', trigger: 'blur' },
          ],
        },
        buttonLoading:false,
      }
    },
    created() {
      bankType().then(response => {
        this.bankTypeList = response.data
      })
    },
    methods: {
      init (id) {
        this.form.id = id || undefined;
        this.open = true;
        this.$nextTick(() => {
          this.$refs['form'].resetFields();
          getBorrow(id).then(response => {
            this.form.id = response.data.id;
            this.form.tradeNo = response.data.tradeNo;
            this.form.firstBankType = response.data.firstBankType;
            this.form.firstBackCardNum = response.data.firstBackCardNum;
            this.form.bankType = response.data.bankType;
            this.form.backCardNum = response.data.backCardNum;
            this.open = true;
            this.title = "修改银行信息";
          });
        })
      },
      // 表单提交
      submitForm () {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.buttonLoading = true;
            updateBank({
              id: this.form.id,
              bankType: this.form.bankType,
              backCardNum: this.form.backCardNum,
            }).then(data => {
                this.msgSuccess("修改成功");
                this.buttonLoading = false;
                this.open = false
                this.$emit('refreshDataList')
            }).catch(() => {
                this.buttonLoading = false;
            });
          }
        })
      },
    }
  }
</script>
