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

    /**
     * 页面的初始数据
     */
    data: {
        catalogList: [],
        currentPage: 1,
        isRefreshing: false,
        activeNames: ['0'],
        catalogOneList: [], //一级类目
        catalogTwoList: [], //二级类目
        catalogThirdList: [], //三级类目
        selectItems: [], //选中的行业,
        selectItemNames: '',
        isAddPage: false //用来判断是否需要刷新页面
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCatalogList()
    },
    //获取店铺商品类目
    getCatalogList() {
        let _self = this
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get(app.urls.getRecycleCategoryList, {
                header: {
                    token: app.globalData.token
                },
            })
            .then(res => {
                wx.hideLoading({})
                _self.setData({
                    isRefreshing: false
                })
                res.data.forEach(el => {
                    el.isSel = false
                })
                _self.setData({
                    catalogThirdList: res.data
                })
            })
            .catch(err => {
                wx.hideLoading({})
                _self.setData({
                    isRefreshing: false
                })
            })
    },
    //清除全部
    toClearAll() {
        if (this.data.catalogThirdList.length > 0) {
            let _self = this
            wx.showModal({
                title: '警告',
                content: '删除后会删除该类目下所有商品,确定要删除该类目吗?',
                success(res) {
                    if (res.confirm) {
                        var ids = ''
                        _self.data.catalogThirdList.forEach(it => {
                            ids += it.categoryId + ','
                        })
                        ids = ids.slice(0, ids.length - 1)
                        wx.showLoading({
                            title: '删除中...',
                        })
                        app.http.delete(app.urls.doDelCatalogRecycle.format({
                                ids: ids
                            }), {
                                header: {
                                    token: app.globalData.token,
                                }
                            })
                            .then(res => {
                                wx.hideLoading({})
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'success',
                                    success() {
                                        _self.dorefreshList()
                                    }
                                })

                            })
                            .catch(err => {
                                wx.hideLoading({})
                            })
                    }
                }
            })
        } else {
            wx.showToast({
                title: '回收站空空如也',
            })
        }
    },
    //删除选中的
    toDelSel() {
        var _self = this
        var ids = ''
        _self.data.catalogThirdList.forEach(it => {
            if (it.isSel) {
                ids += it.categoryId + ','
            }
        })
        if (ids.length > 0) {
            ids = ids.slice(0, ids.length - 1)
            wx.showModal({
                title: '警告',
                content: '删除后会删除该类目下所有商品,确定要删除该类目吗?',
                success(res) {
                    if (res.confirm) {
                        wx.showLoading({
                            title: '删除中...',
                        })
                        app.http.delete(app.urls.doDelCatalogRecycle.format({
                                ids: ids
                            }), {
                                header: {
                                    token: app.globalData.token,
                                }
                            })
                            .then(res => {
                                wx.hideLoading({})
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'success',
                                    success() {
                                        _self.dorefreshList()
                                    }
                                })
                            })
                            .catch(err => {
                                wx.hideLoading({})
                            })
                    }
                }
            })
        } else {
            wx.showToast({
                title: '请选择要删除的类目',
            })
        }
    },
    //删除单条
    toDel(e) {
        var item = e.currentTarget.dataset.item
        var index = e.currentTarget.dataset.index
        let _self = this
        wx.showModal({
            title: '警告',
            content: '删除后会删除该类目下所有商品,确定要删除该类目吗?',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '删除中...',
                    })
                    app.http.delete(app.urls.doDelCatalogRecycle.format({
                            ids: item.categoryId
                        }), {
                            header: {
                                token: app.globalData.token,
                            }
                        })
                        .then(res => {
                            wx.hideLoading({})
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success',
                                success() {
                                    _self.dorefreshList()
                                }
                            })

                        })
                        .catch(err => {
                            wx.hideLoading({})
                        })
                }
            }
        })
    },
    //恢复单个
    toRecoverOne(e) {
        var item = e.currentTarget.dataset.item
        var _self = this
        wx.showLoading({
            title: '恢复中...',
        })
        app.http.put(app.urls.doRecoverCatelog.format({
                ids: item.categoryId
            }), {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                wx.showToast({
                    title: '恢复成功',
                    success() {
                        _self.dorefreshList()
                    }
                })
            })
            .catch(err => {
                wx.hideLoading({})
            })
    },
    //恢复全部
    toRecoverAll() {
        var _self = this
        if (_self.data.catalogThirdList.length > 0) {
            var ids = ''
            var _self = this
            _self.data.catalogThirdList.forEach(it => {
                ids += it.categoryId + ","
            })
            ids = ids.slice(0, ids.length - 1)
            wx.showLoading({
                title: '恢复中...',
            })
            app.http.put(app.urls.doRecoverCatelog.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: '回收站空空如也',
            })
        }
    },
    //刷新列表
    dorefreshList() {
        this.setData({
            catalogThirdList: [], //三级类目
            isRefreshing: true,
            currentPage: 1
        })
        this.getCatalogList()
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let _self = this
        if (_self.data.isAddPage) {
            _self.dorefreshList()
        }
    },
    doEdit(e) {
        var item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: './catalogedit?catalogId=' + item.id,
        })
    },
    // 选中了条目
    selectItem(e) {
        let _self = this
        let {
            type,
            item,
            index
        } = e.currentTarget.dataset
        _self.data.catalogThirdList.forEach(el => {
            if (el.categoryId == item.categoryId) {
                el.isSel = !item.isSel
            }
        })
        _self.setData({
            catalogThirdList: _self.data.catalogThirdList
        })
    },
})