// components/filling/index.js
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        fileUrlArray:{
            type:Array
        },
        limit:{
            type:Number
        }
    },

    /**数据监听 */
    observers: {
        fileUrlArray:function(val){
            this.setData({
                uploaderList:val
            })
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        quantity:5,
        uploaderList: [],
        uploaderNum:0,
        showUpload:true
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 删除图片
        clearImg:function(e){
            var nowList = [];//新数据
            var uploaderList = this.data.uploaderList;//原数据
            
            for (let i = 0; i < uploaderList.length;i++){
                if (i == e.currentTarget.dataset.index){
                    continue;
                }else{
                    nowList.push(uploaderList[i])
                }
            }
            this.setData({
                uploaderNum: this.data.uploaderNum - 1,
                uploaderList: nowList,
                showUpload: true
            })
            console.log(this.data.uploaderList)
            this.triggerEvent("files", this.data.uploaderList); 
        },
        //上传图片
        upload: function(e) {
            if(this.data.limit){
                this.data.quantity=this.data.limit;
            }
            var that = this;
            wx.chooseMessageFile({
                count: that.data.quantity - that.data.uploaderNum, // 默认9
                success: function(res) {
                    // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
                    let tempFilePaths = res.tempFiles;
                    let uploaderList = that.data.uploaderList.concat(tempFilePaths);
                    if (uploaderList.length==that.data.quantity){
                        that.setData({
                            showUpload:false
                        })
                    }
                    that.setData({
                        uploaderList: uploaderList,
                        uploaderNum: uploaderList.length,
                    })
                    that.triggerEvent("files", that.data.uploaderList); 
                }
            })
        },
    }
})