var boardListService = (function () {
    var boardList = [];
    var searchOption = {
        start: 0,
        length: 10,
        pagingYn: true
    };

    return {
        boardListApi : function (option) {
            $.ajax({
                url: "/clfd/bod/list.json?bodId=140",
                data: searchOption,
                type: "GET",
                success: function (res) {
                    if(res.result === "success") {
                        boardList = res.boardList;
                        if (option && option.scb) {
                            option.scb(res);
                        }
                    } else {
                        if (option && option.ecb) {
                            option.ecb(res);
                        }
                    }
                }
            });
        },
        setSearchOptionToPrev : function () {
            if (!boardList) { return; }
            if (searchOption.start - boardList.length < 0) {
                return false;
            } else {
                searchOption.start -= searchOption.length;
                return true;
            }
        },
        setSearchOptionToNext : function () {
            if (!boardList) { return; }
            if (boardList.length < 10) {
                return false;
            } else {
                searchOption.start += boardList.length;
                return true;
            }
        },
        drawBoardList : function ($root) {
            if (!boardList) { return; }
            if (boardList.length === 0) {
                if (searchOption.start === 0) {
                    clearBoardTable();
                    displayEmptyData();
                } else {
                    alert("다음페이지가 없습니다.");
                    searchOption.start -= searchOption.length;
                }
            } else {
                clearBoardTable();
                for (var i=0; i<boardList.length; i++) {
                    displayBoardRow(boardList[i], i);
                }
            }

            function clearBoardTable() {
                $root.empty();
                var $th_no = $("<th>번호</th>");
                var $th_id = $("<th style='display: none;'>게시물ID</th>");
                var $th_company = $("<th>기업</th>");
                var $th_title = $("<th>제목</th>");
                var $th_wrtDate = $("<th>등록일</th>");
                var $th_hit = $("<th>조회수</th>");
                $root.append($th_no);
                $root.append($th_id);
                $root.append($th_company);
                $root.append($th_title);
                $root.append($th_wrtDate);
                $root.append($th_hit);
            }

            function displayEmptyData() {
                var $tr = $("<tr></tr>");
                var $td = $("<td colspand='5'>게시물이 없습니다.</td>");
                $tr.append($td);
                $root.append($tr);
                $("#prevBtn").css("display", "none");
                $("#nextBtn").css("display", "none");
            }

            function displayBoardRow(boardData, cnt) {
                var $tr = $("<tr></tr>");
                var $th_no = $("<td>"+(cnt+1)+"</td>");
                var $th_id = $("<td style='display: none;'>"+boardData.artiId+"</td>");
                var $th_company = $("<td>"+boardData.regNm+"</td>");
                var $th_title = $("<td>"+boardData.title+"</td>");
                var $th_wrtDate = $("<td>"+boardData.regDate+"</td>");
                var $th_hit = $("<td>"+boardData.hit+"</td>");
                $tr.append($th_no);
                $tr.append($th_id);
                $tr.append($th_company);
                $tr.append($th_title);
                $tr.append($th_wrtDate);
                $tr.append($th_hit);
                $root.append($tr);
            }
        }
    }
}());