const Parse = getApp().Parse
const company = getApp().globalData.company
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageIndex: 0,
        loadLen: 10,
        logs: []

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function(options) {
        await this.getLog()
    },
    async getLog() {
        let uid = Parse.User.current().id
        let BehaviorLog = new Parse.Query('BehaviorLog')
        BehaviorLog.equalTo('company', company)
        BehaviorLog.equalTo('user', uid)
        BehaviorLog.include('targetObject')
        BehaviorLog.limit(this.data.loadLen)
        BehaviorLog.descending('createdAt')
        BehaviorLog.skip(this.data.pageIndex * this.data.loadLen)
        let logs = await BehaviorLog.find()
        console.log(logs)
        if (logs && logs.length > 0) {
            let logsJSON = []
            logs.forEach(log => {
                logsJSON.push(log.toJSON())
            })
            let newLogs = this.data.logs.concat(logsJSON)
            this.setData({
                logs: newLogs
            })
        }

    },

    toDetail(e) {
        console.log(e)
        let item = e.currentTarget.dataset.item
        if (item.targetClassName == 'ShopGoods') {
            wx.navigateTo({
                url: '/nova-shop/pages/shop-goods/goods-detail/index?id=' + item.targetId
            })
        } else if (item.targetClassName == 'Article') {
            wx.navigateTo({
                url: '/common-page/pages/cates/article-detail/index?aid=' + item.targetId + "&title=" + item.name
            })
        } else if (item.targetClassName == 'Lesson') {
            wx.navigateTo({
                url: '/nova-lesson/pages/lesson-detail/index?id=' + item.collectId
            })
        } else if (item.targetClassName == 'Activity') {
            wx.navigateTo({
                url: '/nova-activity/pages/activity-detail/index?aid=' + item.targetId
            })
        }
    },

    onClose(event) {
        console.log(event);
        let that = this
        let id = event.currentTarget.dataset.id
        let index = event.currentTarget.dataset.index
        console.log(id, index)
        const {
            position,
            instance
        } = event.detail;
        switch (position) {
            case 'right':
                instance.close();
                break;
        }
        wx.showModal({
            title: '确认删除',
            content: '确定删除该记录？',
            success: function(res) {
                if (res.confirm) {
                    that.deleteCollect(id, index)
                }
            }
        })
    },
    async deleteCollect(id, index) {
        let that = this
        let BehaviorLog = new Parse.Query('BehaviorLog')
        let behaviorLog = await BehaviorLog.get(id)
        if (behaviorLog && behaviorLog.id)(
            behaviorLog.destroy().then(res => {
                console.log(res)
                if (res && res.id) {
                    console.log(id, index)
                    let logs = that.data.logs
                    collectList.splice(index, 1)
                    that.setData({
                        logs: logs
                    })
                }
            })
        )
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: async function() {
        this.setData({
            pageIndex: this.data.pageIndex + 1
        })
        await this.getLog()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})