<template>
  <div class="w-up-btn">
    <el-upload :disabled="disabledVal" class="upload-demo" ref="uploadEl" :data="uploadParams" :action="action" :on-remove="handleRemove" :before-remove="beforeRemove" multiple :limit="3" :file-list="fileList">
      <el-button size="small" type="primary">点击上传</el-button>
    </el-upload>
  </div>
</template>

<script>
import store from '@/store/index'
export default {
  name: 'WUploadBtn',
  props: {
    disabledVal: Boolean,
    tossUrls: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      action: process.env.VUE_APP_BASE_API + '/supportMaterial/upload',
      uploadParams: {
        userId: store.state.user.userInfo.user.userId
      },
      fileList: []
    }
  },
  methods: {
    getTossUrlsString() {
      return this.tossUrls.toString()
    },

    beforeRemove() {},

    handlePreview() {},

    onSuccess(response) {
      const { id } = response.data[0]
      this.tossUrls.push(id)
    },

    handleRemove(file, fileList) {
      const files = this.$refs.uploadEl.uploadFiles
      for (let i = 0; i < files.length; i++) {
        if (files[i]['url'] === file.url) {
          files.splice(i, 1)
          this.tossUrls.splice(i, 1)
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
@import './index.scss';
</style>
