const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    focus: false,
    inputValue: '',
    animationAddressMenu: {},
    addressMenuIsShow: false,
    region: ['山西省', '太原市', '迎泽区'],
    customItem: '',
    news: [],
    dataObj: {},
    checked:true
  },

  bindButtonTap: function() {
    this.setData({
      focus: true
    })
  },
  bindRegionChange: function(e) {
    this.setData({
      region: e.detail.value
    })
  },

  //新增地址

  formSubmit: function(e) {
    var _this = this;
    var province = '',
      city = '',
      district = '';
    var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    this.setData({
      dataObj: e.detail.value
    })
    var dataname = e.detail.value.name,
      dataphone = e.detail.value.phone,
      dataaddress = e.detail.value.address,
      dataarea = e.detail.value.area,
      datachecked = e.detail.value.checked;
    var is_default = "";
    if (datachecked) {
      is_default = "是"
    } else {
      is_default = "否"
    }
    if (dataname !== "") {
      if (reg.test(dataphone) && dataphone !== "") {
        if (dataarea !== "") {
          wx.getStorage({
            key: 'key',
            success: function (cookies) {
              wx.request({
                url: app.globalData.prodUrl + 'web-pm/wx/address/add',
                header: {
                  'ydw-token': cookies.data.ydw_token
                },
                data: {
                  user_name: dataname,
                  user_mobile: dataphone,
                  address: dataaddress,
                  detail: dataarea,
                  is_default: is_default,
                  province: dataaddress[0],
                  city: dataaddress[1],
                  district: dataaddress[2],
                },
                success: function (data) {
                  if (_this.data.commodityid) {
                    wx.navigateBack({
                      delta: 1,
                      success: function () {
                        var pages = getCurrentPages();
                        var page = getCurrentPages().pop();
                        if (page == undefined || page == null) return;
                        pages[2].onLoad(pages[2].options);
                        pages[3].onLoad(pages[3].options);
                      }
                    })
                  } else {
                    wx.navigateBack({
                      delta: 1,
                      success: function () {
                        var pages = getCurrentPages();
                        var page = getCurrentPages().pop();
                        if (page == undefined || page == null) return;
                        pages[1].onLoad(pages[1].options);
                      }
                    })
                  }
                }
              })
            }
          })
        } else {
          wx.showToast({
            title: '请填写地址',
            icon: 'none',
            duration: 2000
          })
        }
      } else {
        wx.showToast({
          title: '请正确填写手机号',
          icon: 'none',
          duration: 2000
        })
      }
    } else {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none',
        duration: 2000
      })
    } 
    // wx.navigateTo({
    //   url: '../receivingaddress/receivingaddress',
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var _this = this;
    if (options.commodityid){
      _this.setData({
        commodityid: options.commodityid
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  }
})