Page({
    data: {
        page    :   1,
        has_more:   true,
        lists   :   [],
        share_id:   '',
        loading    : true,
    },

    onLoad(e){
        this.setData({
            share_id : e.share_id || ''
        })
        getApp().globalData.inviteText = e.invite || ''
        this.getList()
    },

    onHide(){
        if(this.data.share_id){
            this.setData({
                share_id:''
            })
        }
    },
    onShow(){
        if( this.data.lists.length>0 && this.data.share_id){
            this.setData({
                page:1,
                has_more:true,
                lists:[]
            })
            this.getList()
        }
    },
    getList(){
        let params = {
            page:this.data.page,
            share_id:this.data.share_id
        }
        wx.$api.found.foundIndex(params).then(res => {
            let lists = this.data.lists
            if(this.data.page == 1){lists = []}
            lists = this.data.lists.concat(res.data.data)
            
            this.setData({
                lists   :   lists,
                has_more:   res.data.page.has_more,
            })

            this.setData({
                loading:false
            })
            setTimeout(()=>{
                wx.hideLoading()
                wx.stopPullDownRefresh()
            },3000)
        }).catch(err=>{
            wx.showToast({
              title: err.message,
              icon :'none',
              mask :true,
              duration:2000
            })
        })
    },
    onPullDownRefresh(){
        this.setData({
            page:1,
            has_more:true,
            lists:[],
            loading:true
        })
        this.getList()
    },
    onReachBottom(){
        if(this.data.has_more){
            this.setData({
                page:this.data.page +1
            })
            this.getList()
            wx.showLoading({
              title: '疯狂加载中..',
            })
        }else{
            wx.showToast({
                title: '没有更多',
                icon:'none',
                mask:true,
                duration:2000
              })
        }
    },
    // 预览图片
    preImg(e){
        let current = e.currentTarget.dataset.idx
        console.log(current)
        let urls = e.currentTarget.dataset.urls
        wx.previewImage({
          urls,
          current:urls[current]
        })
    },
    // 收藏
    doSubscribeFavorite(e){
        let index= e.currentTarget.dataset.index
        let type = e.currentTarget.dataset.type
        let item = this.data.lists[index]
        let lists = this.data.lists
        wx.$api.found.foundSubscribeFavorite(item.id,type).then(res => {
            lists[index].is[`${type}`] = res.data.res
            lists[index].count[`${type}`]  = res.data.count
            this.setData({
                lists:lists
            })
        }).catch(err=>{
            wx.showToast({
              title: err.message,
              icon :'none',
              mask :true,
              duration:2000
            })
        })
    },
	onShareAppMessage(e){
        let {describe,id,share_cover}= e.target.dataset.obj
		return {
			title	: describe,
            imageUrl: share_cover,
            path	: "/pages/found/index?share_id=" + id + '&invite=' + wx.getStorageSync("invite")
		}
    },
})