<template>
    <Upload
        :data="posData"
        :multiple="multiple"
        :action="getUploadAction"
        :format="formateAry"
        :disabled="disabled"
        :before-upload="handleBeforeUpload"
        :on-format-error="handleFormatError"
        :on-exceeded-size="handleMaxSize"
        :on-success="handleSuccess"
        :on-error="handleError"
        :show-upload-list="false"
        :headers="headers"
        :max-size="maxSize"
    >
        <Button  :disabled="disabled" :class="extClass" class="go_upload_btn" icon="ios-cloud-upload-outline">{{itemLabel}}
        </Button>
        <div v-if="loadingFlag" style="position: absolute;left:17px;top:3px">
            <Icon type="ios-loading" size=18 class="demo-spin-icon-load" style="color:#1890ff;font-weight: bold;"></Icon>
        </div>
    </Upload>
</template>

<script>
    import {AssertUtils} from "goingutils";
    import uploadFileMixin from "../../ctlMixins/UploadFileMixins";

    export default {
        name: "go-upload",
        mixins: [uploadFileMixin],
        data: function () {

            return {

                posData: {
                    ...this.params
                }
            };
        },
        props: {
            disabled: {
                type: Boolean,
                default: false
            },
            multiple: {
                type: Boolean,
                default: true
            },
            maxSize: {
                type: Number,
                default: 204800
            },
            item: {
                type: Object,
                default: () => {
                }
            },
            action: {
                type: String,
                default: ""
            }
        },
        computed: {
            extClass(){
                return this.disabled?'disabled':'';
            },
            itemLabel(){
                if(this.loadingFlag){
                    return "上传中...";
                }else{
                    return this.item.text;
                }
            },
            getUploadAction() {
                if(AssertUtils.isNotEmpty(this.action)){
                    return this.action;
                }else{
                    return this.uploadAction;
                }

            }
        },
        methods: {
            handleBeforeUpload() {
                this.$emit("beforeUpload", {item:this.item});
                if(this.item.flag===false){
                    return false;
                }
                this.loadingFlag=true;
            },
            handleSuccess(res) {
                this.loadingFlag=false;
                this.$emit("uploadSuccess", {res,item:this.item});
                return;
            }
        },
        mounted() {

        },
        components: {}
    };
</script>
