<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-button v-for="borrowStatus in borrowList" @click="borrowStatusChange(borrowStatus)">{{ borrowStatus.borrowName }}</el-button>
        </el-col>
      </el-row>
      <el-row>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="文字颜色" prop="borrowNameStyle">
            <el-input v-model="form.borrowNameStyle" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="借款状态" prop="borrowName">
            <el-input v-model="form.borrowName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="借款说明" prop="borrowRemark">
            <el-input v-model="form.borrowRemark" />
          </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, updateBorrow, updateBorrowStatus } from '@/api/dk/borrow'
  import { all } from '@/api/dk/BorrowStatus'

  export default {
    components: {
      ImageUpload,
    },
    data () {
      return {
        open: false,
        title: '',
        borrowList: [],
        form:{
          id: undefined,
          usedRemit: undefined,
          borrowNameStyle: undefined,
          borrowName: undefined,
          borrowRemark: undefined,
        },
        rules:{
          borrowName: [
            { required: true, message: '不能为空', trigger: 'blur' },
          ],
          borrowRemark: [
            { required: true, message: '不能为空', trigger: 'blur' },
          ],
        },
        buttonLoading:false,
      }
    },
    created() {
      all().then(response => {
        this.borrowList = 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.borrowNameStyle = response.data.borrowNameStyle;
            this.form.borrowName = response.data.borrowName;
            this.form.borrowRemark = response.data.borrowRemark;
            this.form.borrowStatusId = response.data.borrowStatusId;
            this.open = true;
            this.title = "修改状态";
          });
        })
      },
      // 表单提交
      submitForm () {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.buttonLoading = true;
            updateBorrowStatus({
              id: this.form.id,
              borrowNameStyle: this.form.borrowNameStyle,
              borrowName: this.form.borrowName,
              borrowRemark: this.form.borrowRemark,
              usedRemit: this.form.usedRemit,
              borrowStatusId: this.form.borrowStatusId
            }).then(data => {
                this.$modal.msgSuccess("修改成功");
                this.buttonLoading = false;
                this.open = false
                this.$emit('refreshDataList')
            }).catch(() => {
                this.buttonLoading = false;
            });
          }
        })
      },
      borrowStatusChange(borrowStatus){
        this.form.borrowNameStyle = borrowStatus.borrowNameStyle;
        this.form.borrowName = borrowStatus.borrowName;
        this.form.borrowRemark = borrowStatus.borrowRemark;
        this.form.usedRemit = borrowStatus.usedRemit;
        this.form.borrowStatusId = borrowStatus.id
      }
    }
  }
</script>
<style>
.borrowStatusList {
  color: red;
  border-style:solid;
  border-color:black;
  display: inline-block;
  white-space: nowrap;
}
</style>
