// pages/tabber/home/index.js
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        menuList: [
            // {
            //     url: '/image/home/ruhexiadan.png',
            //     text: '如何下单'
            // },
            // {
            //     url: '/image/home/changjianwenti.png',
            //     text: '常见问题'
            // }
            // ,
            // {
            //     url: '/image/home/xidifanwei.png',
            //     text: '洗涤范围'
            // }

        ],
        current: 3,
        btnInfo: app.globalData.btnInfo,
        topShow: true,
        worke: [],
        status: "",
        islocation: "",
        
        hasUserInfo: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var topShow=wx.getStorageSync("topShow");
        console.log(topShow);
        if(topShow===""){
            wx.setStorageSync('topShow',true);
        }
        // this.getUser()
      
    },
    onShow: function (options) {

        var topShow=wx.getStorageSync('topShow');
        console.log(topShow);
        this.setData({
            islocation: wx.getStorageSync('islocation'),
            status: app.globalData.status,
            topShow:topShow
           
        })
        var nid =wx.getStorageSync('nid');
        var token =wx.getStorageSync('token');
            if(!nid || !token){
                app.login().then(res => {
                    console.log(res)
                    this.setData({
                        token: res.message.token,
                        nid: res.message.network_id
                    })
                    this.workDesc(res.message.network_id);
                })
            
            }else{
                this.workDesc(nid);
            }
      
    },
    onReady: function () {
        
    },
    /**
     * 获取用户信息
     */
    getUser(){
        var that=this;
        app.api.useApi(app.globalData.baseAppUrl+"api/user/getUser","","get").then((res)=>{
            if(res.code==200){
                wx.hideLoading();
                that.setData({
                    hasUserInfo:res.message.data.avatar!=""?true:false
                })
            }else{
                that.getUser()
            }
        }).catch((err)=>{
            that.getUser()
        })
    },
    getUserProfile(e) {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
        // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                this.setData({
                    hasUserInfo: true
                })
                this.placeOrder()
            }
        })
    },
    //网点详情
    workDesc: function (nid="") {
        var that = this;
            
        if (nid<1) {
            that.setData({
                worke: { 'title': '请选择网点', 'institution_id': 0, 'business_status': 3, 'distance': 0 }
            })
            return false;
        }
        app.api.useApi(app.globalData.baseAppUrl + "api/network/desc", {
            network_id: nid,
            lat:wx.getStorageSync('latitude'),
            lng:wx.getStorageSync('longitude')
        }, "get").then(function (res) {
            console.log(res);
            if (res.code != 200) {
                wx.showLoading({
                    title: '重新选择网点' + res.status,
                })
                setTimeout(function () {
                    wx.hideLoading();
                   wx.navigateTo({
                     url: '/pages/home/map/map',
                   })
                }, 1000)

            } else {
                that.setData({
                    worke: res.message.data
                })
            }

           

        }).catch(function (err) {
            console.log(err);
        })
    },
    toMap() {
        wx.navigateTo({
            url: '/pages/home/map/map',
        })
    },
    placeOrder() {
        if (this.data.islocation) {
            this.getLocation()
        }
        var parmise=this.data.worke;
        if(wx.getStorageSync('nid')&&wx.getStorageSync('nid')!=0){
            wx.navigateTo({
                url: '/pages/order/placeOrder/placeOrder?parmise='+JSON.stringify(parmise),
            })
        }else{
            wx.showToast({
                title: '请先选择网点',
                icon: 'none',
                duration: 2000
            })
        }
    },


    close() {
        wx.setStorageSync('topShow', false);
        this.setData({
            topShow: false
        })
    },
    onReady: function () {
        this.toast = this.selectComponent("#toast");
    },
    listenerLogin: function () {
        this.toast.showToast('领取成功');
    },
    rightClick(e) {
        console.log(1);
    },

    getLocation(e) {
        var that = this;
        wx.getLocation({
            success(res) {
                console.log('开启后台定位', res)
              
                    console.log('location change', res)
                    that.setData({
                        location: res,
                        islocation: true,
                    })

                    app.api.useApi(app.globalData.baseAppUrl + "api").then(function (res) {
                        console.log(res)

                    }, function (error) {
                        console.log(error);
                    })
                
            },
            fail(res) {
                console.log('开启后台定位失败', res);
                that.setData({
                    islocation: true
                })
                wx.showLoading({
                    title: '开启后台定位失败,请重试。',
                })
                return false;
            }
        })
    }



})