const app = getApp()
Page({
        data: {
                is_mobile:false,
                ordoerState1:false,
                order1:0,
                oneone:[],
                twotwo:[],
                threethree:[],
                fourfour:[],
                ninenine:[],
                ordoerState2: false,
                order2: 0,
                ordoerState3: false,
                order3: 0,
                ordoerState4: false,
                order4: 0,
                ordoerState9: false,
                order9: 0,
                imgUrl: app.globalData.imgUrl,
                popup_state: false,
                arr: [1, 2, 3],
                user: [],
                orderList: [],
                stateArr: [{
                        id: 1,
                        name: '待付款',
                        icon: 'aicon44__1.png'
                }, {
                        id: 2,
                        name: '待发货',
                        icon: 'aicon44__2.png'
                }, {
                        id: 3,
                        name: '已发货',
                        icon: 'aicon44__3.png'
                }, {
                        id: 4,
                        name: '待评价',
                        icon: 'aicon44__4.png'
                }],
        },
        //点击查看积分
        look_jifen(){
                wx.navigateTo({
                        url: './personal_center/personal_center',
                })
        },
        //获取购物车数量
        fetchCartNum() {
                let url = '/wxapp/cart/index'
                app.post(url).then(r => {
                        if (r.code == 1) {
                                if (r.data.cartNum > 0) {
                                        wx.showTabBarRedDot({
                                                index: 1,
                                                success: function (red) {
                                                        wx.setTabBarBadge({
                                                                index: 1,
                                                                text: r.data.cartNum.toString(),
                                                        })
                                                }
                                        })
                                } else {
                                        wx.hideTabBarRedDot({
                                                index: 1
                                        })
                                }
                        }
                })
        },
        //查看我的积分
        jump_jifen(){
                wx.navigateTo({
                        url: './personal_center/personal_center',
                })
        },
        //跳转至会员中心
        jump_vip() {
                wx.navigateTo({
                        url: '../vip/vip_index/vip_index',
                })
        },
        //获取个人信息
        fetchPersonal() {
                let url = '/wxapp/user/index';
                app.post(url).then(r => {
                        console.log(r);
                        if(r.code==1){
                                if(r.data.user.mobile!=''){
                                        this.setData({
                                                is_mobile:false
                                        })
                                }else{
                                        this.setData({
                                                is_mobile:true
                                        })
                                }
                        this.setData({
                                user: r.data
                        });
                        }
                });
        },
        //绑定手机
        go_bangding_mobile(){
                wx.navigateTo({
                        url: './modifyPhone/modifyPhone',
                })
                // this.setData({
                //         is_mobile:true
                // })
        },
        //跳转售后
        jump_afterMarket() {
                wx.navigateTo({
                        url: './afterMarketList/afterMarketList',
                })
        },
        //签到
        signIn() {
                this.setData({
                        popup_state: true
                })
        },
        //签到成功
        back_person() {
                let url = '/wxapp/user/sign';
                app.post(url).then(r => {
                        if (r.code == 1) {
                                wx.showToast({
                                        title: '签到成功',
                                        icon: 'none'
                                })
                                this.fetchPersonal();
                        }else{
                                wx.showToast({
                                        title: '您今天已经签到过',
                                        icon:'none'
                                })
                        }
                });
                this.setData({
                        popup_state: false
                });
        },
        cancleMask() {
                this.setData({
                        popup_state: false
                })
        },
        onLoad: function() {
                this.fetchOrderlist();
                this.fetchPersonal();
                console.log(this.data)
        },
        /**
     * 生命周期函数--监听页面显示
     */
        onShow: function () {
                this.fetchPersonal();
                this.fetchCartNum();
        },
        // 消息中心
        get_message() {
                wx.navigateTo({
                        url: '../my/activityInformation/activityInformation',
                })
        },
        //渲染订单列表
        fetchOrderlist() {
                let url = '/wxapp/order/index'
                let that=this
                let oneone=[]
                let twotwo=[]
                let threethree=[]
                let fourfour=[]
                let ninenine=[]
                let orderState1=that.data.ordoerState1
                let orderState2 = that.data.orderState2
                let orderState3 = that.data.orderState3
                let orderState4 = that.data.orderState4
                let orderState9 = that.data.orderState9

                app.post(url).then(r => {
                        if (r.code == 1) {
                                r.data.forEach(function(ele,index){
                                        if(ele.status==1){
                                                oneone.push(ele)
                                        }else if(ele.status==2){
                                                twotwo.push(ele)
                                        } else if (ele.status == 3) {
                                                threethree.push(ele)
                                        } else if (ele.status == 4) {
                                                fourfour.push(ele)
                                        } else if (ele.status == 9) {
                                                ninenine.push(ele)
                                        }
                                })

                                console.log(oneone, twotwo, threethree, fourfour, ninenine)
                                if (oneone.length > 0) {
                                        orderState1 = true
                                } else {
                                        orderState1 = false
                                }
                                if (twotwo.length > 0) {
                                        orderState2 = true
                                } else {
                                        orderState2 = false
                                }
                                if (threethree.length > 0) {
                                        orderState3 = true
                                } else {
                                        orderState3 = false
                                }
                                if (fourfour.length > 0) {
                                        orderState4 = true
                                } else {
                                        orderState4 = false
                                }
                                if (ninenine.length > 0) {
                                        orderState9 = true
                                } else {
                                        orderState9 = false
                                }
                                that.setData({
                                        order1: oneone.length,
                                        order2: twotwo.length,
                                        order3: threethree.length,
                                        order4: fourfour.length,
                                        order9: ninenine.length,
                                        orderState1: orderState1,
                                        orderState2: orderState2,
                                        orderState3: orderState3,
                                        orderState4: orderState4,
                                        orderState9: orderState9
                                })
                        }
                })
        },
        // 修改个人信息
        getamend_person() {
                wx.navigateTo({
                        url: '../my/modifyPersonInformation/modifyPersonInformation',
                })
        },
        // 兑换码充值
        get_activity() {
                wx.navigateTo({
                        url: '../my/charger_code/charger_code',
                })
        },
        //点击积分抵现
        integral() {
                wx.navigateTo({
                        url: '../my/personal_center/personal_center'
                })
        },
        // 待付款
        get_obligation1(e) {
                let index = e.currentTarget.dataset.index * 1 + 2
                let status = e.currentTarget.dataset.id
                let that=this
                wx.navigateTo({
                        url: '../my/myOrder/myOrder?index=' + 3 + '&status=' + 1
                })
        },
        // 待发货
        get_obligation2(e) {
                let index = e.currentTarget.dataset.index * 1 + 2
                let status = e.currentTarget.dataset.id
                let that = this
                wx.navigateTo({
                        url: '../my/myOrder/myOrder?index=' + 4 + '&status=' + 2
                })
        },
        // 待收货
        get_obligation3(e) {
                let index = e.currentTarget.dataset.index * 1 + 3
                let status = e.currentTarget.dataset.id
                let that = this
                wx.navigateTo({
                        url: '../my/myOrder/myOrder?index=' + 5 + '&status=' +3
                })
        },
        // 待评价
        get_obligation4(e) {
                let index = e.currentTarget.dataset.index * 1 + 2
                let status = e.currentTarget.dataset.id
                let that = this
                wx.navigateTo({
                        url: '../my/myOrder/myOrder?index=' + 6 + '&status=' + 4
                })
        },
        // 售后
        get_sale() {
                wx.navigateTo({
                        url: '../my/orderDetail/orderDetail',
                })
        },
        //优惠券
        coupons() {
                wx.navigateTo({
                        url: '../my/coupons/coupons',
                })
        },
        // 储值有礼
        get_recharge(e) {
                wx.navigateTo({
                        url: '../my/balance _center2/balance _center2?id=' + e.currentTarget.dataset.id,
                })
        },
        // 分享有礼
        get_share() {
                wx.navigateTo({
                        url: 'invitationPolite/invitationPolite',
                })
        },
        // 我的收藏
        get_collection() {
                wx.navigateTo({
                        url: '../my/myCollection/myCollection',
                })
        },
        // 地址管理
        get_address() {
                wx.navigateTo({
                        url: '../my/myAddress/myAddress',
                })
        },
        // 常见问题
        get_issue() {
                wx.navigateTo({
                        url: '../my/editAddress2/editAddress2',
                })
        },
        //用户指南
        suggestion() {
                wx.navigateTo({
                        url: '../my/suggestion/suggestion',
                })
        }
})