// subpages/goodsaddress/goodsaddress.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        token: '',
        list: [],
        hidePageLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var _self = this;
        _self.getToken().then(result => {
            _self.getList();
        })
    },
    getToken() {
        var self = this;
        return new Promise(resolve => {
            wx.getStorage({
                key: 'token',
                success(res) {
                    self.setData({
                        token: res.data
                    })
                    return resolve();
                }
            })
        })
    },
    //获取列表
    getList() {
        var _self = this
        var header = {
            headers: {
                token: _self.data.token
            }
        };

        app.restAjax.get(app.restAjax.path('{shopUrl}/app/shopaddress/list', [app.shopUrl]), null, header, function (code, data) {
            console.log(data);
            _self.setData({
                list: data,
                hidePageLoading: true
            })
        }, function (code, data) {
            app.dialog.msg(data.msg);
            _self.setData({
                hidePageLoading: true
            })
        }, function () {
            wx.hideLoading();
            _self.setData({
                hideLoading: true
            })
        })
    },
    doRefresh() {
        this.setData({
            list: []
        })
        this.getList();
    },
    doEdit(e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/subpages/editshopaddress/editshopaddress?id=' + id,
        })
    },
    doDel(e) {
        var id = e.currentTarget.dataset.id;
        var _self = this;
        wx.showModal({
            title: '警告',
            content: '确定要删除该收货地址吗?',
            complete: (res) => {
                if (res.confirm) {
                    app.dialog.loading("删除中...")
                    app.restAjax.delete(app.restAjax.path('{shopUrl}/app/shopaddress/remove/{ids}', [app.shopUrl, id]), null, {
                        headers: {
                            token: _self.data.token
                        }
                    }, function (code, data) {
                        wx.hideLoading()
                        if (code == 200) {
                            app.dialog.msg("删除成功")
                            _self.getList()
                        }
                    }, function (code, data) {
                        app.dialog.msg(data.msg)
                    });
                }
            }
        })
    },
    doSetDefault(e) {
        var _self = this;
        app.dialog.loading('设置中')
        var id = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index
        app.restAjax.put(app.restAjax.path('{shopUrl}/app/shopaddress/updatedefault/{shopAddressId}', [app.shopUrl, id]), null, {
            headers: {
                token: _self.data.token
            }
        }, function (code, data) {
            wx.hideLoading();
            if (code == 200) {
                app.dialog.msg('设置成功')
            }
            _self.setData({
                list: []
            })
            _self.getList();
        }, function (code, data) {
            console.log(data)
            wx.hideLoading();
            app.dialog.msg(data.msg)
        })

    },
    addAddress() {
        wx.navigateTo({
            url: '/subpages/addshopaddress/addshopaddress'
        })
    },
})