// 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: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    onShow: function (options) {
        this.setData({
            islocation: wx.getStorageSync('islocation'),
            status: app.globalData.status,
           
        })
        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 () {
        
    },
    //网点详情
    workDesc: function (nid="") {
        var that = this;
            
        if (!nid) {
            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,
        }, "get").then(function (res) {
            if (res.code != 200) {
                wx.showLoading({
                    title: '网点加载失败' + res.status,
                })
            } else {
                that.setData({
                    worke: res.message.data
                })
            }

            wx.hideLoading();

        }).catch(function (err) {
            console.log(222);
        })
    },
    toMap() {
        wx.navigateTo({
            url: '/pages/home/map/map',
        })
    },
    placeOrder() {
        if (this.data.islocation) {
            this.getLocation()
        }
        var parmise=this.data.worke;

        wx.navigateTo({
            url: '/pages/order/placeOrder/placeOrder?parmise='+JSON.stringify(parmise),
        })
    },


    close() {
        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;
            }
        })
    }



})