// 100 待付款 200 付款成功代发货， 300 已发货待收货 400 已收货待评价，500 申请退款审核中
// 600 审核通过准备退款  700 退款成功 800 评价完成订单完成 

const Parse = getApp().Parse
const company = getApp().globalData.company
Page({
    data: {
        value: 5,
        fileList: [],
        checked: true,
        task: null,
        images: null
    },
    async getOrder(id) {
        console.log(id);
        let shopOrder = new Parse.Query("Order")
            // shopOrder.include("goods")
        let res = await shopOrder.get(id)
        if (res && res.id) {
            let order = res.toJSON()
            this.setData({
                order: order
            })
            if (order.score) {
                wx.showToast({
                    title: '该订单已评价过了',
                    icon: 'none',
                    image: '',
                    duration: 1500,
                    mask: false,
                });
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    });
                }, 1500);
                return
            }
        }
    },
    onChange(e) {
        console.log(e);
        this.setData({
            value: e.detail
        })
    },
    onOpen(e) {
        console.log(e.detail);
        this.setData({
            checked: e.detail,
        });
    },

    //添加图片
    changeFile(e) {
        console.log(e);
        let fileList = e.detail
        console.log(fileList);
        let images = []
        fileList.forEach(file => {
            images.push(file.url)
        })
        this.setData({
            fileList: fileList,
            images: images
        })
        console.log(this.data.images);
    },

    async getUptoken() {
        let res = await Parse.Cloud.run('qiniu_uptoken', {
            company: company
        })
        console.log(Object.keys(res));
        console.log(res);
        this.setData({
            uptokenURL: res.uptoken,
            domain: res.domain,
            uploadURL: res.zoneUrl
        })
    },

    async submit() {
        let {
            id,
            value,
            images,
            checked,
            content
        } = this.data
        console.log(id,
            value,
            images,
            checked,
            content);
        try {
            let shopOrder = new Parse.Query("Order")
                // shopOrder.include("goods")
            let res = await shopOrder.get(id)
            if (res && res.id) {
                res.set("open", checked)
                res.set("images", images)
                res.set("score", +value)
                res.set("content", content)
                res.set("status", '800')
                await res.save()
                await this.createdLog()
                wx.showToast({
                    title: '评价成功',
                    icon: 'success',
                    image: '',
                    duration: 1500,
                    mask: false,
                });
                let pages = getCurrentPages();
                let prevPage = pages[pages.length - 2]
                let e = {
                    detail: { index: 4, name: 4, title: "待评价" }
                }
                prevPage.onChange(e)
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    });
                }, 1500);
            }
        } catch (err) {
            wx.showToast({
                title: '系统异常，请稍后再试',
                icon: 'success',
                image: '',
                duration: 1500,
                mask: false,
            });
            setTimeout(() => {
                wx.navigateBack({
                    delta: 1
                });
            }, 1500);
            console.log(err);
        }


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let {
            id
        } = options
        console.log(id);
        this.setData({
            id
        })
        this.getOrder(id)
        this.getUptoken()
        this.getTask()

    },
    async getTask() {
        let Task = new Parse.Query('Task')
        Task.equalTo('company', company)
        Task.equalTo('isOpen', 'true')
        Task.equalTo('taskType', 'product-evaluation')
        let task = await Task.first()
        if (task && task.id) {
            this.setData({
                task: task
            })
        }
    },
    async createdLog() {
        let userid = Parse.User.current().id
        if (!this.data.task) {
            return
        }
        let TaskLog = Parse.Object.extend('TaskLog')
        let tasklog = new TaskLog()
        tasklog.set('user', {
            __type: "Pointer",
            className: '_User',
            objectId: userid
        })
        tasklog.set('task', {
            __type: "Pointer",
            className: 'Task',
            objectId: this.data.task.id
        })
        tasklog.set('company', {
            __type: "Pointer",
            className: 'Company',
            objectId: company
        })
        tasklog.set('isReceive', false)
        tasklog.set('credit', this.data.task.credit)
        await tasklog.save()
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})