<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="2">
        <row-item>
          <a-form-item
            label="维修人"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled='disabled'
              v-decorator="['createdUserName']"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="维修时间"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-date-picker
              showTime
              disabled='disabled'
              v-decorator="['createdTime']"
              style="width: 100%"
              :format="BaseTool.Date.PICKER_NORM_DATETIME_PATTERN"
            />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="维修结束时间"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-date-picker
              showTime
              disabled='disabled'
              v-decorator="['updateTime']"
              style="width: 100%"
              :format="BaseTool.Date.PICKER_NORM_DATETIME_PATTERN"
            />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="维修时长"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled='disabled'
              v-decorator="['repairHours']"/>
          </a-form-item>
        </row-item>
      </row-list>
      <row-list :col="1">
        <row-item>
          <a-form-item
            label="主管建议"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea v-decorator="['opinion']"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="维修描述"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea v-decorator="['dataRemark']"/>
          </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="defaultRepairFileList"
              @change="handleRepairFileChange"
              accept="image/*"
              :headers="headers"
            >
              <a-button> <a-icon type="upload" /> 上传图片 </a-button>
            </a-upload>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <spare-part-used-select-modal ref="sparePartUsedSelectModal" />
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { uploadUrl } from '@/api/upms/file'
import { updateRepairRecord } from '@/api/customize/fieldTemplateData'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'

export default {
  name: 'BaseRepairApplicationForm',
  data () {
    return {
      model: null,
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      headers: {
        Authorization: 'Bearer ' + Vue.ls.get(ACCESS_TOKEN)
      },
      uploadUrl: uploadUrl,
      defaultRepairFileList: [],
      repairFileList: [] // 维修图片,
    }
  },
  components: {
  },
  props: {
  },
  created () {
    // 下拉框map
  },
  methods: {
    base (record) {
      this.visible = true
      this.model = record
      // 如果是空标识添加
      this.modalTitle = '维修编辑'
      this.repairFileList = record.imageList
      this.defaultRepairFileList = this.BaseTool.UPLOAD.transImg(this.repairFileList)
      const { form: { setFieldsValue } } = this
      // 日期处理
      record.createdTime = this.BaseTool.Date.formatter(record.createdTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
      record.updateTime = this.BaseTool.Date.formatter(record.updateTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'createdTime',
          'updateTime',
          'createdUserName',
          'repairHours',
          'dataRemark',
          'opinion'
        ])))
      })
    },
    handleRepairFileChange (info) {
      this.defaultRepairFileList = info.fileList
      this.repairFileList = this.setFileList(info, 12)
    },
    setFileList (info, type) {
      const file = info.file
      const fileList = info.fileList
      if (file.status === 'done') {
        return this.BaseTool.UPLOAD.getUploadFileDTO(fileList, type)
      } else if (file.status === 'removed') {
        return this.BaseTool.UPLOAD.getUploadFileDTO(fileList, type)
      } 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.imageList = this.repairFileList
        values.remark = values.dataRemark
        values.createdTime = this.BaseTool.Date.formatter(values.createdTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
        values.updateTime = this.BaseTool.Date.formatter(values.updateTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
        updateRepairRecord(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>
