<template>
<div>
  <el-form size="middle" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
           label-width="150px">
    <el-row  :gutter="0">
      <el-col :span="12">
        <el-form-item label="合同名称" prop="name">
          <el-input :disabled="true" v-model="inputForm.name"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="12">
        <el-form-item label="合同编号" prop="no">
          <el-input :disabled="true" v-model="inputForm.no"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="12">
        <el-form-item label="客户名称" prop="clientName">
          <el-input :disabled="true" v-model="inputForm.clientName"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="12">
        <el-form-item label="借用人" prop="borrowName">
          <el-input :disabled="true" v-model="inputForm.borrowName"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="12">
        <el-form-item label="借用日期" prop="borrowData" :rules="[
                  {required: true, message:'请填写借用日期', trigger:'blur'}
               ]">
          <el-date-picker
            style="width: 100%"
            :disabled="this.inputForm.borrowType === '2'"
            v-model="inputForm.borrowData"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-col>

      <el-col :span="12">
        <el-form-item label="大概归还日期" prop="borrowRetData" :rules="[
                  {required: true, message:'请填写大概归还日期', trigger:'blur'}
               ]">
          <el-date-picker
            style="width: 100%"
            :disabled="this.inputForm.borrowType === '2'"
            v-model="inputForm.borrowRetData"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-col>

    </el-row>

    <el-form-item label="备注" prop="remarks">
      <el-input v-model="inputForm.remarks"
                :disabled="this.inputForm.borrowType === '2'"
                type="textarea"
                :rows="5"
                maxlength="500"
                placeholder="请输入备注"
                show-word-limit>
      </el-input>
    </el-form-item>

  </el-form>
</div>
</template>

<script>
  import WorkContractService from '@/api/sys/WorkContractService'
  import WorkContractBorrowService from '@/api/sys/WorkContractBorrowService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          name: '',
          no: '',
          clientName: '',
          borrowName: '',
          borrowData: '',
          borrowRetData: '',
          remarks: '',
          borrowType: '',
          type: ''
        }
      }
    },
    props: {
      businessId: {
        type: String,
        default: ''
      },
      formReadOnly: {
        type: Boolean,
        default: false
      }
    },
    components: {
    },
    workContractBorrowService: null,
    workContractService: null,
    created () {
      this.workContractBorrowService = new WorkContractBorrowService()
      this.workContractService = new WorkContractService()
    },
    watch: {
      'businessId': {
        handler (newVal) {
          if (this.businessId) {
            this.init(this.businessId)
          } else {
            this.$nextTick(() => {
              this.$refs.inputForm.resetFields()
            })
          }
        },
        immediate: true,
        deep: false
      }
    },
    computed: {
      userName () {
        return JSON.parse(localStorage.getItem('user')).name
      }
    },
    methods: {
      init (id) {
        if (id) {
          this.loading = true
          this.inputForm.id = id
          this.$nextTick(() => {
            console.log('id', id)
            this.$refs.inputForm.resetFields()
            this.workContractBorrowService.findById(this.inputForm.id).then(({data}) => {
              if (this.commonJS.isEmpty(data.id)) {
                this.workContractService.findById(this.inputForm.id).then(({data}) => {
                  this.inputForm = this.recover(this.inputForm, data)
                  this.inputForm.borrowName = this.userName
                  this.inputForm.borrowData = new Date()
                  this.loading = false
                })
              } else {
                this.inputForm = this.recover(this.inputForm, data)
                this.inputForm.borrowName = this.userName
                this.inputForm.borrowData = new Date()
                this.loading = false
              }
            })
          })
        }
      },
      // 表单提交
      startForm (callback) {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            let id = this.inputForm.id
            this.loading = true
            this.inputForm.contractInfoId = id
            this.inputForm.borrowType = '2'
            console.log('dosubmit', this.inputForm)
            this.workContractBorrowService.save(this.inputForm).then(({data}) => {
              callback(data.businessTable, data.businessId, this.inputForm)
              this.$refs.inputForm.resetFields()
              this.loading = false
            }).catch(() => {
              this.$refs.inputForm.resetFields()
              this.loading = false
            })
          }
        })
      },
      // 同意
      agreeForm (callback) {
        this.inputForm.borrowType = '5'
        this.workContractBorrowService.updateStatusById(this.inputForm).then(() => {
          this.workContractBorrowService.updateMessageStatusById(this.inputForm).then(({data}) => {
            callback(data.businessTable, data.businessId, this.inputForm)
            this.$refs.inputForm.resetFields()
            this.loading = false
          }).catch(() => {
            this.$refs.inputForm.resetFields()
            this.loading = false
          })
        }).catch(() => {
          this.$refs.inputForm.resetFields()
          this.loading = false
        })
      },
      // 关闭
      close () {
        this.$refs.inputForm.resetFields()
        this.visible = false
        this.businessId = ''
      },
      // 更改状态
      updateStatusById (type) {
        console.log('updateStatusById', this.inputForm)
        if (type === 'agree') {
          this.inputForm.borrowType = '5'
          this.workContractBorrowService.updateStatusById(this.inputForm)
        }
        if (type === 'reject') {
          this.inputForm.borrowType = '0'
          this.workContractBorrowService.updateStatusById(this.inputForm).then(() => {
            this.inputForm.borrowType = '3'
            this.inputForm.type = 'reject'
            this.workContractBorrowService.updateMessageStatusById(this.inputForm)
          })
        }
        if (type === 'reback') {
          this.inputForm.borrowType = '0'
          this.workContractBorrowService.updateStatusById(this.inputForm)
        }
      }
    }
  }
</script>


