<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="['remark', {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="defaultVerifyImages"
              @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>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { examine, returnRepair } from '@/api/repair/application-form'
import { uploadUrl } from '@/api/upms/file'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'

export default {
  name: 'BaseRepairApplicationForm',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      statusMap: {},
      model: null,
      headers: {
        Authorization: 'Bearer ' + Vue.ls.get(ACCESS_TOKEN)
      },
      uploadUrl: uploadUrl,
      defaultVerifyImages: [],
      verifyImages: [] // 图片,
    }
  },
  components: {
  },
  props: {
  },
  created () {
    // 下拉框map
  },
  methods: {
    base (record, type) {
      this.visible = true
      this.model = record
      this.verifyImages = []
      this.defaultVerifyImages = []
      this.model.type = type
      const { form: { setFieldsValue } } = this
      // 日期处理
      record.applyTime = this.BaseTool.Moment(record.applyTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id'
        ])))
      })
      if (type === 1) {
        this.modalTitle = '报修审核驳回'
      } else if (type === 2) {
        this.modalTitle = '提交报修审核'
      }
    },
    handleRepairFileChange (info) {
      this.defaultVerifyImages = info.fileList
      this.verifyImages = 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.verifyImages = this.verifyImages
        // 日期处理
        if (this.model.type === 1) {
          returnRepair(values)
            .then(() => {
              this.$message.info('已驳回维修，2s后返回【报修审核】')
              this.handleCancel(values)
              /* setTimeout(() => {
                self.$router.push({ path: '/repair/caller/check' })
              }, 2000) */
            }).catch(() => {
              this.confirmLoading = false
            })
        } else if (this.model.type === 2) {
          examine(values).then(() => {
            this.$message.info('已提交，请等待审核')
            this.handleCancel(values)
            /* setTimeout(() => {
              self.$router.push({ path: '/repair/manager/check' })
            }, 1000) */
          }).catch(() => {
            this.confirmLoading = false
          })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok')
      } else {
        this.$emit('ok')
      }
    }
  }
}
</script>
