// pages/address/address.js
const getReq = require('../../config.js').getReq
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address_list: [],
    firstLoad: true,
    fromPerson: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDatas()
    let len = getCurrentPages().length
    let prev = getCurrentPages()[len - 2] 
    if(prev['route'].indexOf('/person/person') != -1) {
      this.setData({
        fromPerson: true
      })
    }
  },
  getDatas() {
    wx.showLoading({
      title: '加载中',
    })
    var self = this
    getReq(app,{
      act: 'member_address',
      op: 'address_list'
    }, function (res) {
      wx.hideLoading()
      if (res.code == 200) {
        let address_list = res.datas.address_list
        if(!address_list.length) {
          app.clearAddress()
        }
        self.setData({
          address_list,
          firstLoad: false
        })

      }
      else {
        wx.showToast({
          icon: 'none',
          title: res.message,
          duration: 2000
        })
      }
    })
  },
  check_address(e) {
    if(this.data.fromPerson)return;
    let dataset = e.currentTarget.dataset
    let { address } = dataset
    let true_name = dataset.truename
    let mob_phone = dataset.mobphone
    let area_info = dataset.areainfo
    let address_id = dataset.addressid
    let area_id = dataset.areaid
    let city_id = dataset.cityid
    
    app.checkDefaultAddress({
      true_name,
      mob_phone,
      area_info,
      address,
      address_id,
      area_id,
      city_id
    })
    wx.navigateBack()
  },
  modify_address(e) {
    let dataset = e.currentTarget.dataset
    let { address } = dataset
    let true_name = dataset.truename
    let mob_phone = dataset.mobphone
    let area_info = dataset.areainfo
    let address_id = dataset.addressid
    let area_id = dataset.areaid
    let city_id = dataset.cityid
    let page = `/pages/addAddress/addAddress?true_name=${true_name}&mob_phone=${mob_phone}&area_info=${area_info}&address=${address}&area_id=${area_id}&address_id=${address_id}&city_id=${city_id}`
    app.navigateto(page)
  },
  del_address(e) {
    let self = this;
    let address_id = e.currentTarget.dataset.addressid
    wx.showModal({
      content: "确认删除此收货地址",
      confirmText: "确定",
      cancelText: "取消",
      success: function(res) {
        if (res.confirm) {
          getReq(app,{
            act: 'member_address',
            op: 'address_del',
            address_id
          }, function (res) {
            if (res.code == 200) {
              self.getDatas()
            }
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

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

  },

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

  },

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

  },

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

  }
})