class pageChannelExPlan {
    constructor() {
        this.loading = false
        this.finished = false

        this.$footer = $('.dw-common-footer')
        this.$listWrap = $('.video-list__wrap')
        this.$videoList = this.$listWrap.children('.video-list')

        this.init()
    }

    init() {
        this.loadMore()
    }

    loadMore() {
        var self = this
        var footHeight = this.$footer.height()
        var screenHeight = $(window).height()
        $(window).scroll(function () {
            var $body = $('body')
            if ($(this).scrollTop() + screenHeight + footHeight >= $body.height() && !self.loading) {
                self.getData()
            }
        })
    }

    getData() {
        if (this.finished) return
        this.loading = true
        var self = this
        var $pageNum = $('#pageNum')
        var href = $pageNum.find('span:last-child a').attr("href")
        var urlArr = location.href.split('/').slice(0, -1)
        urlArr.push(href)
        $.get(urlArr.join('/'), function (ret) {
            var $ret = $(ret)
            var $ele = $ret.find('.video-list').children()
            var _$pageNum = $ret.find('#pageNum')
            var _$pageNow = _$pageNum.find('#pageNow')
            var index = _$pageNow.index()
            var totalpage = _$pageNum.children().length

            if (index == totalpage - 1) {
                self.finished = true
            }
            $pageNum.remove()
            self.$listWrap.append(_$pageNum)
            self.$videoList.append($ele)
            self.loading = false
        })

        this.checkPage()
    }

    //检查是否只有一页，只有一页则隐藏加载更多按钮
    checkPage() {
        if ($('#pageNum').children().length == 1)
            this.finished = true
    }


}

$(() => {
    new pageChannelExPlan
})
