var app = getApp()
var that=null
var sopenFrame=null
Page( {
  data: {
    loadingHidden:false,
    weixinbutton:"一键获取微信收货地址",
    myaddress:[]
  },

  onLoad: function() {
      that = this
      that.rdata(0)
      sopenFrame=setInterval(function (){
        if(app.globalData.IFRefreshmyaddress==1){
            that.rdata()
            app.globalData.IFRefreshmyaddress=0
        }
    }, 1000);
    }, // 下拉刷新
    onPullDownRefresh: function () {
       //在当前页面显示导航条加载动画
     wx.showNavigationBarLoading(); 
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
         title: '刷新中...',
     })
        that.clearCache();
        that.rdata(1);//第一次加载数据
    },rdata: function(t) {
     
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"getAddress",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId)
        },
        method:"GET",
        dataType:"json", 
        header: { 
          "content-type": "application/json" // 默认值
        },
        success (res) { 
            if(res.data.status==1){
            var qygm = wx.getStorageSync("qygm")
            if(qygm){
                that.setData({
                    selectHidden:false
                })
               
            }else{
                that.setData({
                    selectHidden:true
                })
            }
            that.setData({
                 myaddress:res.data.data
             })
             if(res.data.data.length==0){
                  wx.removeStorageSync('editaddress')
                  wx.navigateTo({  
                    url: "../myaddress_add/index"
                  })
             }
             that.setData({
                  loadingHidden:true
             })
          }
          if(t==0){
            //隐藏loading 提示框
            wx.hideLoading();
            //隐藏导航条加载动画
            wx.hideNavigationBarLoading();
            //停止下拉刷新
            wx.stopPullDownRefresh();
        }
          }
      })
    },select:function(e) {
      var index = e.currentTarget.dataset.id
      var id=that.data.myaddress[index].addressId
      var contact=that.data.myaddress[index].contact
      var tel=that.data.myaddress[index].tel
      var area=that.data.myaddress[index].area
      var detail=that.data.myaddress[index].detail
      var Province=that.data.myaddress[index].Province
      var City=that.data.myaddress[index].City
      var County=that.data.myaddress[index].County
      var qygm = wx.getStorageSync("qygm")
      if(qygm){
          app.globalData.selladdress={id:id,contact:contact,tel:tel,address:area+detail,Province:Province,City:City,County:County}
          app.globalData.IFRefreshsell=1
          wx.removeStorageSync('qygm')
          wx.navigateBack({})
      }
    },isDefault:function(e) {
      var index = e.currentTarget.dataset.id
      var id=that.data.myaddress[index].addressId
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"setDefaultAddress",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId),
           addressId:id
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res) { 
            if(res.data.status==1){
              wx.showToast({
                title: res.data.msg,
                icon: "success",
                duration: 2000
               })
               that.rdata()
            }else{
               wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 2000
               })
            }
          }
      })
    
    },isDefaulted:function(e) {
      var index = e.currentTarget.dataset.id
      var id=that.data.myaddress[index].addressId
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"setDefaultAddress",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId),
           t:1,
           addressId:id
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res) { 
            if(res.data.status==1){
              wx.showToast({
                title: res.data.msg,
                icon: "success",
                duration: 2000
               })
               that.rdata()
            }else{
               wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 2000
               })
            }
          }
      })
    },getweixinaddress:function(e) {
      that.setData({
        weixinbutton:"请稍候..."
    })
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.address']) {
            wx.authorize({
              scope: 'scope.address',
              success () {
                that.addweixinaddress()
              }
            })
          }else{
               that.addweixinaddress()
          }
        }
      })
      
    },addweixinaddress:function() {
      wx.chooseAddress({
        success (res) {
          var userName=res.userName
          var postalCode=res.postalCode
          var provinceName=res.provinceName
          var cityName=res.cityName
          var countyName=res.countyName
          var detailInfo=res.detailInfo
          var nationalCode=res.nationalCode
          var telNumber=res.telNumber
          var userId = wx.getStorageSync("userId")
          var LoginId = wx.getStorageSync("LoginId")
          if(userName!=""&&provinceName!=""&&cityName!=""&&telNumber!=""){
            wx.request({
              url: app.globalData.apiurl + "/xapiajax.ashx", 
              data: { 
                 action:"addweixinaddress",
                 userId:(!userId?"":userId),
                 LoginId:(!LoginId?"":LoginId),
                 contact:userName,
                 tel:telNumber,
                 provinceName:provinceName,
                 cityName:cityName,
                 countyName:countyName,
                 detail:detailInfo,
                 IFIndex:1
              },
              method:"POST",
              dataType:"json", 
              header: { 
                "content-type": "application/x-www-form-urlencoded" // 默认值
              },
              success (res) { 
                if(res.data.status==1){
                  wx.showToast({
                    title: res.data.msg,
                    icon: "success",
                    duration: 3000
                   })
                   app.globalData.IFRefreshmyaddress=1
                   that.setData({
                    weixinbutton:"一键获取微信收货地址"
                  })
                }else{
                   wx.showToast({
                    title: res.data.msg,
                    icon: "none",
                    duration: 3000
                   })
                   that.setData({
                    weixinbutton:"一键获取微信收货地址"
                  })
                }
              }
            })
          }
        },fail (res) {
          wx.showToast({
            title: JSON.stringify(res),
            icon: "none",
            duration: 3000
           })
        }
      })
    },showaddaddress:function(e) {
      wx.removeStorageSync('editaddress')
      wx.navigateTo({  
        url: "../myaddress_add/index"
       })
    },showedit:function(e) {
      var index = e.currentTarget.dataset.id
      wx.setStorageSync('editaddress', JSON.stringify(that.data.myaddress[index]))
      wx.navigateTo({  
        url: "../myaddress_add/index"
       })
    },delete:function(e) {
      wx.showModal({
        title: '删除提示',
        content: '此操作不可恢复！您确认要删除吗？',
        success (res0) {
          if (res0.confirm) {
      var index = e.currentTarget.dataset.id
      var addressId=that.data.myaddress[index].addressId
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"deleteAddress",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId),
           addressId:addressId
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res) { 
          if(res.data.status==1){
            wx.showToast({
              title: res.data.msg,
              icon: "success",
              duration: 3000
             })
             that.rdata()
             that.setData({
              weixinbutton:"一键获取微信收货地址"
            })
          }else{
             wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })
             that.setData({
              weixinbutton:"一键获取微信收货地址"
            })
          }
          }
      })
    }
  }
})
    }
})
