var t = require("../../utils/network.js");

Page({
    data: {
        num: 1,
        selectnum: [],
        ouid: "",
        carlist: [],
        hasList: !1,
        totalPrice: 0,
        selected: !1,
        totalnum: 0,
        ids: ""
    },
    bindMinus: function(a) {
        var e = this, i = a.currentTarget.dataset.index, s = this.data.carlist, c = parseInt(s[i].count), n = s[i].id;
        s[i].count > 1 && (c -= 1, t.POST({
            url: "Pay/update_car",
            header: "application/x-www-form-urlencoded",
            params: {
                count: c,
                id: n
            },
            success: function(t) {
                e.setData({
                    carlist: s
                }), e.getcartlist();
            }
        })), this.setData({
            carlist: s
        });
    },
    bindPlus: function(a) {
        var e = this, i = a.currentTarget.dataset.index, s = this.data.carlist, c = parseInt(s[i].count), n = s[i].id;
        c += 1, t.POST({
            url: "Pay/update_car",
            header: "application/x-www-form-urlencoded",
            params: {
                count: c,
                id: n
            },
            success: function(t) {
                e.setData({
                    carlist: s
                }), e.getcartlist(), e.getTotalPrice();
            }
        });
    },
    bindManual: function(t) {
        var a = t.detail.value;
        this.setData({
            num: a
        });
    },
    onLoad: function(t) {
        this.getTotalPrice();
    },
    getcartlist: function(a) {
        var e = this;
        t.GET({
            url: "Pay/my_cars",
            header: "application/x-www-form-urlencoded",
            params: {
                ouid: e.data.ouid
            },
            success: function(t) {
             let totalnum=0;
             let ids='';
             var a = [];
                if (t.data.cars) {
                   
                    a = t.data.cars;
                    for (var obj of a){
                     if (obj.selected){
                      totalnum++;
                      ids += obj.id+','
                     }
                    } 
                }
                e.setData({
                    carlist: a,
                    totalnum: totalnum,
                    ids:ids
                });
             e.getTotalPrice();
            }
        });
    },
    selectList: function(e) {
     let that=this;
     t.POST({
      url: "pay/selected",
      header: "application/x-www-form-urlencoded",
      params: {
       ouid: that.data.ouid,
       id: e.currentTarget.dataset.id
      },
      success: function (e) {
        that.getcartlist();
      }
     });   
    },
    getTotalPrice: function() {
        for (var t = this.data.carlist, a = 0, e = 0; e < t.length; e++) t[e].selected && (a += t[e].count * t[e].pprice);
        this.setData({
            carlist: t,
            totalPrice: a.toFixed(2)
        });
    },
    deleteList: function(a) {
        var e = this, i = a.currentTarget.dataset.index, s = this.data.carlist;
        wx.showModal({
            title: "确认删除商品吗",
            content: "确定要删除该商品吗?",
            success: function(a) {
                a.confirm ? t.POST({
                    url: "Pay/delete_car",
                    header: "application/x-www-form-urlencoded",
                    params: {
                        ouid: e.data.ouid,
                        id: s[i].id
                    },
                    success: function(t) {
                        s.splice(i, 1), e.data.totalnum, e.setData({
                            carlist: s,
                            totalnum: 0,
                            totalPrice: 0
                        }), e.getcartlist();
                    }
                }) : a.cancel && console.log("用户点击取消"), e.getTotalPrice();
            }
        }), s.length ? this.getTotalPrice() : this.setData({
            carlist: s
        });
    },
    topay: function() {
        0 == this.data.totalnum ? wx.showToast({
            title: "你还未选择商品",
            icon: "none"
        }) : wx.navigateTo({
            url: "../my/pay/pay?ids=" + this.data.ids
        });
    },
    onReady: function() {},
    onShow: function() {
        var t = this;
        this.setData({
            carlist: [],
            totalnum: 0,
            totalPrice: 0
        }), wx.getStorage({
            key: "ouid",
            success: function(a) {
                wx.showLoading({
                    title: "加载中..."
                }), t.setData({
                    ouid: a.data
                }), t.getcartlist(), wx.hideLoading();
            }
        });
    },
    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;
    },
});