const Parse = getApp().Parse;
const company = getApp().globalData.company
const dateF = require('../../../utils/date')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: null
    },
    async getBehaviorLog(id) {
        let BehaviorLog = new Parse.Query('BehaviorLog')
        BehaviorLog.equalTo('company', company)
        BehaviorLog.equalTo('user', id)
        BehaviorLog.descending('createdAt')
        BehaviorLog.include('targetObject')
        let behaviorLog = await BehaviorLog.find()
        let listJSON = []
        if (behaviorLog && behaviorLog.length > 0) {
            behaviorLog.forEach(behaviorLog => {
                let b = behaviorLog.toJSON()
                b.joinTime = dateF.formatTime("YYYY-mm-dd HH:MM:SS", b.createdAt)
                listJSON.push(b)
            })
            this.setData({ list: listJSON })
        }
        console.log(this.data.list);
    },
    lesson(e) {
        let id = e.currentTarget.dataset.item.targetObject[0].objectId
        console.log(id);
        wx.navigateTo({
            url: '/nova-lesson/pages/lesson-detail/lesson-detail?id=' + id
        })

    },

    goods(e) {
        let id = e.currentTarget.dataset.item.targetObject[0].objectId
        console.log(id);
        wx.navigateTo({

            url: '/nova-shop/pages/shop-goods/goods-detail/index?id=' + id
        })

    },
    async collection(e) {
        console.log(e.currentTarget.dataset.item);
        // if (schemaName == "ShopGoods") {
        //     let name = e.currentTarget.dataset.item.targetObject[0].name
        // }
        // if (schemaName == "Lesson") {
        //     let name = e.currentTarget.dataset.item.targetObject[0].title
        // }
        let collectId = e.currentTarget.dataset.item.targetObject[0].objectId
        let schemaName = e.currentTarget.dataset.item.targetClassName
        let image = e.currentTarget.dataset.item.targetObject[0].image
        console.log(collectId, schemaName, image);
        let Collecti = new Parse.Query('Collect')
        Collecti.equalTo('company', company)
        Collecti.equalTo('user', Parse.User.current().id)
        Collecti.equalTo('collectId', collectId)
        let res = await Collecti.first()
        if (res && res.id) {
            wx.showToast({
                title: '您已收藏该课程',
                icon: 'none'
            })
            return
        }
        let collect
        let Collect = Parse.Object.extend("Collect")
        collect = new Collect()
        collect.set("company", {
            __type: "Pointer",
            className: "Company",
            objectId: company
        })
        collect.set("user", {
            __type: "Pointer",
            className: "_User",
            objectId: Parse.User.current().id
        })
        if (schemaName == "ShopGoods") {
            collect.set("collectTarget", [{
                __type: "Pointer",
                className: "ShopGoods",
                objectId: collectId
            }])
            collect.set("name", e.currentTarget.dataset.item.targetObject[0].name)
            collect.set("type", "goods")
        }
        if (schemaName == "Lesson") {
            collect.set("collectTarget", [{
                __type: "Pointer",
                className: "Lesson",
                objectId: collectId
            }])
            collect.set("name", e.currentTarget.dataset.item.targetObject[0].title)
            collect.set("type", "lesson")
        }
        // if (schemaName == "Lesson") {
        //     collect.set("collectTarget", [{
        //         __type: "Pointer",
        //         className: "Lesson",
        //         objectId: collectId
        //     }])
        // }
        collect.set("collectId", collectId)
        collect.set("schemaName", schemaName)
        collect.set("image", image)

        collect.save().then(a => {
            console.log(a)

            wx.showToast({

                title: '收藏成功',
                icon: 'none'
            })


        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let id = Parse.User.current().id
        console.log(id);
        this.getBehaviorLog(id)
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})