document.addEventListener('DOMContentLoaded', () => {
    const p = new URLSearchParams(window.location.search);
    if (!p.get('profileRoom')) initLiveList();
    function initLiveList() {
        let queryData = {
            page: 1,
            gameId: '',
            index: 0,
            isLoading: false,
            totalPage: 0
        };
        $('.joe_live__type-title .icon').on('click', function () {
            if (queryData.isLoading) return;
            if (queryData.index === 3) queryData.index = 0;
            queryData.index++;
            renderLiveType();
        });
        $('.joe_live__type-list').on('click', '.item', function () {
            if (queryData.isLoading) return;
            $(this).addClass('active').siblings().removeClass('active');
            queryData.page = 1;
            queryData.gameId = $(this).attr('data-gameId');
            renderLiveList();
        });
        renderLiveType();
        function renderLiveType() {
            $.ajax({
                url: '/usr/themes/Joe/assets/json/joe.live.json',
                success(res) {
                    const item = res[queryData.index];
                    $('.joe_live__type-title .text').html(item.name);
                    let htmlStr = '
全部';
                    item.list.forEach(_ => (htmlStr += `${_.name}`));
                    $('.joe_live__type-list').html(htmlStr);
                    $('.joe_live__type-list .item').first().click();
                }
            });
        }
        function renderLiveList() {
            queryData.isLoading = true;
            $('.joe_live__list').html('');
            $.ajax({
                url: Joe.BASE_API,
                type: 'POST',
                data: {
                    routeType: 'huya_list',
                    page: queryData.page,
                    gameId: queryData.gameId
                },
                success(res) {
                    if (res.code !== 1) return;
                    let htmlStr = '';
                    res.data.datas.forEach(_ => {
                        htmlStr += `
                            
                                
                                    ${_.recommendTagName}
                                    
                                 
                                
                                    
                                    ${_.nick}
                                    ${_.introduction}
                                    
                                 
                            
                        `;
                    });
                    $('.joe_live__list').html(htmlStr);
                    queryData.totalPage = res.data.totalPage;
                    initPagination();
                },
                complete: () => (queryData.isLoading = false)
            });
        }
        function initPagination() {
            let htmlStr = '';
            if (queryData.page != 1) {
                htmlStr += `
            		
            		
            		
            	`;
            }
            htmlStr += ``;
            if (queryData.page != queryData.totalPage) {
                htmlStr += `
            		
            		
            	`;
            }
            if (queryData.page < queryData.totalPage) htmlStr += ``;
            $('.joe_live__pagination').html(htmlStr);
        }
        $('.joe_live__pagination').on('click', '.joe_live__pagination-item', function () {
            const page = $(this).attr('data-page');
            if (!page || queryData.isLoading) return;
            window.scrollTo({ top: 0, behavior: 'smooth' });
            queryData.page = Number(page);
            renderLiveList();
        });
        function parseNum(num = 0) {
            if (num >= 10000) return Math.round(num / 1000) / 10 + '万';
            return num;
        }
    }
});