var e = getApp(), t = require("../../../utils/request.js"), os = e.globalData.setting, oo = e.globalData, t = e.request;

Page({
  data: {
    url: e.globalData.setting.url,
    resourceUrl: e.globalData.setting.resourceUrl,
    iurl: e.globalData.setting.imghost,
    addresses: [],
    operate: null,
    curpage: 1,
    total: 0,
    ismore: 0,
    is_back:0,  //有没有跳转
    isloading:0,
    is_address_read:0
  },
  onLoad: function (e) {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      is_zy: 1
    })
    //清空is_pick_up
    getApp().request.put("/api/weshop/useraddress/updatePickUp", {
      data: {user_id: getApp().globalData.user_id, is_pickup: 0},
      success: function (s) {
      }
    });

    this.data.operate = e.operate;
  },
  onShow: function () {
    var is_back=getApp().globalData.is_cart_old;
    if (is_back) this.setData({ is_back: is_back})

    this.data.curpage = 1;
    this.data.addresses = [];
    this.requestAddressList();
    console.log(this.data.addresses)
  },

  onReachBottom: function () {
    if (this.data.total <= 6) return;
    if (this.data.ismore) return;
    this.requestAddressList();
  },

  requestAddressList: function () {
    var tt = Math.random() * 100
    var e = this;
    t.get(e.data.url + "/api/weshop/useraddress/page", {
      data: { store_id: os.stoid, user_id: oo.user_id, page: e.data.curpage, pageSize: 20, t: tt },
      success: function (t) {
        e.data.curpage++;
        var arr1 = e.data.addresses;
        var arr2 = t.data.data.pageData;
        var arr3 = [...arr1, ...arr2];
        var ismore = 0;
        if (arr3.length == t.data.data.total) ismore = 1
        e.setData({
          addresses: arr3, total: t.data.data.total, ismore: ismore,
          is_address_read:1
        }), wx.stopPullDownRefresh();
      }
    });
  },

  /*----------编辑地址----------*/
  editAddress: function (e) {
    var t = this.getAddressData(e.currentTarget.dataset.id), s = "";
    for (var a in t) s += (0 != s.length ? "&" : "?") + a + "=" + t[a];
    s && wx.navigateTo({
      url: "/packageF/pages/user/add_address/add_address" + s
    });
  },

  /*-----选择地址返回确认------*/
  selectAddress: function (e) {
    if (null != this.data.operate) {
      oo.baddr = e.currentTarget.dataset.item;
      wx.navigateBack();
    }
  },
  
  getAddressData: function (e) {
    var t = this.data.addresses;
    for (var s in t) if (t[s].address_id == e) break;
    return s ? t[s] : {};
  },

  go_url: function (e) {
    var url = e.currentTarget.dataset.url;
    getApp().goto(url);
    
  },
  //判断是否选择
  add_is_pickup:function(e){

     if(this.data.is_back==0) return false;

     var th = this;
     var index = e.currentTarget.dataset.ind;
     var a = this.data.addresses[index];

     if (th.data.isloading) return;
     th.data.isloading=1;

       t.put("/api/weshop/useraddress/updatePickUp", {
          data: { user_id: oo.user_id, is_pickup: 0 },
          isShowLoading:0,
          success: function (s) {

            a.is_pickup = 1;
            t.put("/api/weshop/useraddress/updateById", {
              data: a,
              isShowLoading: 0,
              success: function (s) {

                   th.data.isloading=0         
                   if (th.data.is_back) {
                      wx.navigateBack({});
                   }

              },fail: function (t) {
                th.data.isloading = 0;
              }
            });
          },
          fail: function (t) {
            th.data.isloading = 0;
           }
        });

      
    
  } ,
  
   //获取微信地址
    get_wx:function(){
      var th=this;
      wx.getSetting({
        success(res) {
          getApp().globalData.no_clear=1;
          console.log("vres.authSetting['scope.address']：", res.authSetting['scope.address']);
          if (res.authSetting['scope.address']) {
            getApp().globalData.no_clear=1;
            wx.chooseAddress({
              success(res) {
                th.save_wx_addr(res);
              }
            })
  
          } else {
            if (res.authSetting['scope.address'] == false) {
              getApp().globalData.no_clear=1;
              wx.openSetting({
                success(res) {}
              })
            } else {

              getApp().globalData.no_clear=1;
              wx.chooseAddress({
                success(res) {
                  th.save_wx_addr(res);
                }
              })
            }
          }
        }
      })
    },
  
    save_wx_addr:function (res) {
      var pd={},th=this;
      pd.store_id=os.stoid;
      pd.user_id=getApp().globalData.user_id;
      pd.userName=res.userName;
      pd.provinceName=res.provinceName;
      pd.countryName=res.countyName;
      pd.cityName=res.cityName;
      pd.detailInfo=res.detailInfo;
      pd.postalCode=res.postalCode;
      pd.telNumber=res.telNumber;
      getApp().request.promisePost("/api/weshop/useraddress/saveWx",{data:pd}).then(res=>{
        if(res.data.code==0){
          getApp().my_warnning("保存成功",1,th);
          setTimeout(function () {
            th.onShow();
          },1000)
        }
      })
  
    },



});