// nova-lesson/pages/lesson-detail/write-comment/index.js
let Parse = getApp().Parse;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        rate: 0,
        content: '',
        uploadURL: "",
        domain: "",
        uptokenURL: "",
        fileList: [],
        activeColor: getApp().globalData.activeColor,
        titleColor: getApp().globalData.titleColor,
    },
    onChange(e) {
        let rate = this.data.rate
        this.setData({
            rate: e.detail
        })
    },
    blur(e) {
        let content = e.currentTarget.dataset.name
        this.setData({
            content: e.detail.value
        })
    },
    changeFile(e) {
        console.log(e)
        if (e.detail && e.detail.length > 0) {
            let fileList = []
            let filearr = e.detail
            filearr.forEach(file => {
                fileList.push(file.url)
            })
            this.setData({
                image: fileList
            })
        } else {
            this.setData({
                image: []
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function(options) {

        let activeColor = getApp().globalData.activeColor
        let titleColor = getApp().globalData.titleColor
        let {
            id
        } = options
        this.setData({
            id: id,
            activeColor: activeColor,
            titleColor: titleColor
        })
        console.log(id);
    },

    async confirm(e) {
        let type = e.currentTarget.dataset.type
        console.log(this.data.rate, this.data.content, this.data.fileList);
        switch (type) {
            case "cencle":
                wx.navigateBack();
                break;

            case "submit":
                if (this.data.rate < 0 || !this.data.content) {
                    wx.showToast({
                        title: '请填写内容',
                        icon: 'none',
                        duration: 1500,
                    });
                    return
                } else {
                    wx.showModal({
                        title: '提交评价',
                        content: '是否确认提交评价',
                        showCancel: true,
                        cancelText: '取消',
                        cancelColor: '#000000',
                        confirmText: '确定',
                        confirmColor: '#3CC51F',
                        success: async(result) => {
                            let dis = await this.setDiscuss()
                            if (result.confirm) {
                                if (dis) {
                                    wx.showToast({
                                        title: '提交成功',
                                        icon: 'success',
                                    });
                                    wx.redirectTo({
                                        url: '/nova-lesson/pages/lesson-detail/lesson-detail?id=' + this.data.id
                                    });
                                } else {
                                    wx.showToast({
                                        title: '提交失败',
                                        icon: 'none',
                                    });
                                }

                            } else {
                                wx.showToast({
                                    title: '取消提交',
                                    icon: 'none',
                                });
                            }
                        },
                        fail: () => {},
                        complete: () => {}
                    });
                }
                break;
        }
    },

    async setDiscuss() {
        let LessonComment = Parse.Object.extend("LessonComment")
        let lessonComment = new LessonComment()
        lessonComment.set("score", this.data.rate)
        lessonComment.set("content", this.data.content)
        lessonComment.set("images", this.data.images)
        lessonComment.set("user", {
            __type: "Pointer",
            className: "_User",
            objectId: Parse.User.current().id
        });
        lessonComment.set("company", {
            __type: "Pointer",
            className: "Company",
            objectId: getApp().globalData.company
        })
        lessonComment.set("lesson", {
            __type: "Pointer",
            className: "Lesson",
            objectId: this.data.id
        })
        let result = await lessonComment.save();
        if (result && result.id) {
            return result
        } else {
            return false
        }
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})