var e = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")), t = require("../../@babel/runtime/helpers/asyncToGenerator"), a = (getApp(), 
require("../../utils/login.js")), r = require("../../utils/common.js");

Page({
    data: {
        showLoding: !0,
        num: "",
        name: "",
        mobile: "",
        uniqid: "",
        tips: "",
        info: {},
        totalprice: "",
        amount: 0,
        data: {},
        ordercart: [],
        orderData: {},
        token: "",
        restTime: "",
        orderid: ""
    },
    onLoad: function(e) {
        this.setData({
            num: e.num,
            name: e.name,
            mobile: e.mobile,
            amount: e.amount,
            uniqid: e.uniqid,
            orderid: e.orderid
        }), e.orderid ? this.getOrderDetail() : this.payCreate();
    },
    input: function(e) {
        this.setData({
            tips: e.detail.value
        });
    },
    payCreate: function() {
        var r = this;
        return t(e.default.mark(function t() {
            var n, s;
            return e.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return r, e.prev = 1, e.next = 4, a.request(a.baseUrl + "shop/order/submit", {
                        uniqid: r.data.uniqid,
                        name: r.data.name,
                        mobile: r.data.mobile,
                        amount: r.data.amount,
                        orderid: r.data.orderid
                    }, !0);

                  case 4:
                    n = e.sent, s = n.data, r.setData({
                        message: n.message,
                        status: s.status,
                        data: s
                    }), r.getOrderDetail(), e.next = 13;
                    break;

                  case 10:
                    e.prev = 10, e.t0 = e.catch(1), console.log(e.t0);

                  case 13:
                  case "end":
                    return e.stop();
                }
            }, t, null, [ [ 1, 10 ] ]);
        }))();
    },
    getOrderDetail: function() {
        var n = this;
        return t(e.default.mark(function t() {
            var s, i, o;
            return e.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return n, s = n.data.orderid || n.data.data.orderid, e.prev = 2, e.next = 5, a.request(a.baseUrl + "shop/order/detail", {
                        orderid: s
                    }, !0);

                  case 5:
                    i = e.sent, o = i.data, console.log("(data.orderData.status", o.orderData.status), 
                    1 == o.orderData.status && wx.redirectTo({
                        url: "/pages/orderPaySuccess/orderPaySuccess?orderid=" + o.orderData.orderid
                    }), o.orderData.updatetime = r.changeTime(new Date(1e3 * o.orderData.updatetime)), 
                    n.setData({
                        showLoding: !1,
                        ordercart: o.ordercart,
                        orderData: o.orderData,
                        token: o.token
                    }), n.calculateResttime(), e.next = 17;
                    break;

                  case 14:
                    e.prev = 14, e.t0 = e.catch(2), console.log(e.t0);

                  case 17:
                  case "end":
                    return e.stop();
                }
            }, t, null, [ [ 2, 14 ] ]);
        }))();
    },
    calculateResttime: function() {
        var e = this;
        if (0 == this.data.orderData.status) {
            var t = r.restTime(this.data.orderData.payexpiredtime);
            t ? (this.setData({
                restTime: t.minute + ":" + t.second
            }), setTimeout(function() {
                e.calculateResttime();
            })) : (this.data.orderData.status = -1, this.setData({
                orderData: this.data.orderData
            }));
        }
    },
    ispay: !1,
    topay: function() {
        var r = this;
        return t(e.default.mark(function t() {
            var n, s, i;
            return e.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    if (n = r, -1 != r.data.orderData.status) {
                        e.next = 3;
                        break;
                    }
                    return e.abrupt("return", !1);

                  case 3:
                    if (r.data.token) {
                        e.next = 5;
                        break;
                    }
                    return e.abrupt("return", !1);

                  case 5:
                    if (!r.ispay) {
                        e.next = 7;
                        break;
                    }
                    return e.abrupt("return", !1);

                  case 7:
                    return r.ispay = !0, n = r, e.prev = 9, e.next = 12, a.request(a.baseUrl + "shop/order/pay", {
                        token: r.data.token,
                        mark: r.data.tips
                    }, !0);

                  case 12:
                    if (s = e.sent, i = s.data.result) {
                        e.next = 17;
                        break;
                    }
                    return n.ispay = !1, e.abrupt("return", !1);

                  case 17:
                    wx.showLoading({
                        title: "正在发起支付..."
                    }), wx.requestPayment({
                        timeStamp: i.timestamp,
                        nonceStr: i.nonceStr,
                        package: i.package,
                        signType: i.signType,
                        paySign: i.paySign,
                        success: function(e) {
                            var t = n.data.orderid || n.data.data.orderid;
                            wx.redirectTo({
                                url: "/pages/orderPaySuccess/orderPaySuccess?orderid=" + t
                            });
                        },
                        fail: function(e) {
                            console.log(e);
                        },
                        complete: function(e) {
                            n.ispay = !1, wx.hideLoading(), console.log("complate", e);
                        }
                    }), e.next = 25;
                    break;

                  case 21:
                    e.prev = 21, e.t0 = e.catch(9), console.log(e.t0), n.ispay = !1;

                  case 25:
                  case "end":
                    return e.stop();
                }
            }, t, null, [ [ 9, 21 ] ]);
        }))();
    }
});