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) {
                if (t.data.cars) {
                    var a = [];
                    a = t.data.cars;
                    for (var i = 0; i < a.length; i++) a[i].selected = !1;
                }
                e.setData({
                    carlist: a
                });
            }
        });
    },
    selectList: function(t) {
        var a = [], e = [], i = t.currentTarget.dataset.index, s = this.data.carlist, c = s[i].selected;
        s[i].selected = !c;
        for (var n = 0; n < this.data.carlist.length; n++) 1 == this.data.carlist[n].selected && (a.push(this.data.carlist[n].selected), 
        e.push(this.data.carlist[n].id));
        this.setData({
            carlist: s,
            selected: c,
            totalnum: a.length,
            ids: e.join(",")
        }), this.getTotalPrice();
    },
    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() {}
});