<template>
  <div>
    <slot name="down"></slot>
    <el-upload
      class="avatar-uploader"
      ref="upload"
      :action="url"
      :multiple="false"
      :data="{ ...query }"
      :limit="1"
      :on-success="upSuccess"
      :on-error="upError"
      accept="image/*"
    >
      <!-- accept="application/vnd.ms-excel" -->
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>

<script>
export default {
  name: "downUpData",
  props: {
    query: {
      type: Object,
      default: function () {
        return {};
      },
    },
    url: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      file: null,
      imageUrl: "",
    };
  },
  methods: {
    async submitUpload() {
      this.$refs.upload.submit();

      // const formData = new FormData()
      // formData.append('id',this.componentId)
      // formData.append('file',new File(this.file.raw))
      // let {status,info} = await uploadExcel(formData);
      // if(status===0){
      //   this.$message.success(info);
      //   this.$emit("upSuccess")
      // } else {
      //   this.$message.error(info);
      // }
    },
    upSuccess(res, file) {
      if (res && res.status == 0) {
        this.imageUrl = URL.createObjectURL(file.raw);
        this.$message.success("上传成功");
        this.$emit("upSuccess", res);
      } else {
        this.$message.error(res.info);
      }
    },
    upError(res) {
      if (res && res.status == 0) {
        this.$message.error("上传失败");
      } else {
        this.$message.error(res.message);
      }
    },
  },
};
</script>

<style>
.avatar-uploader {
  line-height: 1;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader .avatar-uploader-icon {
  font-size: 24px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar-uploader .avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>
