import toast from "../../../utils/tool/toast";

export default {

    data(){
        return {

            /*
            * 	status 1 上传中 2 上传成功 4上传失败
            * 	url  上传后的路径
            *   src  展示的路径
            *   type 类型后缀
            *   other 其他参数配置
            * */
            uploadData:[],

            uploadLength:0,

            avgWidth: 0,

            hidden:false

        }
    },


    methods:{

        updateUpdateLength(){
            let length = this.uploadData && this.uploadData.length || 0;
            if (length !== this.uploadLength) {
                this.uploadLength = length;
            }
        },

        /* 计算宽度 */
        countAvgWidth:function () {
            this.avgWidth = !this.size ? ((this.width - (this.padding * 2)) - (this.row - 1 ) * this.margin) / this.row : this.size;
        },

        createdData:function (data) {


            this.uploadData = data.map((item)=>{

                if (typeof item === 'string') {
                    item ={url:item,src:item,is_del:true};
                } else {
                    item ={url:item.url,src:item.url,is_del:item.is_del};
                }

                return {
                    status: 2,
                    ...item
                };

            });

            if (this.uploadData.length > 0) {
                this.uploadData.push(...this.uploadData);
            }

            this.hidden = this.uploadData.length >= this.max;

            return this.changeData();
        },

        // 选择图片
        selectImage:function () {

            return uni.chooseImage({

                count: this.max - this.uploadData.length,
                sizeType:'compressed',
                success: (e)=> {

                    // 执行处理文件
                    let status = false;
                    let i = 0;
                    let count = this.uploadData.length;
                    e.tempFiles.map((item)=>{

                        let type = item.path.substr(item.path.lastIndexOf('.') + 1);

                        if (this.fileType.indexOf(type) < 0) {
                            status = true;
                        } else {

                            this.uploadData[count+i] = {
                                src: item.path,
                                url:'',
                                status:1,
                                size: item.size,
                                type
                            };

                            count++;
                        }

                    });
                    this.hidden = count >= this.max;

                    if (status) {
                        if (e.tempFiles.length <= 1) {
                            return toast.fail('图片格式有误');
                        } else {
                            toast.fail('部分图片格式有误');
                        }

                    }


                    return this.changeData();

                }

            });

        },

        // 删除一个被选中的文件
        deleteImage:function(index){
            if (this.uploadData[index]) {
                this.uploadData.splice(index,1);

                this.hidden = this.uploadData.length >= this.max;

                return this.changeData();
            }
        },

        // change的时候触发
        changeData(){
            this.updateUpdateLength();
            return this.$emit('input',this.uploadData.map((item)=> item.src));
        }

    },

    mounted(){
        this.countAvgWidth();
        return this.createdData(this.value);
    }

}