<template>
    <el-upload
        class="single-upload"
        :action="uploadUrl"
        :headers="headers"
        :show-file-list="false"
        :on-success="onSuccess"
        :before-upload="beforeUpload"
    >
        <div></div>
        <div class="wrapper">
            <img v-if="imageUrl" :src="imageUrl" class="upload-image" />
            <i v-else class="el-icon-plus single-uploader-icon"></i>
            <div v-if="loading" class="loading">
                <i class="el-icon-loading"></i>
            </div>
        </div>
        <div slot="tip" class="el-upload__tip">
            <slot></slot>
        </div>
    </el-upload>
</template>
<script>
import resolveUrl from 'resolve-url';
export default {
    created() {
        this.uploadUrl = resolveUrl(this.$baseUrl, 'upload/file');
        this.updateImageUrl(this.value);
    },
    props: {
        value: String,
        usePrefix: {
            type: Boolean,
            default: true
        },
        url: {
            type: String
        }
    },
    data() {
        return {
            imageUrl: '',
            loading: false,
            uploadUrl: ''
        };
    },
    computed: {
        headers() {
            return {
                Authorization: 'Bearer ' + localStorage.getItem('token')
            };
        }
    },
    methods: {
        onSuccess(res, file) {
            this.loading = false;
            this.imageUrl = URL.createObjectURL(file.raw);
            var newVal = '';
            if (res instanceof Array) {
                newVal = res[0];
            } else {
                newVal = res;
            }
            this.$emit('input', newVal);
        },
        onError(err, file, fileList) {
            this.loading = false;
        },
        beforeUpload(file) {
            this.loading = true;
            return true;
        },
        updateImageUrl(url) {
            this.imageUrl = url;
        }
    },
    watch: {
        value(val) {
            this.updateImageUrl(val);
        }
    }
};
</script>
<style lang="less" scoped>
.single-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    background-color: #fbfdff;

    &:hover {
        border-color: #409eff;
    }
}

.upload-image {
    height: 178px;
    display: block;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    &:hover {
        border-color: #409eff;
    }
}

.wrapper {
    position: relative;
}

.single-upload .el-upload {
    position: relative;
}

.loading {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.6);
    color: #333;
    font-size: 24px;
}
</style>
