<template>
  <div>
    <Upload
    id="uploadavatar"
      ref="upload"
      :show-upload-list="false"
      :default-file-list="defaultList"
      :on-success="handleSuccess"
      :format="['jpg','png']"
      :max-size="2048"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleMaxSize"
      type="drag"
      :data="{manager_access_token}"
      :action="this.$api.uploadAvatar()"
      style="display: inline-block;width:102px;"
    >
      <div style="height:102px;width:102px;border-radius:50%;text-align:center;background:red"></div>
    </Upload>
  </div>
</template>
<script>
import { getStore } from "@/utils/utils";
export default {
  data() {
    return {
      defaultList: [],
      imgUrl: "",
      visible: false,
      uploadList: []
    };
  },
  props: {},
  computed: {
    manager_access_token() {
      return getStore("userInfo").manager_access_token;
    }
  },
  watch: {},
  methods: {
    handleSuccess(data, file) {
      if (data.code == 200) {
        file.url = data.data.thumbnail;
        file.status = "finished";
        this.$api.setUserAvatar({avatar:data.data.saveName}).then(res=>{
          if(res.code==200){
            this.$Message.info(res.msg)
            this.$emit('newAvatar',data.data.thumbnail)
          }else{
            this.$Message.info(res.msg)
          }
        })
      }else{
        this.$Message.info(data.msg)
      }
    },
    handleFormatError(file) {
      this.$Message.error("上传的图片只允许jpg、png");
    },
    handleMaxSize(file) {
      this.$Message.error("上传的图片太大了，不能超过2M");
    },
  },
  mounted() {
    this.uploadList = this.$refs.upload.fileList;
  }
};
</script>
<style>
.demo-upload-list {
  display: inline-block;
  width: 60px;
  height: 60px;
  text-align: center;
  line-height: 60px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 4px;
}
.demo-upload-list img {
  width: 100%;
  height: 100%;
}
.demo-upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}
.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}
.demo-upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}
#uploadavatar .ivu-upload-drag{
    border: none;
}
</style>
