<template>
  <div>
    <el-upload
      :action="upUrl"
      :data="updata"
      :show-file-list="false"
      :headers="headers"
      :on-success="handleAvatarSuccess"
      :on-error="handleAvatarError"
      :before-upload="beforeAvatarUpload">
      <slot name="content">{{ content }}</slot>
    </el-upload>
  </div>
</template>


<script>
    export default {
        name: "ui-template",
        computed: {},
        props:{
            upUrl:{
                default:'',
                type:String
            },
            headers:{
                default: ()=> { return {};},
                type:Object
            },
            updata:{
                default: ()=> { return {};},
                type:Object
            },
            img:{
                default:'',
                type:String
            },
            fileName:{
                default:'',
                type:String
            },
            type:{
                default:'img',
                type:String
            },
          tmpId:{
            default:0,
            type:Number
          },
          content:{
            type: String,
            default: ''
          }
        },
        data() {
            return {
            }
        },

        created() {
        },
        mounted(){

        },
        methods: {
            beforeAvatarUpload(file) {
                const array = ['image/jpeg','image/png','image/gif'];
                const isLt2M = file.size / 1024 / 1024 < 2;
                var isImg = true;
                if (array.indexOf(file.type) == -1) {
                    this.$message.error('上传头像图片只能支持JPG,PNG,GI格式!');
                    isImg = false;
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                this.$utils.loading(this,"上传中..");
                return isImg && isLt2M;
            },
            /**
             * 上传成功以后
             */
            handleAvatarSuccess(res, file) {
                this.$utils.loadingClose();
                // this.imageUrl = URL.createObjectURL(file.raw);
                this.$emit("onUpload",res,this.tmpId);
            },

            handleAvatarError:function (err, file, fileList) {
                this.$utils.loadingClose();
              //  this.$message.error(err);
                console.log(err);
            }

        }
    }
</script>
<style scoped>
  .avatar-uploader{
    border: 1px dashed #d9d9d9;
    width: 82px;
    height: 82px;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
  }
  .update-img{ height: 80px; width: 80px; overflow:hidden;}
  .avatar {width: 80px;display: block;}
</style>
