var app = getApp();

Page({
  data: {
    addressListData: [],
    state: 0,
  },
  // 获取收货地址列表
  _getData: function () {
    var that = this;
    app._getNetWork({
      showToastIn: false,
      url: app.getNetAddresss("member.member-address.index"),
      success: function (res) {
        var _data = res.data;
        if (_data.result == 1) {
          var _state = 0;
          if (_data.data.length <= 0) {
            _state = 1;
          } else {
            _state = 2;
          }
          that.setData({
            addressListData: _data.data,
            state: _state,
          });
          console.log("收货地址列表", _data.data);
        } else {
          wx.showToast({
            title: "数据错误",
            icon: "success",
            duration: 2000,
            success: function () {},
            fail: function () {},
          });
        }
      },
      fail: function (res) {},
    });
  },
  // 设置默认收货地址
  defaultAddress: function (e) {
    var that = this;
    var url = app.getNetAddresss("member.member-address.setDefault");
    url += "&address_id=" + e.currentTarget.dataset.id;
    app._getNetWork({
      url: url,
      success: function (res) {
        var _data = res.data;
        console.log("设置默认收货地址", res);
        if (_data.result == 1) {
          that._getData();
        } else {
          wx.showToast({
            title: _data.msg,
            icon: "none",
            duration: 2000,
            success: function () {},
            fail: function () {},
          });
        }
      },
      fail: function (res) {},
    });
  },
  // 修改收货地址
  editAddress: function (e) {
    var _item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url:
        "/packageD/member/addressEdit/addressEdit?model=" +
        JSON.stringify(_item),
    });
  },
  // 删除收货地址
  delAddress: function (e) {
    var that = this;
    var url = app.getNetAddresss("member.member-address.destroy");
    url += "&address_id=" + e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "确定要删除么？此操作不可逆！",
      success: function (res) {
        if (res.confirm) {
          console.log("用户点击确定");
          app._getNetWork({
            url: url,
            success: function (res) {
              var _data = res.data;
              console.log("收货地址列表", _data);
              if (_data.result == 1) {
                that._getData();
                wx.showToast({
                  title: _data.msg,
                  icon: "success",
                  duration: 2000,
                  success: function () {},
                  fail: function () {},
                });
              } else {
                wx.showToast({
                  title: "数据错误",
                  icon: "success",
                  duration: 2000,
                  success: function () {},
                  fail: function () {},
                });
              }
            },
            fail: function (res) {},
          });
        }
      },
    });
  },

  _gotoAddressAdd: function (options) {
    wx.navigateTo({
      url: "/packageD/member/addressAdd_v2/addressAdd_v2",
    });
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this._getData();
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
});
