// more.js 
var app = getApp(),
    { hostname, hostadmin } = app.globalData;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        applyName: '',
        applyPhone: '',
    },

    //区会员
    member: function () {
        wx.navigateTo({
            url: 'recharge/recharge',
        })
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        var showShop = wx.getStorageSync("showShop");
        that.setData({
            showShop: showShop == undefined ? false : showShop
        })
        //调用应用实例的方法获取全局数据
        app.getUserInfo(function (userInfo, imgUrlBefore) {
            that.setData({
                userInfo: wx.getStorageSync('userInfo'),
                openid: wx.getStorageSync('openid')
            })
            wx.request({
                url: app.globalData.hostname + 'index_info',
                data: '',
                header: {},
                method: 'GET',
                dataType: '',
                success: function (res) {
                    console.log(res,'当前地址');
                    if (res.data.code == 200) {
                        // console.log(res);
                        wx.setStorageSync('more_info', res.data.data);
                        that.setData({
                            more_info: res.data.data,
                        })
                    }
                },
            })
        })

        //获取会员等级
        wx.request({
            url: hostname+'get_grade',
            data: { user_id: wx.getStorageSync('openid')},
            success:(res)=>{
                console.log(res);
                that.setData({
                    goldtitle:res.data.data[0].title
                })
            }   
        })



        // this.getRecommendGoods()
    },
    seeOfficialWeb: function () {
        var indexurl = this.data.more_info.indexurl
        wx.showModal({
            title: '欢迎访问我们的网站',
            content: indexurl,
            confirmText: '复制',
            success: function (res) {
                if (res.confirm) {
                    wx.setClipboardData({
                        data: indexurl,
                        success: function () {
                            wx.showToast({
                                title: '已复制',
                            })
                        }
                    })
                }
            }
            // showCancel: false,
        })
    },
    seeGoodsDetail: function (e) {
        var goods_id = e.currentTarget.dataset.goodsid;
        console.log(goods_id);
        wx.navigateTo({
            url: '../index/goodsdetail/goodsdetail?id=' + goods_id,
        })
    },
    seeAboutUs: function () {
        wx.navigateTo({
            url: 'aboutus/aboutus',
        })
    },
    //查看我的订单
    seeOrder: function (e) {
        var typekind = e.currentTarget.dataset.typekind;
        wx.navigateTo({
            url: '../order/order?typekind=' + typekind,
        })
    },
    navCoupon: function () {
        wx.navigateTo({
            url: 'coupon/coupon',
        })
    },
    navMyDownline() {
        wx.navigateTo({
            url: './downLine1/downLine'
        })
    },
    switchCart() {
        wx.switchTab({
            url: '../car/car',
        })
    },
    // 获取下线
    getDownline() {
        let that = this
        wx.request({
            url: hostname + 'share_friend',
            data: {
                user_id: wx.getStorageSync('openid')
            },
            success: (res) => {
                if (res.data.code == 200) {
                    let downline = res.data.data.one.length
                    res.data.data.two.forEach((value) => {
                        downline += value.length
                    })
                    that.setData({ downline })
                }
                wx.hideLoading()
            }
        })
    },
    // 获取推荐商品
    getRecommendGoods() {
        let that = this
        wx.request({
            url: hostname + 'like_goods',
            success: function (res) {
                if (res.data.code == 200) {
                    that.setData({ recommendInfo: { data: res.data.data, imgUrl: wx.getStorageSync('imgUrlBefore') } })
                }
            },
            fail: function (res) { },
            complete: function (res) { },
        })
    },
    //查看拼团订单
    seeGroupOrder: function (e) {
        wx.navigateTo({
            url: '../order/order?orderKind=group',
        })
    },
    enter: function (e) {
        var kind = e.currentTarget.dataset.kind;
        wx.navigateTo({
            url: kind + '/' + kind,
        })
    },
    // 查看我的资讯收藏
    seeFindCollect: function () {
        wx.navigateTo({
            url: '../find/find_collect/find_collect',
        })
    },
    seeShopAddress: function () {
        var more_info = wx.getStorageSync('more_info');
        var longitude = more_info.longitude - 0;
        var latitude = more_info.latitude - 0;
        console.log(latitude, longitude);
        wx.openLocation({
          latitude: latitude,
          longitude: longitude,
            scale: 28,
            name: more_info.pro_title,
            address: more_info.address
        })
    },
    makeCall: function (e) {
        var phoneNumber = this.data.more_info.phone;
        wx.makePhoneCall({
            phoneNumber: phoneNumber,
        })
    },
    navAllOrder() {
        wx.navigateTo({
            url: '../order/order?tabIndex=0',
        })
    },
    // 获取优惠券
    getCouponList() {
        let that = this
        wx.request({
            url: hostname + 'coupon_list',
            data: {
                user_id: wx.getStorageSync('openid'),
            },
            success: function (res) {
                if (res.data.code == 200) {
                    let couponList = res.data.data || []
                    that.setData({ coupon: couponList.length })
                }
            },
            fail: function (res) { },
            complete: function (res) { },
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        that.judgeSign(that);
        this.getCouponList()
        this.getDownline()
        this.getTotalCount()
        this.getCollectionList()
        //获取积分并判断是否变化
        app.getScore(function (score) {
            var lastScoreNum = that.data.lastScoreNum;
            var showScoreChange = ((lastScoreNum != undefined) && (lastScoreNum - score != 0)) ? true : false;
            if (showScoreChange) {
                that.setData({
                    scoreChange: (score - lastScoreNum > 0 ? "+" : '') + (score - lastScoreNum)
                })
            }
            that.setData({
                scoreNum: score,
                lastScoreNum: score,
                showScoreChange: showScoreChange,
            })
        })
        var userInfo = wx.getStorageSync('userInfo');
        if (userInfo.share_status == 0) {
            setTimeout(function () {
                app.getLatestUserInfo(that);
            }, 2000)
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.setData({
            showScoreChange: false,
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    navAddress() {
        wx.chooseAddress({

        })
    },
    // 获取总消费额
    getTotalCount() {
        let that = this
        wx.request({
            url: hostadmin + 'admin/api/getUserinfo',
            data: {
                openid: wx.getStorageSync('realOpenid'),
                nickname: wx.getStorageSync('userInfo').nickname,
                headimgurl: wx.getStorageSync('userInfo').avatarUrl,
            },
            success: function (result) {
                if (result.statusCode == 200) {
                    that.setData({ totolCount: result.data.count_sale })
                }
            },
            complete: (res) => {
            }
        })
    },
    // 获取收藏列表
    getCollectionList() {
        wx.showLoading({
            title: '加载中...',
        })
        let that = this
        wx.request({
            url: hostname + 'mine_collect',
            data: {
                user_id: wx.getStorageSync('openid')
            },
            success: function (res) {
                if (res.data.code == 200) {
                    let collection = res.data.data
                    that.setData({
                        collection,
                        url: wx.getStorageSync('imgUrlBefore'),
                    })
                    wx.hideLoading()
                }
            },
            fail: function (res) { },
            complete: function (res) { },
        })
    },
    seeDownLine: function (e) {
        var that = this;
        var share_status = e.currentTarget.dataset.share_status;
        console.log(share_status);
        if (share_status != 1) {
            that.setData({
                showApply: true,
            })
            return;
        }
        wx.navigateTo({
            url: "downLine/downLine?withdraw=true"
        })
    },
    //获取申请输入的姓名和电话
    //   getKey: function (e) {
    //       var value = e.detail.value;
    //       var kind = e.currentTarget.dataset.kind;
    //       var applyData = this.data.applyData;
    //       applyData[kind] = value;
    //       this.setData({
    //           applyData: applyData,
    //       })
    //   },
    //姓名
    getName: function (e) {
        var value = e.detail.value;
        this.setData({
            applyName: value,
        })
    },
    //手机号
    getPhone: function (e) {
        var value = e.detail.value;
        this.setData({
            applyPhone: value,
        })
    },

    //取消或确认提交申请
    whetherApply: function (e) {
        var that = this;
        var kind = e.currentTarget.dataset.kind;
        if (kind == 'cancle') {
            that.setData({
                showApply: false,
            })
        } else {
            //   var applyData = that.data.applyData;

            var errorContent;
            var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
            var phoneValid = reg.test(that.data.applyPhone);
            if (that.data.applyName == undefined || that.data.applyName == '') {
                errorContent = '请输入姓名';
            } else if (that.data.applyPhone == undefined || that.data.applyPhone == '') {
                errorContent = '请输入电话';
            } else if (!phoneValid) {
                errorContent = '请输入正确的手机号码';
            }
            if (errorContent) {
                wx.showModal({
                    title: '温馨提示',
                    content: errorContent,
                    showCancel: false,
                })
                return false;
            }
            // console.log(applyData,'applyData');
            // return false;
            wx.request({
                url: app.globalData.hostname + 'share_apply',
                data: {
                    user_id: wx.getStorageSync('openid'),
                    name: that.data.applyName,
                    phone: that.data.applyPhone
                },
                method: 'GET',
                dataType: 'json',
                success: function (res) {
                    if (res.data.code == 200) {
                        wx.showToast({
                            title: '已提交申请',
                            duration: 1500,
                            success: function () {
                                that.setData({
                                    showApply: false,
                                })
                            }
                        })
                    }
                },
                fail: function (res) { },
                complete: function (res) { },
            })
        }
    },

    //游戏
    seeGamesArea: function (e) {
        wx.navigateTo({
            url: 'game_area/game_area',
        })
    },
    // 签到
    sign: function () {
        var that = this;
        if (that.data.isSign) {
            wx.showModal({
                title: '温馨提示',
                content: '今天您已签到',
                showCancel: false,
            })
        } else {
            wx.request({
                url: app.globalData.hostname + 'sign',
                method: 'GET',
                data: { user_id: wx.getStorageSync('openid') },
                dataType: 'json',
                success: function (signRes) {
                    that.setData({
                        isSign: true,
                    })
                    wx.showToast({
                        title: signRes.data.info,
                    })
                },
            })
        }
        // wx.navigateTo({
        //   url: 'sign/sign',
        // })s
    },
    //判断是否已签到
    judgeSign: function (that) {
        if (!that.data.isSign) {
            wx.request({
                url: app.globalData.hostname + 'vers_sign',
                data: { user_id: wx.getStorageSync('openid') },
                method: 'GET',
                dataType: 'json',
                success: function (judgeRes) {
                    console.log(judgeRes);
                    that.setData({
                        isSign: judgeRes.data.data == 1 ? true : false //待定
                    })
                },
                fail: function (res) { },
                complete: function (res) { },
            })
        }
    },
    //大转盘
    seeLuchkDraw: function () {
        wx.navigateTo({
            url: 'luckydraw/luckydraw',
        })
    },
    //查看推客记录
    seePush: function () {
        wx.navigateTo({
            url: 'push/push',
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: app.globalData.tuikeTitle,
            path: '/pages/index/index?tuike_uid=' + wx.getStorageSync('openid'),
            success: function (res) {
                // 转发成功
                wx.showToast({
                    title: '分享成功',
                })
            },
            fail: function (res) {
                // 转发失败
                wx.showToast({
                    title: '分享失败',
                    image: '../../../images/warning.png'
                })
            }
        }
    }
})