// pages/home/map/map.js
const  app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        location:{},
        mapworkelist:[],
        inputValue:"",
        workelist:[],
        hasMarkers:true,
        workedesc:"",
        status:"",
        show:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
          that.setData({
            location:{
              'latitude':wx.getStorageSync('latitude'),
            'longitude':wx.getStorageSync('longitude')
          }
          })
          that.workDesc();
          that.setData({
            status:app.globalData.status
          })
    
    },

    onHide(){
        wx.stopLocationUpdate({
            success: (res) => {
                console.log(res);
            },
          })
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.stopLocationUpdate({
            success: (res) => {
                console.log(res);
            },
          })
    },

  //网点列表
  workDesc:function(e){
    var that=this;
    var keyword=that.data.inputValue;
    console.log(keyword);
    var latitude=wx.getStorageSync('latitude');
    var longitude=wx.getStorageSync('longitude');
  
    app.api.useApi(app.globalData.baseAppUrl + "api", {
      lat: latitude,
      lng: longitude,
      keyword: keyword,
      }, "get").then(function (res) {
        if (res.code != 200) {
            wx.showLoading({
                title: '网点加载失败'+res.status,
              })
        } else {
         var data=res.message.data.data
        
         that.setData({
            workelist:res.message.data.data
         })
         var maplist=[];
         var workelist=[];
         for (var i = 0; i < data.length; ++i) {
          maplist[i]={
            'id':data[i]['id'],
            'latitude':data[i]['lat'],
            'longitude':data[i]['lng'],
            'iconPath': '/image/address/location.png',
            'width':60,
            'height':60,
          }
          workelist[data[i]['id']]={
            'id':data[i]['id'],
            'latitude':data[i]['lat'],
            'longitude':data[i]['lng'],
            'title':data[i]['title'],
            'address':data[i]['address'],
            'name':data[i]['name'],
            'signing_status':data[i]['signing_status'],
            'distance':data[i]['distance']
          }
           
          if((keyword!='' || keyword!=null)&& i==0){
            
            that.setData({
              workedesc: workelist[data[i]['id']],
              show:true
            })
           }

        }
       

        console.log(data);
        if(data=="" || data ==null || data==[]){
          console.log(33333);
         var workedesc={
            'id':0,
            'latitude':0,
            'longitude':0,
            'title':'暂无数据',
            'address':'暂无数据',
            'name':'暂无数据',
            'signing_status':0,
            'distance':0
          }
          that.setData({
            workedesc: workedesc,
            show:true
          })

         }
        

        that.setData({
          mapworkelist:maplist,
          hasMarkers:true,
          workelist:workelist
        })


        console.log(that.data.mapworkelist)
        }

        wx.hideLoading();

      }).catch(function (err) {
        var workedesc={
           'id':0,
           'latitude':0,
           'longitude':0,
           'title':'暂无数据!',
           'address':'暂无数据!',
           'name':'暂无数据!',
           'signing_status':12,
           'distance':0
         }
         that.setData({
           workedesc: workedesc,
           show:true
         })})
},
bindKeyInput: function (e) {
  this.setData({
    inputValue: e.detail.value
  })
},
markertap(e) {
  console.log('@@@ markertap', e)
var workelist=this.data.workelist;
var markerId=e.detail.markerId;
var workedesc=workelist[markerId];
this.setData({
  workedesc: workedesc,
  show:true
})

},
//设置网点
setworke(e){
  const network_id=e.currentTarget.dataset.id;
  var that=this;
  app.api.useApi(app.globalData.baseAppUrl+"api/user/"+network_id+"/setNetwork",{
  },"post").then(function(res){
    if (res.code == 200) {
      wx.showLoading({
        title: res.message.msg,
      })
      wx.setStorageSync('nid', network_id);
      setTimeout(function () {
        wx.switchTab({
          url: '/pages/tabber/home/index',
        })
        
        }, 1000)

 
     
     
    }
  }).catch(function(err){
    console.log(222);
  })
},
location(e){
 
  console.log(this.data.workedesc.latitude);
  wx.openLocation({
    latitude: Number(this.data.workedesc.latitude),
    longitude: Number(this.data.workedesc.longitude)
  });
}

})