<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" type="hidden"/>
      </a-form-item>

      <row-list :col="1">
        <row-item>
          <a-form-item
            label="维修情况"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea
              :rows="4"
              v-decorator="['finishRemark', {rules: [{required: true, message: '维修情况不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="上传图片"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-upload
              :action="uploadUrl"
              :multiple="true"
              list-type="picture"
              :file-list="defaultFinishFileList"
              @change="handleFinishFileChange"
              accept="image/*"
              :headers="headers"
            >
              <a-button> <a-icon type="upload" /> 上传图片 </a-button>
            </a-upload>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="上传附件"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-upload
              :action="uploadUrl"
              :multiple="true"
              :file-list="defaultAttachmentList"
              @change="handleAttachmentChange"
              :headers="headers"
            >
              <a-button> <a-icon type="upload" /> 上传附件 </a-button>
            </a-upload>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">完成</a-button>
    </template>
  </a-modal>
</template>

<script>
import { finishRepairEntrust } from '@/api/repair/entrust'
import { uploadUrl } from '@/api/upms/file'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'

export default {
  name: 'FinishOutForm',
  data () {
    return {
      model: null,
      confirmLoading: false,
      modalTitle: '委外完成',
      form: this.$form.createForm(this),
      visible: false,
      headers: {
        Authorization: 'Bearer ' + Vue.ls.get(ACCESS_TOKEN)
      },
      uploadUrl: uploadUrl,
      defaultFinishFileList: [],
      defaultAttachmentList: [],
      finishFileList: [],
      finishAttachmentList: []
    }
  },
  components: {
  },
  created () {
  },
  methods: {
    base (record) {
      this.visible = true
      this.model = record
      this.defaultFinishFileList = []
      this.defaultAttachmentList = []
      this.finishFileList = []
      this.finishAttachmentList = []
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue({
          id: record.id
        })
      })
    },
    handleFinishFileChange (info) {
      this.defaultFinishFileList = info.fileList
      this.finishFileList = this.setFileList(info)
    },
    handleAttachmentChange (info) {
      this.defaultAttachmentList = info.fileList
      this.finishAttachmentList = this.setFileList(info)
    },
    setFileList (info) {
      const file = info.file
      const fileList = info.fileList
      if (file.status === 'done') {
        return this.BaseTool.UPLOAD.getUploadFileDTO(fileList, 12)
      } else if (file.status === 'removed') {
        return this.BaseTool.UPLOAD.getUploadFileDTO(fileList, 12)
      } else if (file.status === 'error') {
        this.$message.error('上传失败')
        return []
      }
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        values.finishFileList = this.finishFileList
        values.finishAttachmentList = this.finishAttachmentList
        finishRepairEntrust(values.id, values).then(() => {
          this.handleCancel(values)
        }).catch(() => {
          this.confirmLoading = false
        })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok')
      }
    }
  }
}
</script>
