// pages/usermyorder/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        frameStyle: { useTop: true, name: '订单管理', leftArrow: true, useBar: false },
        list: [],
    },
    back() {
        wx.navigateBack({ delta: 1 })
    },
    // 支付
    toPay: async function (e) {
        const that = this;
        const id = e.currentTarget.dataset?.item?._id;
        const res = await app.$post('/newCourt/api/payOrder/toRePay', { id })
        if (res.errcode == '0') {
            wx.requestPayment({
                "timeStamp": res.data.wxSign.timestamp,
                "nonceStr": res.data.wxSign.nonceStr,
                "package": `prepay_id=${res.data.wxSign.prepay_id}`,
                "signType": res.data.wxSign.signType,
                "paySign": res.data.wxSign.paySign,
                "success": async function (arr) {
                    wx.showToast({ title: `支付成功`, icon: 'success', duration: 2000 });
                    const aee = await app.$post(`/newCourt/api/payOrder/${res.data.data._id}`, { status: '1' });
                    if (aee.errcode == '0') { that.watchLogin(); }
                },
                "fail": function (res) {
                    wx.showToast({ title: `支付未成功`, icon: 'error', duration: 2000 })
                    that.watchLogin()
                },
            })
        }
    },
    // 退款
    toRefund: async function (e) {
        const that = this;
        const { item } = e.currentTarget.dataset;
        wx.showModal({
            title: '提示',
            content: '您是否确定要进行退款，一旦操作不可恢复？',
            async success(res) {
                if (res.confirm) {
                    const arr = await app.$post('/newCourt/api/payOrder/toRefund', { id: item._id })
                    if (arr.errcode == '0') {
                        wx.showToast({ title: `退款成功`, icon: 'success', duration: 2000 });
                        that.watchLogin();
                    } else {
                        wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 });
                    }
                } else if (res.cancel) { }
            }
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.watchLogin();
    },


    // 监听用户是否登录
    watchLogin: async function () {
        const that = this;
        wx.getStorage({
            key: 'user',
            success: async res => {
                const arr = await app.$get(`/newCourt/api/payOrder`, { openid: res.data.openid, skip: 0, limit: 1000 })
                if (arr.errcode == '0') {
                    let list = arr.data;
                    list = list.map(i => ({ ...i, statusZh: that.getStatusZh(i.status) }))
                    that.setData({ list })
                }
            },
            fail: res => {
                wx.redirectTo({ url: '/pages/index/index', })
            }
        })
    },
    getStatusZh(status) {
        let word = "未知"
        switch (status) {
            case '0':
                word = "未支付"
                break;
            case '1':
                word = "支付成功"
                break;
            case '-1':
                word = "支付失败"
                break;
            case '-2':
                word = "申请退款"
                break;
            case '-3':
                word = "已退款"
                break;
            default:
                break;
        }
        return word;
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})