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

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

            function clearDgnsTable() {
                $root.empty();
                var $th_cnt = $("<th>차수</th>");
                var $th_date = $("<th>테스트일시</th>")
                var $th_skTy = $("<th>피부타입</th>");
                var $th_dtl = $("<th>기능</th>");
                $root.append($th_cnt);
                $root.append($th_date);
                $root.append($th_skTy);
                $root.append($th_dtl);
            }

            function displayEmptyData () {
                var $tr = $("<tr></tr>");
                var $td = $("<td colspan='4'>테스트결과가 없습니다.</td>");
                $tr.append($td);
                $root.append($tr);
                $("#prevBtn").css("display", "none");
                $("#nextBtn").css("display", "none");
            }

            function displayDgnsRow(dgnsData) {
                var $tr = $("<tr onclick='location.href=\"/clfd/dgns/dtl.do?dgns=" + dgnsData.dgnsId + "\"'></tr>");
                var $td_cnt = $("<td>"+dgnsData.dgnsCnt+"회차 테스트</td>");
                var $td_date = $("<td>"+dgnsData.regDt+"</td>");
                var $td_skTy = $("<td>"+dgnsData.skTy.skTyCd+"</td>");
                var $td_dtl = $("<td><button type='button' class='dia-btn__dtl' onclick='location.href=\"/clfd/dgns/dtl.do?dgns=" + dgnsData.dgnsId + "\"'>상세보기</button></td>");
                $tr.append($td_cnt);
                $tr.append($td_date);
                $tr.append($td_skTy);
                $tr.append($td_dtl);
                $root.append($tr);
            }
        }
    }
}());
