<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>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">确定</a-button>
    </template>
  </a-modal>
</template>

<script>
import { importUser } from '@/api/upms/user'

export default {
  name: 'UserImportForm',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      dept: {},
      fileList: []
    }
  },
  methods: {
    base (dept) {
      if (dept == null || !dept.deptId) {
        this.$message.info('请选择导入的部门')
        return
      }
      this.visible = true
      this.fileList = []
      this.modalTitle = '导入用户'
      this.dept = dept
    },
    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 () {
      if (!this.fileList || this.fileList.length === 0) {
        this.$message.info('请选择上传文件')
        return
      }
      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])
        importUser(this.dept.deptId, formData)
          .then((res) => {
            if (res.data.errorNum > 0) {
              this.$message.error(res.data.message)
            } else {
              this.$message.info('操作成功')
            }
            this.handleCancel(values)
          }).catch(() => {
            this.confirmLoading = false
          })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      this.$emit('ok', values)
    }

  }
}
</script>
