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

    /**
     * 页面的初始数据
     */
    data: {
        collectList: [],
        activeColor: null,
        titleColor:null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function(options) {
        await this.getCollect()
        console.log(getApp().globalData.activeColor);
        this.setData({ activeColor: getApp().globalData.activeColor,
            titleColor:getApp().globalData.titleColor
         })
    },
    async getCollect() {
        let userid = Parse.User.current().id
        let Collect = new Parse.Query('Collect')
        Collect.equalTo('user', userid)
        Collect.equalTo('company', company)
        Collect.include('collectTarget')
        let collect = await Collect.find()
        if (collect && collect.length > 0) {
            let collectJSON = []
            collect.forEach(c => {
                collectJSON.push(c.toJSON())
            })
            this.setData({
                collectList: collectJSON
            })
        }
        console.log(this.data.collectList)
    },

    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)
                }
            }
        })
    },

    lesson(e) {
        let id = e.currentTarget.dataset.item.collectTarget[0].objectId
        console.log(id);
        wx.navigateTo({
            url: '/nova-lesson/pages/lesson-detail/lesson-detail?id=' + id
        })

    },
    async deleteCollect(id, index) {
        let that = this
        let Collect = new Parse.Query('Collect')
        let collect = await Collect.get(id)
        if (collect && collect.id)(
            collect.destroy().then(res => {
                console.log(res)
                if (res && res.id) {
                    console.log(id, index)
                    let collectList = that.data.collectList
                    collectList.splice(index, 1)
                    that.setData({
                        collectList: collectList
                    })
                }
            })
        )
    },
    toDetail(e) {
        console.log(e)
        let item = e.currentTarget.dataset.item
        if (item.type == 'goods') {
            wx.navigateTo({
                url: '/nova-shop/pages/shop-goods/goods-detail/index?id=' + item.collectId
            })
        }
        if (item.type == 'article') {
            wx.navigateTo({
                url: '/common-page/pages/cates/article-detail/index?aid=' + item.collectId + "&title=" + item.name
            })
        }
        if (item.type == 'lesson') {
            wx.navigateTo({
                url: '/nova-lesson/pages/lesson-detail/index?id=' + item.collectId
            })
        }
    },

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

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

    }
})