<template>
  <el-upload v-loading="loading" action="/" :auto-upload="false" :show-file-list="false" :on-change="onChange" :on-progress="onProgress">
    <el-button :type="type" :size="size">
      <slot>导入</slot>
    </el-button>
  </el-upload>
</template>

<script>
import { import_data } from '@/api/import-data'
export default {
  props: {
    action: {
      type: String,
      default: ''
    },
    params: {
      type: Object,
      default: () => ({})
    },
    code: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: 'primary'
    },
    size: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false
    }
  },
  methods: {
    onChange(file) {
      this.loading = true
      import_data({
        file: file.raw,
        action: this.action,
        code: this.code,
        ...this.params
      }).then(res => {
        this.$message({
          type: 'success',
          message: `上传成功: ${res.data}`
        })
        this.$emit('success')
        this.loading = false
      }).catch(err => {
        this.$emit('fail', err)
        this.loading = false
      })
    },
    onProgress(file) {
      console.log('progress')
      console.log(file)
    }
  }
}
</script>
