<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-form-item>

      <a-form-item
        label="名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['name']" />
      </a-form-item>
      <a-form-item
        :label="conditionName"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['condition', {rules: [{required: true}]}]" placeholder="请选择">
          <a-select-option
            v-for="(value,index) in conditions"
            :key="index"
            :label="value"
            :value="value">{{ value }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="上传文件"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-upload :fileList="fileList" :remove="handleRemove" :beforeUpload="beforeUpload">
          <a-button> <a-icon type="upload" />选择上传文件</a-button>
        </a-upload>
      </a-form-item>
    </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 { uploadFile } from '@/api/upms/excel-import'

export default {
  name: 'BaseSysExcelImport',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      fileList: []
    }
  },
  props: {
    type: {
      type: Number,
      default: 1
    },
    conditionName: {
      type: String,
      default: ''
    },
    conditions: {
      type: Array,
      default: () => { }
    }
  },
  methods: {
    base () {
      this.visible = true
      this.modalTitle = '添加'
    },
    handleRemove (file) {
      const index = this.fileList.indexOf(file)
      const newFileList = this.fileList.slice()
      newFileList.splice(index, 1)
      this.fileList = newFileList
    },
    beforeUpload (file) {
      const reg = /\.(xls|xlsx)(\?.*)?$/
      return new Promise((resolve, reject) => {
        if (reg.test(file.name)) {
          this.fileList = [file]
          return false
        } else {
          this.$message.error(`请上传正确的xls|xlsx文件`)
          reject(new Error('请上传正确的xls|xlsx文件'))
          return false
        }
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        const formData = new FormData()
        formData.append('file', this.fileList[0])
        formData.append('type', this.type)
        formData.append('name', values.name ? values.name : '')
        formData.append('conditionInfo', values.condition)
        formData.append('id', values.id ? values.id : '')
        uploadFile(formData)
          .then(() => {
            this.handleCancel(values)
          }).catch(() => {
            this.confirmLoading = false
          })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      this.fileList = []
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    }

  }
}
</script>
