var t = require("../../../utils/network.js");
const app=getApp();
Page({
    data: {
        type: "",
        ids: [],
        goods: [],
        totalmoney: 0,
        address: [],
        oid: ""
    },
    onLoad: function(t) {
        var a = this;
        console.log(t), wx.getStorage({
            key: "ouid",
            success: function(t) {
                a.setData({
                    ouid: t.data
                });
            }
        }), this.setData({
            type: t.type,
            ids: t.ids ? t.ids.split(",") : "",
            id: t.id
        }), this.getTotalPrice();
    },
    gocomment: function() {
        wx.navigateTo({
            url: "../comment/comment"
        });
    },
    usermsg: function(t) {
        var a = t.detail.value;
        console.log(a), this.setData({
            user_msg: a
        }), console.log(this.data.user_msg);
    },
    getgoods: function() {
        var a = this;
        t.POST({
            url: "Pay/confirm_order",
            header: "application/x-www-form-urlencoded",
            params: {
                ids: this.data.ids
            },
            success: function(t) {
                (t.data.state = "success") && (a.setData({
                    goods: t.data.goods
                }), a.getTotalPrice());
            }
        });
    },
    getTotalPrice: function() {
        for (var t = this.data.goods, a = 0, e = 0; e < t.length; e++) a += parseInt(t[e].count) * parseFloat(t[e].pprice);
        this.setData({
            goods: t,
            totalmoney: a.toFixed(2)
        });
    },
    onReady: function() {},
    goTooder: function() {
        var a = this;
        0 == this.data.address.length ? wx.showToast({
            title: "请选择地址",
            icon: "none"
        }) : t.POST({
            url: "Pay/create_order",
            header: "application/x-www-form-urlencoded",
            params: {
                ids: this.data.ids,
                ouid: this.data.ouid,
                total: this.data.totalmoney,
                name: this.data.address.userName,
                tel: this.data.address.telNumber,
                addr: this.data.address.provinceName + this.data.address.cityName + this.data.address.countyName +  
                this.data.address.detailInfo,
                leave: this.data.user_msg ? this.data.user_msg : "",
                share_id: app.globalData.share_id ? app.globalData.share_id :0
            },
            success: function(t) {
            
                wx.showLoading({
                    title: "处理中..."
                }), "success" == t.data.state && a.setData({
                    oid: t.data.oid
                }), wx.hideLoading(), a.pay_order();
            }
        });
    },
    pay_order: function() {
     let that=this;
        t.POST({
            url: "Pay/pay_order",
            header: "application/x-www-form-urlencoded",
            params: {
                ouid: this.data.ouid,
                oid: this.data.oid,
                money: this.data.totalmoney
            },
            success: function(t) {
                t.data && wx.requestPayment({
                    timeStamp: t.data.timeStamp,
                    nonceStr: t.data.nonceStr,
                    package: t.data.package,
                    signType: t.data.signType,
                    paySign: t.data.paySign,
                    success: function(t) {
                        wx.redirectTo({
                            url: "../delivery/delivery"
                        });
                    },
                    fail: function(t) {
                     wx.redirectTo({
                      url: '/pages/index/paydetail/paydetail?id=' + that.data.oid + '&type=0'
                     })
                    }
                });
            },
            fail:function(){
            

            }
        });
    },
    onShow: function() {
        this.getgoods(), this.getTotalPrice();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
 onShareAppMessage: function (options) {
  let that = this;
  let ouid;
  // 设置菜单中的转发按钮触发转发事件时的转发内容
  var shareObj = {
   title: '嗒咕商场', // 默认是小程序的名称(可以写slogan等)
   path: '/pages/start/start?share_id=' + wx.getStorageSync('ouid'), // 默认是当前页面,必须是以‘/’开头的完整路径
   imgUrl: '',//自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径,支持PNG及JPG,不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
   success: function (res) {
    // 转发成功之后的回调
    if (res.errMsg == 'shareAppMessage:ok') { }
   },
   fail: function () {
    // 转发失败之后的回调
    if (res.errMsg == 'shareAppMessage:fail cancel') {
     // 用户取消转发
    } else if (res.errMsg == 'shareAppMessage:fail') {
     // 转发失败,其中 detail message 为详细失败信息
    }
   },
   complete: function () {
    // 转发结束之后的回调(转发成不成功都会执行)
   }
  }
  return shareObj;
 },
    getaddress: function() {
        var t = this;
        wx.chooseAddress ? wx.chooseAddress({
            success: function(a) {
                a ? t.setData({
                    address: a
                }) : wx.showToast({
                    title: "未选择地址",
                    icon: "none"
                });
            },
            fail: function(t) {},
            complete: function(t) {}
        }) : wx.showModal({
            title: "提示",
            content: "当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。"
        });
    }
});