<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" :before-close="closeDialog">
    <div class="up-box">
      <el-upload :limit="limit" class="upload-demo" ref="upload" :action="action" :data="uploadParams" :on-success="onSuccess" :on-change="onChange" :on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList" :auto-upload="false">
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submitUpload">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'WUpload',
  props: {
    action: String,
    title: {
      type: String,
      default: '导入'
    },
    limit: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      dialogVisible: false,
      uploadParams: {},
      fileList: []
    }
  },
  methods: {
    setUploadParams(params) {
      this.uploadParams = { ...params }
    },

    openDialog() {
      this.dialogVisible = true
    },

    closeDialog() {
      this.dialogVisible = false
      this.clearFiles()
    },

    clearFiles() {
      this.$refs.upload.clearFiles()
    },

    submitUpload() {
      this.$refs.upload.submit()
    },

    onChange(file, fileList) {},

    handleRemove(file, fileList) {},

    onSuccess(response, file, fileList) {
      this.$emit('onSuccess', response)
    },

    handlePreview(file) {}
  }
}
</script>
<style lang="scss" scoped>
@import './index.scss';
</style>
