var api = require('../api/api.js'); 
var util=require('../utils/util.js');
var QQMapWX = require('../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
var app = getApp();

function getCity(){ 
  return new Promise(function(resolve, reject) {  
  qqmapsdk = new QQMapWX({
    key: app.globalData.mapKey //这里自己的key秘钥进行填充
  });

  wx.getSetting({
    success: (res) => {
      console.log(JSON.stringify(res))
      // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
      // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
      // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
      if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
        wx.showModal({
          title: '请求授权当前位置',
          content: '需要获取您的地理位置，请确认授权',
          success: function (res) {
            if (res.cancel) { 
              reject(1);
            } else if (res.confirm) {
              wx.openSetting({
                success: function (dataAu) {
                  if (dataAu.authSetting["scope.userLocation"] == true) {
                    wx.showToast({
                      title: '授权成功',
                      icon: 'success',
                      duration: 1000
                    })
                    //再次授权，调用wx.getLocation的API
                   getLocation().then((res)=>{
                      resolve(res)
                    }).catch((res)=>{
                      reject(res)
                    });

                  } else { 
                   reject(1);
                  }
                }
              })
            }
          }
        })
      }  else {
        //调用wx.getLocation的API
        getLocation().then((res)=>{
          resolve(res)
        }).catch((res)=>{
          reject(res)
        });
      }
    }
  })
  })
}
// 微信获得经纬度
function getLocation () { 
  return new Promise(function(resolve, reject) {
  wx.getLocation({
    type: 'wgs84',
    success: function (res) { 
      var latitude = res.latitude
      var longitude = res.longitude 
      //console.info(res)
      qqmapsdk.reverseGeocoder({
        location: {
          latitude: latitude,
          longitude: longitude
        }, 
        success: function (res) {
          let province = res.result.ad_info.province
          let city = res.result.ad_info.city 
          let address=res.result.address; //判断是否有门店服务
          app.globalData.city=city;
            util.request(api.AuthGetShop,{lng:longitude,lat:latitude},"GET").then(response=>{                
           //console.info( response) 
              if(response.errno==0){
                var addressInfo={
                  address:address,
                  lng:longitude,
                  lat:latitude,
                  shopId:response.data.storeId
                }
                wx.setStorageSync('addressInfo', addressInfo)
                resolve(addressInfo)
              }
              else 
                reject(3)
            }).catch(res=>{  
              reject(4)
            })  
        },
        fail: function (res) { 
          reject(5)
        } 
      }); 
    },
    fail: function (res) {
      console.log('fail' + JSON.stringify(res))
      reject(3)
    }
  })
})
}
 
 


module.exports = {
  getCity:getCity,
  getLocation:getLocation
}