// pages/more/new_delivery_address/new_delivery_address.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnDisabled: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var name, phone, region = ['广东省', '广州市', '海珠区'], address_detail, id;
    if (options.ptype == 'add'){
    } else if (options.ptype == 'edit'){
      name = options.name;
      phone = options.phone;
      region = options.region.split(",");
      address_detail = options.address_detail;
      id = options.id;
    }
    this.setData({
      ptype: options.ptype,
      name : name,
      phone : phone,
      region : region,
      address_detail : address_detail,
      id : id
    })
  },
  // 地区
  bindRegionChange: function(e){
    this.setData({
      region: e.detail.value
    })
  },
  // 获得输入的内容
  getKey: function(e){
    var kind = e.currentTarget.dataset.kind;
    var value = e.detail.value;
    this.setData({
      [kind] : value
    })
  },
  //保存地址
  saveAddress: function(){
    var that = this;
    var btnDisabled = that.data.btnDisabled;
    if ( btnDisabled ){
      // wx.showModal({
      //   title: '温馨提示',
      //   content: '请不要重复提交',
      //   showCancel: false,
      // })
      return false;
    }
    var ptype = that.data.ptype;
    var name = that.data.name;
    var phone = that.data.phone;
    var region = that.data.region;
    var address_detail = that.data.address_detail;
    var id = that.data.id;
    var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var phoneValid = reg.test(phone);
    var errorContent;
    if (name == undefined || name == ''){
      errorContent = '请输入收货人姓名';
    }else if( phone == undefined || phone == ''){
      errorContent = '请输入收货人联系电话';
    } else if (!phoneValid) {
      errorContent = '请输入正确的联系电话';
    }else if (phone == undefined || phone == '') {
      errorContent = '请输入收货详细地址';
    }
    if (errorContent != undefined ){
      wx.showModal({
        title: '温馨提示',
        content: errorContent,
        showCancel: false,
      })
      return false;
    }
    that.setData({
      showBtnLoading: true,
      btnDisabled: true,
    })
    setTimeout(function(){
      wx.showToast({
        title: '保存成功',
        duration: 1500,
        success: function(){
          that.setData({
            showBtnLoading: false,
            btnDisabled: false,
          })
          setTimeout(function(){
            wx.navigateBack({})
          },1500)
        }
      })
    },2000)
    if (ptype == 'add'){

    } else if (ptype == 'edit'){

    }

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

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },


})