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

      <el-col :span="12">
        <el-form-item label="报告文号" prop="reportNo">
          <el-input :disabled="true" v-model="inputForm.reportNo"></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>
	<el-tabs v-model="activeName" type="border-card" @tab-click="tabHandleClick">
		<el-tab-pane label="附件" name="files">
			<!--        附件-->
			<UpLoadComponent ref="uploadComponent"></UpLoadComponent>
		</el-tab-pane>

	</el-tabs>
</div>
</template>

<script>
	import UpLoadComponent from '@/views/common/UpLoadComponentV3.1'
	import ProjectReportArchiveService from '@/api/cw/projectRecords/ProjectReportArchiveService'
  import ProjectReportBorrowService from '@/api/cw/projectRecords/ProjectReportBorrowService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
		  activeName: 'files',
        inputForm: {
			workAttachmentDtoList: [],
          id: '',
			projectName: '',
			reportNo: '',
          borrowName: '',
          borrowData: '',
          borrowRetData: '',
          remarks: '',
          borrowType: '',
          type: ''
        },
      }
    },
    props: {
      businessId: {
        type: String,
        default: ''
      },
      formReadOnly: {
        type: Boolean,
        default: false
      }
    },
    components: {
		UpLoadComponent,
    },
	  projectReportBorrowService: null,
	  projectReportArchiveService: null,
    created () {
      this.projectReportBorrowService = new ProjectReportBorrowService()
      this.projectReportArchiveService = new ProjectReportArchiveService()
    },
    watch: {
      'businessId': {
        handler (newVal) {
          if (this.businessId) {
            this.init(this.businessId)
          } else {
            this.$nextTick(() => {
              this.$refs.inputForm.resetFields()
            })
          }
        },
        immediate: true,
        deep: false
      },
		'loading': {
			handler (newVal) {
				console.log(newVal)
				this.$emit('changeLoading', newVal)
				this.$refs.uploadComponent.changeLoading(newVal)
			}
		}
    },
    computed: {
		bus: {
			get () {
				this.$refs.uploadComponent.setDividerName('附件', false)
				return this.businessId
			},
			set (val) {
				this.businessId = val
			}
		},
      userName () {
        return this.$store.state.user.name
      }
    },
    methods: {
		tabHandleClick (event) {
			// console.log(event)
		},
      init (id) {
		  this.activeName = 'files'
        if (id) {
          this.loading = true
          this.inputForm.id = id
          this.$nextTick(() => {
            this.$refs.inputForm.resetFields()
            this.projectReportBorrowService.findById(this.inputForm.id).then((data) => {
            	console.log('data',data)
				this.$refs.uploadComponent.clearUpload()
              if (this.commonJS.isEmpty(data.id)) {
				  this.projectReportArchiveService.queryById(this.inputForm.id).then((data) => {
                  this.inputForm = this.recover(this.inputForm, data)
					  this.inputForm.projectName = data.cwProjectRecordsDTO.projectName
					if (this.commonJS.isEmpty(this.inputForm.borrowName)){
						this.inputForm.borrowName = this.userName
					}
                  this.inputForm.borrowData = this.moment(new Date()).format('YYYY-MM-DD')
					  this.$refs.uploadComponent.newUpload('view', this.inputForm.workAttachmentDtoList, 'cw_project_archive', null, null, null, null, false)
                  this.loading = false
                })
              } else {
				  this.inputForm = this.recover(this.inputForm, data)
				  // this.inputForm.projectName = data.cwProjectRecordsDTO.projectName
				  // if (this.commonJS.isEmpty(this.inputForm.borrowName)){
					//   this.inputForm.borrowName = this.userName
				  // }
				  // this.inputForm.borrowData = this.moment(new Date()).format('YYYY-MM-DD')
				  this.$refs.uploadComponent.newUpload('view', this.inputForm.workAttachmentDtoList, 'cw_project_archive', null, null, null, null, false)
				  this.loading = false
			  }
            })
          })
        }
      },
      // 表单提交
      startForm (callback) {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            let id = this.inputForm.id
            this.loading = true
            this.inputForm.contractInfoId = id
            var date1 = new Date(this.inputForm.borrowData)
            var date2 = new Date(this.inputForm.borrowRetData)
            if ((date2 - date1) < 0) {
              this.loading = false
              this.$message.error('大概归还日期要比借用日期晚')
              throw new Error('大概归还日期要比借用日期晚')
            } else {
              this.inputForm.borrowType = '2'
              this.projectReportBorrowService.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.projectReportBorrowService.updateMessageStatusById(this.inputForm).then((data) => {
          this.projectReportBorrowService.updateStatusById(this.inputForm)
          callback(data.businessTable, data.businessId, this.inputForm)
          this.$refs.inputForm.resetFields()
          this.loading = false
        }).catch(() => {
          this.$refs.inputForm.resetFields()
          this.loading = false
        })
      },
      // 关闭
      close () {
		  this.$refs.uploadComponent.clearUpload()
        this.$refs.inputForm.resetFields()
        this.visible = false
      },
      // 更改状态
      updateStatusById (type) {
        if (type === 'agree') {
          this.inputForm.borrowType = '5'
          this.projectReportBorrowService.updateStatusById(this.inputForm)
        }
        if (type === 'reject') {
          this.inputForm.borrowType = '0'
          this.projectReportBorrowService.updateStatusById(this.inputForm).then(() => {
            this.inputForm.borrowType = '3'
            this.inputForm.type = 'reject'
            this.projectReportBorrowService.updateMessageStatusById(this.inputForm)
          })
        }
        if (type === 'reback') {
          this.inputForm.borrowType = '0'
          this.projectReportBorrowService.updateStatusById(this.inputForm)
          // this.workContractBorrowService.updateStatusById(this.inputForm)
        }
      }
    }
  }
</script>


