text: var commonCityData = require('../../utils/city.js')
  //获取应用实例
  var app = getApp()
  Page({
    data: {
      provinces:[],
      citys:[],
      districts:[],
      selProvince:'请选择',
      selCity:'请选择',
      selDistrict:'请选择',
      selProvinceIndex:0,
      selCityIndex:0,
      selDistrictIndex:0
    },
    bindCancel:function () {
      wx.navigateBack({})
    },
    bindSave: function(e) {
      var that = this;
      var linkMan = e.detail.value.linkMan;
      var address = e.detail.value.address;
      var mobile = e.detail.value.mobile;
      var code = e.detail.value.code;
  
      if (linkMan == ""){
        wx.showModal({
          title: '提示',
          content: '请填写联系人姓名',
          showCancel:false
        })
        return
      }
      if (mobile == ""){
        wx.showModal({
          title: '提示',
          content: '请填写手机号码',
          showCancel:false
        })
        return
      }
      if (!(/^1[34578]\d{9}$/.test(mobile))) {
        wx.showModal({
          title: '提示',
          content: '请填写正确的手机号码',
          showCancel: false
        })
        return
      } 
      if (this.data.selProvince == "请选择"){
        wx.showModal({
          title: '提示',
          content: '请选择地区',
          showCancel:false
        })
        return
      }
      if (this.data.selCity == "请选择"){
        wx.showModal({
          title: '提示',
          content: '请选择地区',
          showCancel:false
        })
        return
      }
      var cityId = commonCityData.cityData[this.data.selProvinceIndex].cityList[this.data.selCityIndex].id;
      var districtId;
      if (this.data.selDistrict == "请选择" || !this.data.selDistrict){
        districtId = '';
      } else {
        districtId = commonCityData.cityData[this.data.selProvinceIndex].cityList[this.data.selCityIndex].districtList[this.data.selDistrictIndex].id;
      }
      if (address == ""){
        wx.showModal({
          title: '提示',
          content: '请填写详细地址',
          showCancel:false
        })
        return
      }
      if (code == ""){
        wx.showModal({
          title: '提示',
          content: '请填写邮编',
          showCancel:false
        })
        return
      }
  
      var apiAddoRuPDATE = "http://127.0.0.1:8090/wx/receivingAddress/addAddress";
      var apiAddid = that.data.id;
      if (apiAddid) {
        apiAddoRuPDATE = "http://127.0.0.1:8090/wx/receivingAddress/udpateById?id=" + apiAddid;
      } else {
        apiAddid = 0;
      }
      wx.request({
        url: apiAddoRuPDATE,
        data: {
          wechatId:111,
          provinceId: commonCityData.cityData[this.data.selProvinceIndex].id,
          cityId: cityId,
          areaId: districtId,
          consignee:linkMan,
          address: this.data.selProvince + this.data.selCity + this.data.selDistrict+address,
          phoneNo:mobile,
          code:code,
          isDefault: this.data.isDefault ? this.data.isDefault : 0
        },
        success: function(res) {
          // if (res.data.code != 0) {
          //   // 登录错误 
          //   wx.hideLoading();
          //   wx.showModal({
          //     title: '失败',
          //     content: res.data.msg,
          //     showCancel:false
          //   })
          //   return;
          // }
          
          // 跳转到结算页面
          wx.navigateBack({})
  
          wx.showModal({
            title: res.data.code+"",
            content: res.data.message,
            showCancel: false
          })
  
          
        }
      })
    },
    initCityData:function(level, obj){
      if(level == 1){
        var pinkArray = [];
        for(var i = 0;i<commonCityData.cityData.length;i++){
          pinkArray.push(commonCityData.cityData[i].name);
        }
        this.setData({
          provinces:pinkArray
        });
      } else if (level == 2){
        var pinkArray = [];
        var dataArray = obj.cityList
        for(var i = 0;i<dataArray.length;i++){
          pinkArray.push(dataArray[i].name);
        }
        this.setData({
          citys:pinkArray
        });
      } else if (level == 3){
        var pinkArray = [];
        var dataArray = obj.districtList
        for(var i = 0;i<dataArray.length;i++){
          pinkArray.push(dataArray[i].name);
        }
        this.setData({
          districts:pinkArray
        });
      }
      
    },
    bindPickerProvinceChange:function(event){
      var selIterm = commonCityData.cityData[event.detail.value];
      this.setData({
        selProvince:selIterm.name,
        selProvinceIndex: event.detail.value,
        selCity:'请选择',
        selCityIndex:0,
        selDistrict:'请选择',
        selDistrictIndex: 0
      })
      this.initCityData(2, selIterm)
    },
    bindPickerCityChange:function (event) {
      var selIterm = commonCityData.cityData[this.data.selProvinceIndex].cityList[event.detail.value];
      this.setData({
        selCity:selIterm.name,
        selCityIndex: event.detail.value,
        selDistrict: '请选择',
        selDistrictIndex: 0
      })
      this.initCityData(3, selIterm)
    },
    bindPickerChange:function (event) {
      var selIterm = commonCityData.cityData[this.data.selProvinceIndex].cityList[this.data.selCityIndex].districtList[event.detail.value];
      if (selIterm && selIterm.name && event.detail.value) {
        this.setData({
          selDistrict: selIterm.name,
          selDistrictIndex: event.detail.value
        })
      }
    },
    radioChange: function (event){
      this.data.isDefault = event.detail.value;
    },
    onLoad: function (e) {
      var that = this;
      this.initCityData(1);
      var id = e.id;
      if (id) {
        // 初始化原数据
        wx.showLoading();
        wx.request({
          url: 'http://127.0.0.1:8090/wx/receivingAddress/selectById',
          data: {
            id: id
          },
          success: function (res) {
            if (res.data.code == 200) {
              wx.hideLoading();
              that.setDBSaveAddressId(res.data.result);
              var obj = { "detail": { "value": that.data.selProvinceIndex } }
              that.bindPickerProvinceChange(obj);
              that.setDBSaveAddressId(res.data.result);
              obj.detail.value = that.data.selCityIndex;
              that.bindPickerCityChange(obj);
              that.setDBSaveAddressId(res.data.result);
              obj.detail.value = that.data.selDistrictIndex;
              that.bindPickerChange(obj);
              res.data.result.address=res.data.result.address.replace(that.data.selProvince + that.data.selCity + that.data.selDistrict, "");
              that.setData({
                id:id,
                addressData: res.data.result
                // selProvince: this.provinces[res.data.result.provinceId],
                // selCity: citys[res.data.result.cityId],
                // selDistrict: districts[res.data.result.areaId]
                });
              return;
            } else {
              wx.showModal({
                title: '提示',
                content: '无法获取快递地址数据',
                showCancel: false
              })
            }
          }
        })
      }
    },
    setDBSaveAddressId: function(data) {
      var retSelIdx = 0;
      for (var i = 0; i < commonCityData.cityData.length; i++) {
        if (data.provinceId == commonCityData.cityData[i].id) {
          this.setData({
            selProvinceIndex : i
          });
          for (var j = 0; j < commonCityData.cityData[i].cityList.length; j++) {
            if (data.cityId == commonCityData.cityData[i].cityList[j].id) {
              this.setData({
                selCityIndex: j
              });
              for (var k = 0; k < commonCityData.cityData[i].cityList[j].districtList.length; k++) {
                if (data.areaId == commonCityData.cityData[i].cityList[j].districtList[k].id) {
                  this.setData({
                    selDistrictIndex: k
                  });
                }
              }
            }
          }
        }
      }
     },
    selectCity: function () {
      
    },
    deleteAddress: function (e) {
      var that = this;
      var id = e.currentTarget.dataset.id;
      wx.showModal({
        title: '提示',
        content: '确定要删除该收货地址吗？',
        success: function (res) {
          if (res.confirm) {
            wx.request({
              url: 'http://127.0.0.1:8090/wx/receivingAddress/deleteById',
              data: {
                id: id
              },
              success: (res) => {
                wx.navigateBack({})
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
    readFromWx : function () {
      let that = this;
      wx.chooseAddress({
        success: function (res) {
          let provinceName = res.provinceName;
          let cityName = res.cityName;
          let diatrictName = res.countyName;
          let retSelIdx = 0;
          for (var i = 0; i < commonCityData.cityData.length; i++) {
            if (provinceName == commonCityData.cityData[i].name) {
              that.data.selProvinceIndex = i;
              for (var j = 0; j < commonCityData.cityData[i].cityList.length; j++) {
                if (cityName == commonCityData.cityData[i].cityList[j].id) {
                  that.data.selCityIndex = j;
                  for (var k = 0; k < commonCityData.cityData[i].cityList[j].districtList.length; k++) {
                    if (diatrictName == commonCityData.cityData[i].cityList[j].districtList[k].id) {
                      that.data.selDistrictIndex = k;
                    }
                  }
                }
              }
            }
          }
  
          that.setData({
            wxaddress: res,
            selProvince: provinceName,
            selCity: cityName,
            selDistrict: diatrictName
          });
        }
      })
    }
  })
  