<template>
  <el-dialog
    :title="!dataForm.loanId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="借款人id" prop="workerId">
      <el-input v-model="dataForm.workerId" placeholder="借款人id"></el-input>
    </el-form-item>
    <el-form-item label="借款人姓名" prop="woekerName">
      <el-input v-model="dataForm.woekerName" placeholder="借款人姓名"></el-input>
    </el-form-item>
    <el-form-item label="借款金额" prop="amount">
      <el-input v-model="dataForm.amount" placeholder="借款金额"></el-input>
    </el-form-item>
    <el-form-item label="借款时间" prop="loanTime">
      <el-input v-model="dataForm.loanTime" placeholder="借款时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除 0-代表正常 1-代表删除" prop="status">
      <el-input v-model="dataForm.status" placeholder="是否删除 0-代表正常 1-代表删除"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createName">
      <el-input v-model="dataForm.createName" 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: {
          loanId: 0,
          workerId: '',
          woekerName: '',
          amount: '',
          loanTime: '',
          status: '',
          createTime: '',
          updateTime: '',
          createName: ''
        },
        dataRule: {
          workerId: [
            { required: true, message: '借款人id不能为空', trigger: 'blur' }
          ],
          woekerName: [
            { required: true, message: '借款人姓名不能为空', trigger: 'blur' }
          ],
          amount: [
            { required: true, message: '借款金额不能为空', trigger: 'blur' }
          ],
          loanTime: [
            { required: true, message: '借款时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '是否删除 0-代表正常 1-代表删除不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          createName: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.loanId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.loanId) {
            this.$http({
              url: this.$http.adornUrl(`/business/businessloan/info/${this.dataForm.loanId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.workerId = data.businessLoan.workerId
                this.dataForm.woekerName = data.businessLoan.woekerName
                this.dataForm.amount = data.businessLoan.amount
                this.dataForm.loanTime = data.businessLoan.loanTime
                this.dataForm.status = data.businessLoan.status
                this.dataForm.createTime = data.businessLoan.createTime
                this.dataForm.updateTime = data.businessLoan.updateTime
                this.dataForm.createName = data.businessLoan.createName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/businessloan/${!this.dataForm.loanId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'loanId': this.dataForm.loanId || undefined,
                'workerId': this.dataForm.workerId,
                'woekerName': this.dataForm.woekerName,
                'amount': this.dataForm.amount,
                'loanTime': this.dataForm.loanTime,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'createName': this.dataForm.createName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
