<template>
  <a-modal
    :title="modalTitle"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <a-form-item
        label="上传文件"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-upload :fileList="fileList" @change="handleChange" :remove="handleRemove" :beforeUpload="beforeUpload">
          <a-button> <a-icon type="upload" />选择上传文件</a-button>
        </a-upload>
      </a-form-item>
    </a-form>
    <p style="color: red">注意事项：<br/>
      1：请确保备件一级-二级-三级类型已经入库<br/>
      2：请确保仓库id已经填写，仓库id请查看仓库详情ID字段<br/>
      3：设备型号已经存在bom的，将不导入，以防止不同项目部的备件重复录入，如有需要，请通过入库的方式导入到仓库中<br/>
      4：设备型号如果提示找不到，请先添加再导入，或者检查型号或者名称是否有误，从而导致系统找不到<br/>
    </p>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">确定</a-button>
    </template>
  </a-modal>
</template>

<script>
import { importSparePartInfo } from '@/api/sqarepartmanage/sparepartinfo'

export default {
  name: 'SparePartInfoImportForm',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      useCompany: null,
      useProject: null,
      fileList: []
    }
  },
  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(`请上传正确的excel文件`)
          reject(new Error('请上传正确的excel文件'))
          return false
        }
      })
    },
    handleChange (info) {
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList)
      }
      if (info.file.status === 'done') {
        this.$message.success(`${info.file.name} file uploaded successfully`)
      } else if (info.file.status === 'error') {
        this.$message.error(`${info.file.name} file upload failed.`)
      }
    },
    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])
        importSparePartInfo(formData)
          .then((res) => {
            this.$message.success(res.data)
            this.handleCancel(values)
          }).catch(() => {
            this.confirmLoading = false
          })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.fileList = []
      this.form.resetFields()
      this.storeId = null
      this.$emit('ok', values)
    }

  }
}
</script>
