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

  /**
   * 页面的初始数据
   */
  data: {
    addressList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

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

  },

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

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  getaddress() {
    wx.showLoading({
      title: '加载中',
    })
    let that = this;
    wx.request({
      url: host + '/adressapi/adressview',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        user_id: app.globalData.user_id,// 'oovpNwjlsY6xx8ceCebFa1dOLd9E'  //app.globalData.user_id,
      },
      success(res) {
        console.log(res, "address")
        that.setData({
          addressList:[...res.data.rows]
        })
      },
      complete(){
        wx.hideLoading()
      }
    })
  },
  addAddress(){
    wx.navigateTo({
      url: '/customized/pages/address/add/add',
    })
  },
  delAddress(e){
    let that = this, id = e.target.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否确定删除地址?',
      success(res){
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: host + '/adressapi/delete',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method:'get',
            data:{
              id: id 
            },
            success(res){
              console.log(res,"删除");
              wx.showToast({
                title: '删除成功!',
                icon:'none'
              })
              that.getaddress()
            }

          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    console.log(e.target.dataset.id)
  },
  editAddress(e){
    wx.setStorageSync('address', e.target.dataset.item)
    wx.navigateTo({
      url: '/customized/pages/address/add/add?type=edit',
    })
  },
  selectAddress(e){ // 选中地址
    console.log(e)
    wx.setStorageSync('accountAddress', e.currentTarget.dataset.item)
    wx.navigateBack({
      delta: 1
    })
    // wx.navigateTo({
    //   url: '/customized/pages/accounts/accounts?type=selectAddress',
    // })
  }
})