var app = getApp()
var that=null
var sopenFrame=null
var areas = null;
Page( {
  data: {
    loadingHidden:false,
    contact:"",
    tel:"",
    detail:"",
    selectHidden:true,
    sectionHidden:true,
    IFDefault:1,
    savebutton:"保存",
    weixinbutton:"一键获取微信收货地址",
    Provinces:[],
            Citys:[],
            Countys:[],
            tradingareas:[],
            hx_index:-1,
            hx_index0:-1,
            hx_index1:-1,
            hx_index2:-1,
            area:"",
            addressId:0,
            weixinaddresshidden:false
  },

  onLoad: function() {
      that = this
      areas = require('../../area.js'); 
      that.setData({
        Provinces:areas.areaslist
      })
      that.rdata()
    },rdata: function() {
           var userInfodata = wx.getStorageSync("userInfo")
           if(userInfodata){
              var userInfo = JSON.parse(userInfodata)
              var contact=""
              if(userInfo.RealName){
                  contact=userInfo.RealName
              }
              var tel=userInfo.Phone
              var Province=userInfo.Province
              var City=userInfo.City
              var County=userInfo.County
              if(Province==""&&City==""&&County==""){
                   var Locationprovince = wx.getStorageSync("Locationprovince")
                   var Locationcity = wx.getStorageSync("Locationcity")
                   var Locationcounty = wx.getStorageSync("Locationcounty")
                   if(Locationprovince&&Locationcity&&Locationcounty){
                        Province=Locationprovince
                        City=Locationcity
                        County=Locationcounty
                   }
              }
              var index=-1
              var index0=-1
              var index1=-1
              if(Province!=""&&City!=""&&County!=""){
              var provinces=that.data.Provinces
              for(var i=0;i<provinces.length;i++){
                    if(provinces[i].ProvinceID==Province){
                      index=i;
                      var citys=provinces[i].City;
                      for(var j=0;j<citys.length;j++){
                           if(citys[j].CityID==City){
                                index0=j;
                                var countys=citys[j].County;
                                for(var k=0;k<countys.length;k++){
                                     if(countys[k].CountyID==County){
                                          index1=k;
                                          break;
                                     } 
                                    
                                }
                                break;
                           }
                           
                      }
                      break;
                    }
              }
              }
               that.setData({
                hx_index: index,
                savebutton:"添加"
              })
              var citys=(index==-1?[]:that.data.Provinces[index].City)
              that.setData({
                Citys:citys,
                hx_index0: index0
              })
              var countys=(index0==-1?[]:that.data.Citys[index0].County)
              that.setData({
                Countys:countys,
                hx_index1: index1
              })
              var area=""
              if(that.data.hx_index!=-1){
                  area+=that.data.Provinces[that.data.hx_index].ProvinceName
              }
              if(that.data.hx_index0!=-1){
                  area+=that.data.Citys[that.data.hx_index0].CityName
              }
              if(that.data.hx_index1!=-1){
                   area+=that.data.Countys[that.data.hx_index1].CountyName
              }
              that.setData({
                contact:contact,
                tel:tel,
                area:area
                
               })
            }
            var editaddress = wx.getStorageSync("editaddress")
            if(editaddress){
              
              var myaddress=JSON.parse(editaddress);
              var id=myaddress.addressId
              var contact=myaddress.contact
              var tel=myaddress.tel
              var Province=myaddress.Province
              var City=myaddress.City
              var County=myaddress.County
              var detail=myaddress.detail
              var IFIndex=parseInt(myaddress.IFIndex)
              
              var index=-1
              var index0=-1
              var index1=-1
              var provinces=that.data.Provinces
              for(var i=0;i<provinces.length;i++){
                    if(provinces[i].ProvinceID==Province){
                      index=i;
                      var citys=provinces[i].City;
                      for(var j=0;j<citys.length;j++){
                           if(citys[j].CityID==City){
                                index0=j;
                                var countys=citys[j].County;
                                for(var k=0;k<countys.length;k++){
                                     if(countys[k].CountyID==County){
                                          index1=k;
                                          break;
                                     }
                                    
                                }
                                break;
                           }
                           
                      }
                      break;
                    }
              }
           
               that.setData({
                hx_index: index,
                weixinaddresshidden:true,
                savebutton:"保存"
              })
              var citys=(index==-1?[]:that.data.Provinces[index].City)
              that.setData({
                Citys:citys,
                hx_index0: index0
              })
              var countys=(index0==-1?[]:that.data.Citys[index0].County)
              that.setData({
                Countys:countys,
                hx_index1: index1
              })
              var area=""
              if(that.data.hx_index!=-1){
                  area+=that.data.Provinces[that.data.hx_index].ProvinceName
              }
              if(that.data.hx_index0!=-1){
                  area+=that.data.Citys[that.data.hx_index0].CityName
              }
              if(that.data.hx_index1!=-1){
                  area+=that.data.Countys[that.data.hx_index1].CountyName
              }
              that.setData({
                addressId:id,
                contact:contact,
                tel:tel,
                detail:detail,
                IFDefault:IFIndex,
                area:area,
                loadingHidden:true
               })
               wx.setNavigationBarTitle({
                title: '修改收货地址' 
              })
            }else{
               that.setData({
                    loadingHidden:true
               })
            }
    },getweixinaddress:function(e) {
      that.setData({
          weixinbutton:"请稍候..."
      })
      wx.getSetting({ 
        success(res) {
          if (!res.authSetting["scope.address"]) {
            wx.authorize({
              scope: 'scope.address',
              success () {
                that.addweixinaddress()
              } 
            })
          }else{
            /*wx.showToast({
              title: JSON.stringify(res.authSetting),
              icon: "none",
              duration: 20000
            })*/
              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:"一键获取微信收货地址"
                  })
                   wx.navigateBack({})
                }else{
                  that.setData({
                    weixinbutton:"一键获取微信收货地址"
                  })
                   wx.showToast({
                    title: res.data.msg,
                    icon: "none",
                    duration: 3000
                   })
                }
              }
            })
          }
        },fail (res) {
          wx.showToast({
            title: JSON.stringify(res),
            icon: "none",
            duration: 3000
           
          })
        }
      })
    },onarea:function(e) {
      that.setData({
          sectionHidden:false
      })
    },ProvinceChange:function(e) {
      var index=e.detail.value;
      var citys=that.data.Provinces[index].City
      that.setData({
        hx_index: index,
        hx_index0: -1,
        hx_index1: -1,
        Citys:citys
      })
      if(citys.length==0){
        that.setData({
           sectionHidden:true,
           area:that.data.Provinces[that.data.hx_index].ProvinceName
         })
      }
    },CityChange:function(e) {
      var index=e.detail.value
      var countys=that.data.Citys[index].County
      that.setData({
        hx_index0: index,
        hx_index1: -1,
        Countys:countys
      })
      if(countys.length==0){
        that.setData({
           sectionHidden:true,
           area:that.data.Provinces[that.data.hx_index].ProvinceName+that.data.Citys[that.data.hx_index0].CityName
         })
      }
    },CountyChange:function(e) {
      that.setData({
        hx_index1: e.detail.value
      })
      that.setData({
        sectionHidden:true,
        area:that.data.Provinces[that.data.hx_index].ProvinceName+that.data.Citys[that.data.hx_index0].CityName+that.data.Countys[that.data.hx_index1].CountyName
      })
    },closesection:function(e) {
      that.setData({
        sectionHidden:true
      })
    },insection:function(e) {
      
    },changeisDefault:function(e) {
      var ifdefault = (e.detail.value==true?1:0)
      that.data.IFDefault=ifdefault
     
    },saveaddress:function(e) {
      var addressId=that.data.addressId
      
      var contact = e.detail.value.contact
      var tel=e.detail.value.tel
      var Province=(that.data.hx_index==-1?"":that.data.Provinces[that.data.hx_index].ProvinceID)
      var City=(that.data.hx_index0==-1?"":that.data.Citys[that.data.hx_index0].CityID)
      var County=(that.data.hx_index1==-1?"":that.data.Countys[that.data.hx_index1].CountyID)
      var detail=e.detail.value.detail
      var IFIndex=that.data.IFDefault
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var b=true
     
      if(contact==null || contact==""){
          b=false
          wx.showToast({
            title: "收货人不能为空！",
            icon: "none",
            duration: 3000
           })
      }
      if(b){
      if(tel==null || tel==""){
        b=false
        wx.showToast({
          title: "手机号码不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(that.data.hx_index==-1){
        b=false
        wx.showToast({
          title: "省份必须选择！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(that.data.hx_index0==-1){
        b=false
        wx.showToast({
          title: "城市必须选择！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(detail==null || detail==""){
        b=false
        wx.showToast({
          title: "详细地址不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
      if(b){
        that.setData({
            savebutton:"请稍候..."
        })
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:(addressId==0?"addAddress":"updateAddress"),
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId),
           addressId:(addressId==0?"":addressId),
           contact:contact,
           tel:tel,
           Province:Province,
           City:City,
           County:County,
           detail:detail,
           IFIndex:IFIndex
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res) { 
          if(res.data.status==1){
           app.globalData.IFRefreshmyaddress=1
           wx.showModal({
                            title: "操作提示",
                            content: res.data.msg,
                            showCancel:false,
                            success (res0) {
                              if (res0.confirm) {
            
             that.setData({
                 savebutton:"保存"
             }) 
              wx.navigateBack({})
            }
          }
        })
          }else{
            that.setData({
              savebutton:"保存"
             }) 
             wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })
          }
          }
      })
      }
    }
})
