// pages/mine/product/recycle/productrecycle.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsList: [], //回收站商品
        isRefreshing: false,
        baseImageUrl: app.urls.baseImgUrl
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getList()
    },
    getList() {
        wx.showLoading({
            title: '加载中...',
        })
        var _self = this
        app.http.get(app.urls.getRecycleGoodsList, {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                _self.setData({
                    goodsList: res.data,
                    isRefreshing: false
                })
            })
            .catch(err => {
                wx.hideLoading({})
                _self.setData({
                    isRefreshing: false
                })
            })
    },
    //删除一个
    doDelOne(e) {
        var _self = this
        var item = e.currentTarget.dataset.item
        wx.showModal({
            title: '警告',
            content: '删除商品将无法找回,请确认要删除吗?',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '删除中...',
                    })
                    app.http.delete(app.urls.doDelRecycleGoods.format({
                            ids: item.goodsId
                        }), {
                            header: {
                                token: app.globalData.token
                            }
                        })
                        .then(res => {
                            wx.hideLoading({})
                            wx.showToast({
                                title: '删除成功',
                                icon: 'none',
                                success() {
                                    _self.dorefreshList()
                                }
                            })
                        })
                        .catch(err => {
                            wx.hideLoading({})
                        })
                }
            }
        })
    },
    //清除全部
    doClearAll() {
        var _self = this
        if (_self.data.goodsList.length > 0) {
            wx.showModal({
                title: '警告',
                content: '删除商品将无法找回,确认要清空回收站吗?',
                success(res) {
                    if (res.confirm) {
                        var ids = ''
                        _self.data.goodsList.forEach(it => {
                            ids += it.goodsId + ','
                        })
                        wx.showLoading({
                            title: '删除中...',
                        })
                        ids = ids.slice(0, ids.length - 1)
                        app.http.delete(app.urls.doDelRecycleGoods.format({
                                ids: ids
                            }), {
                                header: {
                                    token: app.globalData.token
                                }
                            })
                            .then(res => {
                                wx.hideLoading({})
                                wx.showToast({
                                    title: '删除成功',
                                    success() {
                                        _self.dorefreshList()
                                    }
                                })
                            })
                            .catch(err => {
                                wx.hideLoading({})
                            })
                    }
                }
            })
        } else {
            wx.showToast({
                title: '回收站空空如也',
                icon: 'none'
            })
        }
    },
    //恢复一个
    doRecoverOne(e) {
        var item = e.currentTarget.dataset.item
        var _self = this
        wx.showLoading({
            title: '恢复中...',
        })
        app.http.put(app.urls.doRecoverGoods.format({
                ids: item.goodsId
            }), {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                wx.showToast({
                    title: '恢复成功',
                    success() {
                        _self.dorefreshList()
                    }
                })
                var pages = getCurrentPages(); //当前页面
                var beforePage = pages[pages.length - 2]; //前一页
                beforePage.setData({
                    isAddPage: true
                })
            })
            .catch(err => {
                wx.hideLoading({})
            })
    },
    //刷新列表
    dorefreshList() {
        this.setData({
            goodsList: [], //三级类目
            isRefreshing: true,
        })
        this.getList()
    },
})