const $ = global;
const orderTexts = [
    '已预约，等待处理',
    '未付款',
    '已付款'
]
new $.Page({
    data: {
        services: [],
        goods: []
    },
    onShow() {
        this.init();
    },
    init() {
        $.request('my/orders', 'GET', {}).then((res) => {
            const services = []
            const goods = []
            if (res.data && res.data.length > 0) {
                res.data.forEach(e => {
                    e.orderText = orderTexts[e.status];
                    if (e.order_type == 'product_order') {
                        if (e.status == 0)
                            e.orderText = '待付款';
                        goods.push(e);
                    } else {
                        services.push(e);
                    }
                });
                this.setData({
                    services,
                    goods
                });
            }
        });
    },
    toPay(e) {
        const {
            id,
            type
        } = e.target.dataset;
        if (id && type) {
            $.wx.showToast({
                title: '支付中'
            }, 'loading');
            $.request('payments/pay', 'POST', {
                order_id: id,
                order_type: type
            }).then((res) => {
                if (res.msg) {
                    const {
                        timestamp,
                        paySign,
                        nonceStr
                    } = res.msg;
                    const pck = res.msg.package;
                    wx.requestPayment({
                        timeStamp: timestamp + '',
                        nonceStr,
                        package: pck,
                        signType: 'MD5',
                        paySign,
                        success(res) {
                            console.log('支付成功', res);
                            $.wx.hideToast('loading');

                        },
                        fail() {
                            $.wx.showToast({
                                title: '支付失败'
                            });
                        }
                    });
                }
            });
        }
    },
    addComment(e) {
        const {
            id,
            type
        } = e.target.dataset;
        if (id) {
            $.router.goto({
                path: '/pages/addComment/index',
                query: {
                    id,
                    type
                }
            });
        }
    }
});