<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form='form'>

      <a-form-item v-show='false'>
        <a-input v-decorator="['id']" type='hidden' />
        <a-input v-decorator="['repairId']" type='hidden' />
      </a-form-item>

      <row-list :col='1'>
        <row-item>
          <a-form-item
            label='维修单号'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              disabled='disabled'
              v-decorator="['repairNo']" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="方案描述"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-textarea
              :rows="4"
              v-decorator="['opinion', {rules: [{required: true, message: '故障描述不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="方案图片"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-upload
              :action="uploadUrl"
              :multiple="true"
              list-type="picture"
              :file-list="this.defaultApplicationFileList"
              @change="handleApplicationFileChange"
              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.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              v-decorator="['imageList', {rules: [{required: true, message: '维修意见图不能为空'}]}]" />
          </a-form-item>
        </row-item>-->
        <row-item>
          <a-form-item
            label="维修方案"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-upload
              :action="uploadUrl"
              :multiple="true"
              :file-list="defaultFileList"
              @change="handleFileChange"
              :headers="headers"
            >
              <a-button> <a-icon type="upload" /> 上传文件 </a-button>
            </a-upload>
          </a-form-item>

<!--          <a-form-item
            label='维修方案文件'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              v-decorator="['opinionFile', {rules: [{required: true, message: '维修方案文件不能为空'}]}]" />
          </a-form-item>-->
        </row-item>
        <!--<row-item>
          <a-form-item
            label='状态保留字段'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input-number
              style='width: 100%'
              :min='0'
              :formatter='BaseTool.Amount.formatter'
              :parser='BaseTool.Amount.parser'
              v-decorator="['status', {rules: [{required: true, message: '状态保留字段不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label='删除标识 0 正常 1 删除'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input-number
              style='width: 100%'
              :min='0'
              :formatter='BaseTool.Amount.formatter'
              :parser='BaseTool.Amount.parser'
              v-decorator="['deleteFlag', {rules: [{required: true, message: '删除标识 0 正常 1 删除不能为空'}]}]" />
          </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 pick from 'lodash.pick'
import { addRepairScheme, updateRepairScheme } from '@/api/repair/repair'
import { uploadUrl } from '@/api/upms/file'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'

export default {
  name: 'BaseRepairScheme',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      uploadUrl: uploadUrl,
      defaultApplicationFileList: [],
      applicationFileList: [],
      defaultFileList: [],
      fileList: [],
      headers: {
        Authorization: 'Bearer ' + Vue.ls.get(ACCESS_TOKEN)
      }
    }
  },
  props: {},
  components: {
    uploadUrl
  },
  created () {
    // 下拉框map
  },
  methods: {
    base (record) {
      this.visible = true
      this.defaultFileList = []
      this.applicationFileList = []
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'repairId',
          'opinion',
          'imageList',
          'opinionFile',
          'status',
          'deleteFlag',
          'repairNo'
        ])))
      })
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '维修方案'
        return
      }
      this.modalTitle = '编辑'
      this.defaultApplicationFileList = this.BaseTool.UPLOAD.transImg(record.imageList)
      this.defaultFileList = this.BaseTool.UPLOAD.transImg(record.opinionFile)
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        values.imageList = this.applicationFileList // 维修方案图片
        values.opinionFile = this.fileList // 维修方案文件
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          addRepairScheme(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateRepairScheme(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      this.defaultApplicationFileList = []
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      } else {
        this.$emit('ok')
      }
    },
    handleApplicationFileChange (info) {
      this.defaultApplicationFileList = info.fileList
      this.applicationFileList = this.setFileList(info, 11)
    },
    handleFileChange (info) {
      this.defaultFileList = info.fileList
      this.fileList = this.setFileList(info, 19)
    },
    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 []
      }
    }
  }
}
</script>
