initMainArticle = () => {
    if(typeof use_main_page != 'undefined' && use_main_page == false){
        console.log('use_main_page is false do not initMainArticle');
        return;
    }
    readed_contents = localStorage.getArray('readed_contents');
    requestAstro(`notice/v1/notices/all`, {}, 'get')
    .then(r => {
        if (r.code == '0000') {
            $(`.main_cont .article_list ul`).empty();
            try {
                r.data.map((item, idx) => {
                    if (idx >= 5) return;
                    let readed_doc_list = readed_contents[item.category] || [];
                    $(`.main_cont .article_list ul`).append(
                        $('#main_article_list_template').text()
                        .replace(/{{title}}/g, item.title)
                        .replace(/{{doc_seq}}/g, item.uid)
                        .replace(/{{board_seq}}/g, item.category)
                        .replace(/{{update_date}}/g, new Date(item.update_time).toLocaleString())
                        .replace(/{{visited}}/g, readed_doc_list.includes(item.uid) ? '' : '<span class="new"></span>')
                        .replace(/{{prev_board_seq}}/g, idx == 0 ? 'not' : r.data[idx - 1].category)
                        .replace(/{{prev_doc_seq}}/g, idx == 0 ? 'not' : (r.data[idx - 1].uid))
                        .replace(/{{next_board_seq}}/g, idx >= (r.data.length - 1) ? 'not' : r.data[idx + 1].category)
                        .replace(/{{next_doc_seq}}/g, idx >= (r.data.length - 1) ? 'not' : r.data[idx + 1].uid)
                    )
                });
            } catch (error) {
                console.error(`isboard/v1/newboardcontent error`, error);
            }
        } else {
            console.error(`isboard/v1/newboardcontent error`, r);
        }
    })
    ;
}

checkReadWholeContents = (callback) => {
    $(`.main_cont .swiper-slide a.item span.num`).addClass('none');

    let board_infos = {
        '-1': 'article',
        'carfull': 1
    }

    $.when(
        new Promise((resolve, reject) => {
            requestAstro('notice/v1/notices/alluid', { categories: ['C', 'N', 'E'] }, 'post')
                .then((r) => resolve(r))
                .catch((f) => resolve(f))
        })
    )
    .done((noticeresponse) => {
        readed_contents = localStorage.getArray('readed_contents');

        if (noticeresponse.code == '0000') {
            let notice_new_content_cnt = 0,
                carfull_new_content_cnt = 0
                ;

            for (let notice_category in noticeresponse.data) {

                let r_content_list = noticeresponse.data[notice_category];

                //로컬에 저장된 읽은 게시물아이디(readed_contents)중에 새로 조회해온 게시물아이디에서 제외된 아이디는 제거함
                readed_contents[notice_category] = (readed_contents[notice_category] || []).filter(x => r_content_list.includes(x));

                let readed_list = readed_contents[notice_category] || [];

                switch (notice_category) {
                    case 'E':
                    case 'N':
                        try {
                            notice_new_content_cnt += r_content_list.length - readed_list.length;
                            if (notice_new_content_cnt < 0) {
                                notice_new_content_cnt = 0
                            }
                        } catch (error) {
                            notice_new_content_cnt = 0;
                        }

                        break;
                    case 'C':
                        try {
                            carfull_new_content_cnt = r_content_list.length - readed_list.length;
                            if (carfull_new_content_cnt < 0) {
                                carfull_new_content_cnt = 0
                            }
                        } catch (error) {
                            carfull_new_content_cnt = 0;
                        }

                        break;
                    default:
                        break;
                }

                $(`.main_cont .swiper-slide a[data-id=${notice_category == 'C' ? 'carfull' : 'notice'}] span.num`).text(`${notice_category == 'C' ? carfull_new_content_cnt : notice_new_content_cnt}`);
                if ((notice_category == 'C' ? carfull_new_content_cnt : notice_new_content_cnt) > 0) {
                    $(`.main_cont .swiper-slide a[data-id=${notice_category == 'C' ? 'carfull' : 'notice'}] span.num`).removeClass('none');
                } else {
                    $(`.main_cont .swiper-slide a[data-id=${notice_category == 'C' ? 'carfull' : 'notice'}] span.num`).addClass('none');
                }
            }
        }

        if (callback) {
            callback();
        }
    });
}

$(()=>{
    //메인화면 신규게시물 클릭
    $('.main_cont .article_list').off('click', '.main_view_detail_article').on('click', '.main_view_detail_article', async function () {
        let pagename = 'notice'
            , $element_a = $(this).find('>a')
            ;
        await loadExternalHTML('notice', 'content/content-notice.html', $('.sub_cont'));

        let target_boardseq = $(this).data('boardseq');
        let target_uid = $(this).data('docseq');

        $.when(
            requestAstro(`notice/v1/notice/${target_uid}`, {}, 'get'),
            loadBoardData('notice', false, { categories: ['E', 'N'] })
        )
        .then((board_data) => {
            if (board_data.code == '0000') {
                initFooter('notice_view');

                //수정버튼 표시위해...
                $('.view_cont .for_board_view').data('creator', board_data.data.creator);

                $('#detail_title').text(board_data.data.title);

                $('#detail_span_info').text(`${board_data.data.creator_name || ''}/${board_data.data.dept_name || ''}/${board_data.data.update_time ? (gformatter.date(new Date(board_data.data.update_time), 'y-M-d')) : ''}`);

                $('#detail_board_cont').empty();
                $('#detail_board_cont').html(board_data.data.contents);

                //폰트사이즈 시작
                $('#font-setting-buttons').easyView({
                    container: '#detail_board_cont',
                    increaseSelector: '.increase',
                    decreaseSelector: '.decrease',
                    normalSelector: '.reset'
                });

                readed_contents = JSON.parse(localStorage.getItem('readed_contents') || '{}');
                if (readed_contents[board_data.data.category] == undefined) {
                    readed_contents[board_data.data.category] = [];
                }

                if (!readed_contents[board_data.data.category].some(seq => seq == board_data.data.uid)) {
                    readed_contents[board_data.data.category].push(board_data.data.uid);
                }

                localStorage.setItem('readed_contents', JSON.stringify(readed_contents));
                $element_a.addClass('visited');

                $(`.sub_cont .board_list a[data-uid=${board_data.data.uid}]`).addClass('visited');

                let a_list = $(`.sub_cont .board_list>li:not(.none)>a`);
                let thisindex = -1;

                for (let idx = 0; idx < a_list.length; idx++) {
                    if ($(a_list[idx]).data('boardseq') == board_data.data.board_seq && $(a_list[idx]).data('docseq') == board_data.data.doc_seq) {
                        thisindex = idx;
                        break;
                    }
                }

                let previndex = thisindex - 1;
                let nextindex = thisindex + 1;

                //이전 게시물 버튼
                if (previndex == -1) {
                    $('#btn_prev_content').addClass('none');
                } else {
                    $('#btn_prev_content').removeClass('none');
                }

                $('#btn_prev_content').data('uid', $(a_list[previndex]).data('uid'));

                //다음 게시물 버튼
                if (nextindex == a_list.length) {
                    $('#btn_next_content').addClass('none');
                } else {
                    $('#btn_next_content').removeClass('none');
                }

                $('#btn_next_content').data('uid', $(a_list[nextindex]).data('uid'));

                $('#btn_prev_content,#btn_next_content').addClass('board-astro');
                $('#btn_prev_content,#btn_next_content').removeClass('board-saeol');

            }
        })
        .always(() => {
            $(`.view_cont, .header.board, .foot_bt, .for_board_view, .header.board .search`).removeClass('none');
            $('.main_cont, .sub_cont, .header.main, .for_board_edit').addClass('none');
        });
    });
})