<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="totalLoanMoney">
            <el-input v-model="form.totalLoanMoney" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="还款月数" prop="totalMonth">
            <el-input v-model="form.totalMonth" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="月利率" prop="loanMonthRate">
            <el-input v-model="form.loanMonthRate" />
          </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, updateBorrowLoan } from '@/api/dk/borrow'

export default {
    components: {
      ImageUpload,
    },
    data () {
      return {
        open: false,
        title: '',
        form:{
          id: undefined,
          tradeNo: undefined,
          totalLoanMoney: undefined,
          totalMonth: undefined,
          loanMonthRate: undefined,
        },
        rules:{
          totalLoanMoney: [
            { required: true, message: '不能为空', trigger: 'blur' },
          ],
          totalMonth: [
            { required: true, message: '不能为空', trigger: 'blur' },
          ],
          loanMonthRate: [
            { required: true, message: '不能为空', trigger: 'blur' },
          ],
        },
        buttonLoading:false,
      }
    },
    created() {
    },
    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.totalLoanMoney = response.data.totalLoanMoney;
            this.form.totalMonth = response.data.totalMonth;
            this.form.loanMonthRate = response.data.loanMonthRate*100;
            this.open = true;
            this.title = "修改贷款信息";
          });
        })
      },
      // 表单提交
      submitForm () {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.buttonLoading = true;
            updateBorrowLoan({
              id: this.form.id,
              totalLoanMoney: this.form.totalLoanMoney,
              totalMonth: this.form.totalMonth,
              loanMonthRate: this.form.loanMonthRate,
            }).then(data => {
                this.$modal.msgSuccess("修改成功");
                this.buttonLoading = false;
                this.open = false
                this.$emit('refreshDataList')
            }).catch(() => {
                this.buttonLoading = false;
            });
          }
        })
      },
    }
  }
</script>
