<template>
  <div class="van-cell van-field" :class="{'van-cell--required':!!option.required}">
    <div class="van-cell__title van-field__label"><span>{{option.label}}</span></div>
    <van-uploader multiple v-model="fileList" v-bind="option.props" @oversize="onOversize" >
    </van-uploader>
    </div>
</template>

<script>
export default {
    name:"myUpFile",
    model:{
        prop:"give",
        event:'change'
    },
    props:{
        option:{
            type:Object,
            default:{
                upAction:'auto'
            }    
        }
    },
    data(){
        return{
            fileList:[],
            srcList:[]
        }
    },
    computed:{
        getIpAction(){
            return this.option['upAction']||'auto'
        },
        model:{
            get() {
                return this.give;
            },
            set(val) {
                this.$emit('change', val);
            }
        },
    },
    methods:{
        onOversize(file){
            Toast(file.getName+'超过最大限度');
        },
        upload(){
            return new Promise((resolve, reject)=> {

                    let promiseAll=[];
                    for(let i=0;i<this.fileList.length;i++){
                        let file = this.fileList[i]
                        let data = new FormData();
                        data.set("file",file.file);
                        let promise= this.$post(hbase.file+'core/file/upload',data,{
                            headers:{
                                'Content-Type': 'multipart/form-data'
                            },
                            meta:{
                                noSerialize:true
                            }
                        }).then(res=>{
                            this.srcList.push(res.data)
                        })
                        promiseAll.push(promise)
                    }
                    Promise.all(promiseAll).then(()=>{
                        this.model = this.srcList;
                        resolve(console.log(this.option.name+'组件已上传完毕'))
                    })
                })
            
        }
    },
    watch:{
        fileList(nw,old){
            if(nw.length>old.length){
                if(this.getIpAction=='auto'){
                    this.upload()
                }
            }
        }
    }

}
</script>

<style>

</style>