// customized/pages/address/add/add.js
let app = getApp();
let host = app.globalData.servsers; // 请求的url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    phone:'',
    region: ['上海市', '上海市', '黄浦区'],
    detailAddress:'',
    id:null,
    switchChecked:false, //是否默认
    type:'add',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,"options")
    if (options.type ==='edit'){ // 表示编辑
      this.setData({
        type:'edit'
      })
      this.getEditAddress(this)
    }
  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  switchChange(e){
    this.setData({
      switchChecked: e.detail.value
    })
  },
  nameChange(e){
    this.setData({
      name: e.detail.value
    })
  },
  phoneChange(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  addressInput(e){ // 详细地址
  console.log(e)
    this.setData({
      detailAddress: e.detail.value
    })
  },
  addAddress(){ // 保存地址
    let that =this;
    if(!that.data.name){
      wx.showToast({
        title: '收货人没有输入',
        icon:"none"
      });
      return
    }
    if(!that.data.phone){
      wx.showToast({
        title: '手机号码没有输入',
        icon: "none"
      });
      return
    }
    if (!that.data.detailAddress) {
      wx.showToast({
        title: '详细地址没有输入',
        icon: "none"
      });
      return
    }
    let data ={
      consignee: that.data.name, //收货人
      province: that.data.region[0],
      city: that.data.region[1],
      area: that.data.region[2],
      adress: that.data.detailAddress,
      phone: that.data.phone,
      adress_flag: that.data.switchChecked ? 0 : 1  // 是否默认 0是 1 否
    },
      url = host + '/adressapi/insertadd',
      title = "地址添加成功";
      if(that.data.type == 'edit'){
        data['action_account'] = null;
        data['id'] = that.data.id;
        url = host + '/adressapi/update';
        title = "地址编辑成功";
      } else {
        data['user_id'] =app.globalData.user_id;
        url = host + '/adressapi/insertadd';
        title = "地址添加成功";
      }
    wx.request({
      url: url,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: data,
      success(res){
        console.log(res,"地址保存")
        wx.showToast({
          title: title,
          icon:'none',
          success(){
            setTimeout(()=>{
              wx.navigateBack({
                delta: 1
              })
            },1000)
            
          }
        })
        
      }
    })
  },
  getEditAddress(that){ // 获取编辑的地址
    
    let address = wx.getStorageSync('address'), 
    name = address.consignee,
    phone =address.phone,
    region = [address.province,address.city,address.area],
    detailAddress =address.adress,
    switchChecked = address.adress_flag == '0'? true : false ,
    id = address.id;
    console.log(address)
    that.setData({
      name:name,
      phone:phone,
      region: region,
      detailAddress: detailAddress,
      switchChecked: switchChecked,
      id: id
    })
  }
})