var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var map = require('../../../utils/map.js');
var app = getApp();

Page({
  data: {
    addressList: [],
    total: 0, 
    addressName:"",
    isShowServiceAddress:false
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
   if(prevPage.route == "pages/index/index"){ 
    this.setData({
      isShowServiceAddress:true
    })
     }

    this.getUserLocation();
  },
  shaxin(){
    this.getUserLocation();
  },
  getUserLocation: function () {
   map.getUserLocation();
   let that=this;
   setTimeout(function(){
    that.setData({
      addressName:app.globalData.currentLocationName
    })
   },2000)
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
    this.getAddressList();
  },
  getAddressList() {
    let that = this;
    util.request(api.AddressList).then(function(res) {
      if (res.errno === 0) {
        that.setData({
          addressList: res.data.list,
          total: res.data.total
        });
      }
    });
  },
  updateAddress(event){
    let addressId=event.currentTarget.dataset.addressId;
    wx.navigateTo({
      url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
    })
  },
  async updateAddressData(address){
    await util.request(api.AddressSave,  address, 'POST').then(function(res) {
      if (res.errno === 0) {
        var city=res.data.city.indexOf("市辖")!=-1?res.data.province:res.data.city;
        var addresInfo={
          city:city,
          lng:res.data.lng,
          lat:res.data.lat,
          shopId:res.data.shopId,
          address:res.data.addressDetail
        }
        wx.setStorageSync('addressInfo', addresInfo); 
      }
    });
  },
  addressAddOrUpdate(event) {
   // console.log(event)

    let addressId=event.currentTarget.dataset.addressId;
    let addressDetail=event.currentTarget.dataset.addressDetail;
    let address=null;
   this.data.addressList.forEach(item=>{
     if(addressId==item.id)
        address=item;
   })
   console.info(address)

    //返回之前，先取出上一页对象，并设置addressId
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
   if(null!=address&&prevPage.route == "pages/index/index"){ 
    //  console.log("市辖区".indexOf("市"));
     var city=address.city.indexOf("市辖")!=-1?address.province:address.city;

     //通过jz_shop的shop_no关联服务力分配的门店id，拿实际的jz_shop的门店id
     //这个是拿的服务力分配的门店，如果不转要改后台查询商品，要用shopNo，这里的shopId是shopNo
    // let distribution_unit=address.shopId;
      if(address.shopId==null){
        this.updateAddressData(address);
      }else{
        var addresInfo={
          city:city,
          lng:address.lng,
          lat:address.lat,
          shopId:address.shopId,
          address:address.addressDetail
        }
        wx.setStorageSync('addressInfo', addresInfo); 
      }
      wx.navigateBack();
   
   }
   else  if (prevPage.route == "pages/checkout/checkout") {
      try {
        wx.setStorageSync('addressId', event.currentTarget.dataset.addressId);
      } catch (e) {

      }
 
      if (addressId && addressId != 0) {
        wx.navigateBack();
      } else {
        wx.navigateTo({
          url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
        })
      }

    }else if(prevPage.route == "pages/goods/infoCollect/infoCollect"){

      if (addressId && addressId != 0) {
        app.globalData.infoAddress={
          addressId:addressId,
          addressDetail:addressDetail
        }
        wx.navigateBack();
      } else {
        wx.navigateTo({
          url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
        })
      }

    } else {
      wx.navigateTo({
        url: '/pages/ucenter/addressAdd/addressAdd?id=' + event.currentTarget.dataset.addressId
      })
    }
  },
  deleteAddress(event) {
    console.log(event.target)
    let that = this;
    wx.showModal({
      title: '',
      content: '确定要删除地址？',
      success: function(res) {
        if (res.confirm) {
          let addressId = event.target.dataset.addressId;
          util.request(api.AddressDelete, {
            id: addressId
          }, 'POST').then(function(res) {
            if (res.errno === 0) {
              that.getAddressList();
              wx.removeStorage({
                key: 'addressId',
                success: function(res) {},
              })
            }
          });
          console.log('用户点击确定')
        }
      }
    })
    return false;

  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  }
})