const $ = global;
const orderTexts = [
    '已预约，等待处理',
    '未付款',
    '已付款'
]
new $.Page({
    data: {
        services: [],
        goods: []
    },
    onShow() {
        this.init();
    },
    init() {
        $.request('my/orders', 'GET', {}).then((res) => {
            if (res.data) {
                const services = res.data.others || [];
                const goods = res.data.product_orders || [];
                services.forEach(e => {
                    e.orderText = orderTexts[e.status];
                });
                goods.forEach(e => {
                    e.orderText = orderTexts[e.status];
                    if (e.status == 0) e.orderText = '待付款';
                    e.text = '';
                    for (let p of e.products) {
                        let t = `${p.name}×${p.amount}`;
                        e.text += e.text ? ('；' + t) : t;
                    }
                });
                this.setData({
                    services,
                    goods
                });
            }
        });
    },
    toPay(e) {
        const _this = this;
        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');
                            _this.init();
                        },
                        fail() {
                            $.wx.showToast({
                                title: '支付失败'
                            });
                        }
                    });
                }
            });
        }
    },
    addComment(e) {
        const {
            id,
            type
        } = e.target.dataset;
        if (id) {
            $.router.goto({
                path: '/pages/addComment/index',
                query: {
                    id,
                    type
                }
            });
        }
    }
});