// packageA/member/supplier/component/classify/classify.js
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        debtId:{
          type:null
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        // 所选一级
        firstClass: 0,
        // 所选二级
        twoClass: 0,
        // 一级分类
        categoryInfo: [{
            name: ''
        }],
        onePage: 1,
        current_page_first: 1,
        last_page_first: 1,
        // 二级分类
        // 二级分类
        categoryInfo_two: [{
            name: ''
        }],
        twoPage: 1,
        current_page_two: 1,
        last_page_two: 1,

        keyInto: '',
        cat_level: '2'
        // 防止多次点击

    },
    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached() {
            this.getClass();
        },
        moved() { },
        detached() { },
    },
    observers: {
        // 'sid': function (ea) {
        //     console.log(ea, 'sssssssss')
        // }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        init() {
            this.setData({
                // categoryInfo: [],
                onePage: 1,
                current_page_first: 1,
                last_page_first: 1,
                // categoryInfo_two: [],
                twoPage: 1,
                current_page_two: 1,
                last_page_two: 1,
                twoClass: 0,
                keyInto: ''
            });
        },
        toSearch(e) {
            let id = e.currentTarget.dataset.id;
            console.log('debtId::::::::::',this.data.debtId)
            wx.navigateTo({
                url: '/packageA/member/supplier/SupplierSearch/SupplierSearch?debtCId=' + id+'&debtId='+this.data.debtId,
            });
        },
        tapRightTwo(e) {
            this.setData({
                twoClass: e.currentTarget.dataset.id,
                keyInto: `thr_id${e.currentTarget.dataset.kid}`
            });
        },
        tapOneClass(e) {
            let ind = e.currentTarget.dataset.ind;
            console.log(ind,'aaaaaaaaaa')
            this.setData({
                firstClass: ind,
            });
            this.init();
            this.getClass();
        },
        getClass(loadMore = false) {
            wx.showLoading({
                title: '加载中',
            });
            let urlStr = app.getNetAddresss("plugin.debt-shop.frontend.goods.goodsTypeList");
            app._getNetWork({
                url: urlStr,
                data: {
                  paginate: 100,
                  page: this.data.onePage,
                  level:'1'
                },
                success: (resdata) => {
                    var res = resdata.data;
                    if (res.result == 1) {
                        this.setData({
                            cat_level: res.data.max_level
                        });
                        wx.hideLoading({})
                        if (!loadMore) {
                            // 一级分类初始请求
                            this.setData({
                                categoryInfo: res.data.list.data,
                                current_page_first: res.data.list.current_page,
                                last_page_first: res.data.list.last_page
                            });
                            console.log(this.data.categoryInfo,this.data.firstClass)
                            this.getClassTwo(this.data.categoryInfo[this.data.firstClass].id);
                        } else {
                            // 一级分类下滑触底加载更多
                            console.log(this.data.categoryInfo,res.data.list.data)
                            this.setData({
                                categoryInfo: this.data.categoryInfo.concat(res.data.list.data),
                                current_page_first: res.data.list.current_page,
                                last_page_first: res.data.list.last_page
                            });
                            console.log(this.data.categoryInfo)
                        }
                    } else {
                        wx.showToast({
                            title: res.msg,
                        });
                        wx.hideLoading({
                            success: (res) => { },
                        });
                    }
                }
            });
        },
        getClassTwo(child, loadMore = false, pageTwo) {
            // 取二级分类
            let urlStr = app.getNetAddresss("plugin.debt-shop.frontend.goods.goodsTypeList");
            app._getNetWork({
                url: urlStr,
                data: {
                    paginate: 100,
                    parent_id: child,
                    page: this.data.twoPage,
                    get_child: 'get_child',
                },
                success: (resdata) => {
                    wx.hideLoading({})
                    var res = resdata.data;
                    if (res.result == 1) {
                        if (loadMore) {
                            // 加载更多
                            // console.log('加载更多', res.data);
                            // console.log(this.data.current_page_two,this.data.last_page_two)
                            // this.getClassThr(child, res.data.category_2.data[this.data.twoClass].level, true, pageTwo);
                        } else {
                            // 初始进入取二级分类只开了二级分类
                            if(this.data.cat_level == 2){
                                this.setData({
                                    categoryInfo_two :res.data.list.data
                                })
                            }else{
                                this.setData({
                                    categoryInfo_two :res.data.list.data
                                })
                            }
                            // let arr = res.data.list.data;
                            // let _two = []
                            // arr.forEach(element => {
                            //     if (element.id == child) {
                            //         _two = element.has_many_children
                            //     }
                            // });
                            // this.setData({
                            //     categoryInfo_two: _two,
                            //     current_page_two: res.data.list.current_page,
                            //     last_page_two: res.data.list.last_page
                            // });
                            // wx.hideLoading({
                            //     success: (res) => { },
                            // });
                            // if (this.data.cat_level == '3') {
                            //     // 开启了三级分类
                            //     console.log(this.data.categoryInfo_two,this.data.twoClass)
                            //     if (res.data.list.data[this.data.firstClass].has_many_children.length > 0 && this.data.categoryInfo_two.length > 0 ) {
                            //         this.getClassThr(this.data.categoryInfo_two[this.data.twoClass].id);
                            //     } else {
                            //         console.log('没有二级')
                            //     }
                            // }
                        }

                    } else {
                        wx.showToast({
                            title: res.msg,
                        });
                        wx.hideLoading({
                            success: (res) => { },
                        });
                    }
                },
                fail: (err) => {
                    wx.hideLoading({
                        success: (res) => { },
                    });
                }
            });
        },
        getClassThr(child, leve, loadMore = false, pageTwo = null) {
            if (loadMore && pageTwo) {
                this.setData({
                    twoPage: pageTwo
                });
            }
            // 取三级分类
            let urlStr = app.getNetAddresss("plugin.debt-shop.frontend.goods.goodsTypeList");
            app._getNetWork({
                url: urlStr,
                data: {
                    paginate: 15,
                    parent_id: child,
                    page: this.data.twoPage,
                },
                success: (resdata) => {
                    var res = resdata.data;
                    wx.hideLoading({
                        success: (res) => { },
                    });
                    if (res.result == 1) {
                        if (loadMore) {
                            console.log('加载更多取二级和三级', res);
                            this.setData({
                                categoryInfo_two: this.data.categoryInfo_two.concat(res.data.category_2.data),
                                current_page_two: res.data.category_2.current_page,
                                last_page_two: res.data.category_2.last_page
                            });
                            return;
                        }
                        console.log('取三级category_2里面新增category_3', res.data.list.data);
                        this.data.categoryInfo_two.forEach(element => {
                            console.log(element.id == child)
                            if (element.id == child) {
                                element.has_many_children = res.data.list.data
                            }
                        });
                        this.setData({
                            categoryInfo_two: this.data.categoryInfo_two
                        })
                    } else {
                        wx.hideLoading({
                            success: (res) => { },
                        });
                        wx.showToast({
                            title: res.msg,
                        });
                    }

                },
                fail: (err) => {
                    wx.hideLoading({
                        success: (res) => { },
                    });
                }
            });
        },
        getMoreCategoryNew(e) {
            if (this.data.current_page_first >= this.data.last_page_first) {
                console.log('没有更多一级分类');
                return;
            } else {
                this.setData({
                    onePage: this.data.onePage + 1
                });
            }
            this.getClass(true);
        },
        getMoreCategoryNew_two(e) {
            if (this.data.current_page_two >= this.data.last_page_two) {
                return;
            } else {
                let loadPage_two = this.data.twoPage + 1;
                this.getClassTwo(this.data.categoryInfo[this.data.firstClass].id, true, loadPage_two);
            }
        }
    }
});