// pages/my/cashier/cashier.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_no: "",
        pageinfo: {},
        price: 0,
        give_price: 0,
        is_online: 1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        this.setData({
            order_no: options.order_no,
            is_online: wx.getStorageSync('is_online')
        });
        app.api.useApi(app.globalData.baseAppUrl + "api/recharge/order", {
            order_no: options.order_no,
        }, "get").then(function (res) {
            console.log(res)
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
                setTimeout(function () {
                    wx.hideLoading({
                        success: (res) => { },
                    })
                }, 1000);
            } else {
                var pageinfo = res.message.data
                that.setData({
                    pageinfo: pageinfo,
                })

                if (pageinfo.total_money <= 0 || parseFloat(pageinfo.total_money) - parseFloat(pageinfo.price) <= 0) {

                    var price = parseFloat(pageinfo.price) - parseFloat(pageinfo.total_money)

                    that.setData({ price: price })
                }
            }
        }).catch(function (err) { console.log(err) })
    },

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

    },

    pay() {
        let that = this
        if (that.data.price > 0) {
            this.wxPay();
        } else {
            this.opay();
        }

    },
    opay(pay_trade_no = "") {
        var that = this;
        var no = that.data.order_no
        app.api.useApi(app.globalData.baseAppUrl + "api/payment", {
            order_no: no,
            pay_trade_no: pay_trade_no
        }, "post").then(function (res) {
            console.log(res)
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
                setTimeout(function () {
                    wx.hideLoading({
                        success: (res) => { },
                    })
                }, 1000);
            } else {
                wx.reLaunch({
                    url: '/pages/order/detail/detail?order_no=' + no + '&subscribe=1'
                })
            }
        }).catch(function (err) { console.log(err) })

        // wx.switchTab({
        //   url: '/pages/tabber/order/index',
        // })
    },
    click(e) {
        // this.pageinfo.price=e.currentTarget.dataset.price;
        this.setData({
            price: e.currentTarget.dataset.price,
            give_price: e.currentTarget.dataset.gprice,
        })
    },
    wxPay() {
        var that = this;
        var no = that.data.price
        app.api.useApi(app.globalData.baseAppUrl + "api/recharge/charge", {
            price: no.toFixed(2),
        }, "post").then(function (res) {
            console.log(res)
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
                setTimeout(function () {
                    wx.hideLoading({
                        success: (res) => { },
                    })
                }, 1000);
            } else {
                var paydata = res.message.data
                wx.requestPayment({
                    timeStamp: paydata.timeStamp,
                    nonceStr: paydata.nonceStr,
                    package: paydata.package,
                    signType: paydata.signType,
                    paySign: paydata.paySign,
                    success(res) {
                        console.log(res);
                        if (res.errMsg == "requestPayment:ok") {

                            that.opay(paydata.pay_trade_no);
                        }
                    },
                    fail(res) {
                        wx.showLoading({
                            title: "失败，请重试！",
                        })
                        setTimeout(function () {
                            wx.hideLoading({
                                success: (res) => { },
                            })
                        }, 1000);
                        console.log(res)
                    }
                })

            }
        }).catch(function (err) { console.log(err) })

    },

    back: function () {
        wx.redirectTo({
            url: '/pages/order/detail/detail?order_no=' + this.data.order_no + '&subscribe=1',
        })
    }
})