// pages/mine/wxml/edit/edit.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    focus: false,
    inputValue: '',
    animationAddressMenu: {},
    addressMenuIsShow: false,
    region: ['山西省', '太原市', '迎泽区'],
    customItem: '',
    dataObj: {},
    array: [], 
  },
  bindRegionChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  //上传
  formSubmit: function (e) {
    // //console.log(222)
    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}$/;
    // //console.log('form发生了submit事件，携带数据为：', 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 = "";
    //console.log()
    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/update',
                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],
                  kid: _this.data.array.kid
                },
                success: function (data) {
                  //console.log(data)
                  wx.navigateBack({
                    delta: 1,
                    success: function () {
                      var pages = getCurrentPages();
                      var page = getCurrentPages().pop();
                      //console.log(pages)
                      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) {
    //console.log(options.kid)
    var _this = this;
    wx.request({
      url: app.globalData.prodUrl + 'web-pm/wx/address/get',
      header: {
        'ydw-token': wx.getStorageSync('key').ydw_token
      },
      data: {
        kid: options.kid
      },
      success: function (data) {
        //console.log(data.data)
        _this.setData({
          array: data.data.t_receive_address,
        })
        var region = new Array;
        region[0] = data.data.t_receive_address.province;
        region[1] = data.data.t_receive_address.city;
        region[2] = data.data.t_receive_address.district;
        //console.log(region)
        _this.setData({
          region: region
        })
        var is_default = data.data.t_receive_address.is_default;
        if (is_default == "是"){
          //console.log(111)
          _this.setData({
              checked : true
          })
        }else{
          _this.setData({
            checked: false
          })
        }
      }
    })
  },

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

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

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

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

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

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