// var util = require('../../../../../utils/util.js');
// var api = require('../../../../../utils/api.js');
// const pay = require('../../../../../services/pay.js');
// const app = getApp()
var Parse = getApp().Parse;

// 触底上拉刷新 TODO 这里要将page传给服务器，作者没写
Page({
    data: {
        addresses: [],
        nowAddress: 0,
        activeColor: getApp().globalData.activeColor,
        titleColor: getApp().globalData.titleColor
    },
    goAddressDetail: function(e) {
        let id = e.currentTarget.dataset.addressid;
        wx.navigateTo({
            url: '/common-page/pages/info/ucenter/address-detail/index?id=' + id,
        })
    },

    async getAddresses() {
        let that = this;
        let addList = []
        let user = Parse.User.current();
        let query = new Parse.Query("ShopAddress");
        query.equalTo("user", user.id);
        let address = await query.find()
        if(address)
        {
            address.map(item=>{
                addList.push({
                    id:item.id,
                    name:item.get("name"),
                    province_name:item.get("province_name"),
                    city_name:item.get("city_name"),
                    district_name:item.get("district_name"),
                    full_region:item.get("full_region"),
                    address:item.get("address")
                })
            })
        }
        that.setData({
               addresses:  addList
          })
        
        // util.request(api.GetAddresses).then(function(res) {
        //     if (res.errno === 0) {
        //         that.setData({
        //             addresses: res.data
        //         })
        //     }
        // });
    },

    selectAddress:function(e) {
        let addressId = e.currentTarget.dataset.addressid
        let address = e.currentTarget.dataset.address
        wx.setStorageSync('addressId', addressId);
        let pages = getCurrentPages()
        // 获取上一界面的页面内容
        let perPage = pages[pages.length-2]
        perPage.setData({
            address: address
        })

        wx.navigateBack();
    },

    onLoad: function(options) {
        let type = options.type;
        this.setData({
            type: type
        })
    },
    
    onUnload: function() {},
    onShow: function() {
        this.getAddresses();
        let addressId = wx.getStorageSync('addressId');
        if (addressId) {
            this.setData({
                nowAddress: wx.getStorageSync('addressId')
            });
        }
        else {
            this.setData({
                nowAddress: 0
            });
        }
    },
    // 新增地址
    addAddress: function() {
        wx.navigateTo({
            url: '/nova-zhiliang/pages/my/ucenter/address-detail/index',
        })
    },
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading()
        this.getAddresses();
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    }
})