"use strict";
/**
 * mplus_safety_infusion
 */
var mplus_safety_infusion = function(){
    // 상속
    mplus_common.call(this);

    // 상위 객체 생성
    //var mplusPatient = new mplus_mobile_patient();
    //var mplusNursing = new mplus_nursing();

    //super
    var self = this;

    //==== 변수 { ====
    // column 정렬의 토글 상태 제어용 : 0: not selected, 1:오름차순, 2:내림차순
    var varToggleCol1 = 0;
    var varToggleCol2 = 0;
    var varToggleCol3 = 0;
    var varToggleCol4 = 0;
    var varToggleCol5 = 0;
    var varToggleCol6 = 0;
    var varToggleCol7 = 0;
    var varToggleCol8 = 0;

    var varResultLists = [];

    var varSearchCls = "OP";// 조회구분(I:입원, O:외래, E:응급, OP:수술, T:시술, C:협진)

    var varSearchDateGap = 2;	// 날짜검색 간격

    //투약번호 Length처리
    var inputInfusionLength = 0;

    var sliderCapacity = $("#sliderCapacity");

    var sliderDisCapacity = $("#sliderDisCapacity");
    //==== 변수 } ====

    /**
     * 초기화
     */
    this.init = function(){ //여기서 모든화면 콤보박스데이터를 집어넣자!!! 그래야지 화면이동할때 속도가 오래걸리지않음!!!
        // 접근 권한 체크
        checkAccessPermission();

        // 초기 화면 조건 구성
        initCondition();

        // 이벤트 초기화
        addEvent();

        // 환자 검색 실행 및 환자 정보 화면 출력
        //searchPatient();

        $("#txtInfusionPatientSearchTxt").focus();
    };

    //==== 접근 권한 체크 { ====
    var checkAccessPermission = function(){
        //if( !self.common.checkAccessJobKind( "1000")){
        //	self.alertTrue("접근 권한이 없습니다.", self.common.gotoDefaultHomePage);
        //}

        //if( !self.common.checkAccessUserAuth( "01")){
        //	self.alertTrue("접근 권한이 없습니다.", self.common.gotoDefaultHomePage);
        //}

        //self.common.disableElements( $("#myModalBtn"));
    };
    //==== 접근 권한 체크 } ====

    //==== 초기화 설정 { ====
    var initCondition = function(){
        // 초기 화면 조건 구성

        //환자정보 초기화
        initPatientInfo();

        //slider 초기화
        sliderCapacity.slider({ value: 100 });
        $("#txtInfusionSize").val(sliderCapacity.val());

        sliderDisCapacity.slider({ value: 100 });
        $("#txtInfusionDisSize").val(sliderDisCapacity.val());

        // 종료 날짜를 현재 날짜로 등록
        var varCurDate = new Date();
        var strCurDate = self.util.toDate( new Date());
        $("#dateInfusionEndDt").val( strCurDate);

        // 시작 날짜를 날짜 검색 간격 전 날짜를 등록
        var var2DaysAgo = varCurDate;
        var2DaysAgo.setDate( varCurDate.getDate() - varSearchDateGap);
        var str2DaysAgo = self.util.toDate( var2DaysAgo);
        //$("#dateOppatientStartDt").val( str2DaysAgo);
        $("#dateInfusionStartDt").val( str2DaysAgo);

    };

    //환자 정보 초기화
    var initPatientInfo = function(){
        //$("#divResultRows").empty();

        //투약 실행 버튼
        $("#btnInputInfusionAct").attr("disabled" , true);

        $(".modal-patient-name").text("-");
        $(".modal-patient-id").text("-");
        $(".modal-patient-age-gender").text("-");
        $(".modal-patient-treat-cls").text("-");
    };

    //투약실시 초기화
    var initInputInfusion = function(){
        $("#infusionActionNum").empty();
        //CheckBoxLength 초기화
        inputInfusionLength = 0;
        //처방번호 체크
        for(var fCnt = 0 ; fCnt < varResultLists.length ; fCnt++){
            if($("#checkInfusionSel" + fCnt).is(":checked")){
                var tempSpan = "<p>"
                    + "<span id=\"spanInputInfusionNum" + inputInfusionLength +"\" style=\"color:red;\">" + varResultLists[fCnt].col6 + "</span>"
                    + "&nbsp;&nbsp;<button type=\"button\" name=\"button\" class=\"btn btn-primary classBtnInPutActBarcode\" btnLength=\"" + inputInfusionLength + "\" id=\"btnInPutActBarcode" + inputInfusionLength + "\" style=\"color: #ffffff;background: #0067a1;text-align: center;\">"
                    + "<span style=\"color:white;\">⊙</span>"
                    + "</button><p/>";
                $("#infusionActionNum").append(tempSpan);
                inputInfusionLength++;

                //시작 종료 버튼 처리
                if(varResultLists[fCnt].col11 == "Y"){
                    $("#btnInfusionAct").attr("disabled" , true);
                }else{
                    $("#btnInfusionAct").attr("disabled" , false);
                }
                if(varResultLists[fCnt].col12 == "Y"){
                    $("#btnInfusionEnd").attr("disabled" , true);
                }else{
                    $("#btnInfusionEnd").attr("disabled" , false);
                }
            }
        }
        eventMakeBarcodeCondition();
    };

    //바코드 리딩 후 투약실시 초기화
    var initBarcodeInputInfusion = function(barcodeData){
        $("#infusionActionNum").empty();
        //CheckBoxLength 초기화
        inputInfusionLength = 0;
        //처방번호 체크
        for(var fCnt = 0 ; fCnt < varResultLists.length ; fCnt++){
            if(varResultLists[fCnt].col6 == barcodeData){
                var tempSpan = "<p>"
                    + "<span id=\"spanInputInfusionNum" + inputInfusionLength +"\" style=\"color:blue;\">" + varResultLists[fCnt].col6 + "</span>"
                    //+ "&nbsp;&nbsp;<button type=\"button\" name=\"button\" class=\"btn btn-primary classBtnInPutActBarcode\" btnLength=\"" + inputInfusionLength + "\" id=\"btnInPutActBarcode" + inputInfusionLength + "\" style=\"color: #ffffff;background: #0067a1;text-align: center;\">"
                    //+ "<span style=\"color:white;\">⊙</span></button>"
                    + "<p/>";
                $("#infusionActionNum").append(tempSpan);
                inputInfusionLength++;

                //시작 종료 버튼 처리
                if(varResultLists[fCnt].col11 == "Y"){
                    $("#btnInfusionAct").attr("disabled" , true);
                }else{
                    $("#btnInfusionAct").attr("disabled" , false);
                }
                if(varResultLists[fCnt].col12 == "Y"){
                    $("#btnInfusionEnd").attr("disabled" , true);
                }else{
                    $("#btnInfusionEnd").attr("disabled" , false);
                }
                return true;
            }
        }
        return false;
        //eventMakeBarcodeCondition();
    };

    // 환자정보 요청
    var getPatientInfo = function(){
        var param = {
            userId: $("#txtInfusionPatientSearchTxt").val(),
            patientId: "",
            receiptNo: "",
            treatCls: "I"
        };
        self.medical.getPatientInfo(param, processGetPatientInfo);
    };

    // 투약 목록을 처리
    var processGetPatientInfo = function( lists){

        $(".modal-patient-name").text(lists[0].patientNm);
        $(".modal-patient-id").text(lists[0].patientId);
        $(".modal-patient-age-gender").text(lists[0].age + " / " + lists[0].gender);
        $(".modal-patient-treat-cls").text("입원");

        getInfusionList();
        $("#txtInfusionNumberSearchTxt").focus();
    };

    // 투약실시
    var inputStartInfusion = function(){
        var params = {param: []};
        for(var fCnt = 0 ; fCnt < inputInfusionLength ; fCnt++){
            params.param.push({
                param1: $("#spanInputInfusionNum" + fCnt).text(),
                param2: "20190101",
                param3: "121212",
                param4: "aaa",
                param5: $("#txtInfusionSize").val()
            });
        }
        self.safety.inputStartInfusion(params, processInputStartInfusion);
    };

    // 투약종료
    var inputEndInfusion = function(){
        var params = {param: []};
        for(var fCnt = 0 ; fCnt < inputInfusionLength ; fCnt++){
            params.param.push({
                param1: $("#spanInputInfusionNum" + fCnt).text(),
                param2: "20190101",
                param3: "121212",
                param4: "aaa",
                param5: $("#txtInfusionDisSize").val()
            });
        }
        self.safety.inputStartInfusion(params, processInputEndInfusion);
    };

    var processInputStartInfusion = function(lists){


        if(lists != undefined && lists.length > 0){
            var inputCnt = 0;
            for(var fCnt = 0 ; fCnt < lists.length ; fCnt++){
                if(lists[fCnt].returnCd == "0000"){
                    inputCnt++;
                }
            }
            self.alert(inputCnt + "건 처리되었습니다.");
            //데이터 초기화
            $("#mplusAlertInfusionAction").modal("hide");
            initPatientInfo();
            getPatientInfo();
            $("#txtInfusionNumberSearchTxt").focus();
        }else{
            self.alert("처리중 오류가 발생했습니다.");
        }

    };

    var processInputEndInfusion = function(lists){


        if(lists != undefined && lists.length > 0){
            var inputCnt = 0;
            for(var fCnt = 0 ; fCnt < lists.length ; fCnt++){
                if(lists[fCnt].returnCd == "0000"){
                    inputCnt++;
                }
            }
            self.alert(inputCnt + "건 처리되었습니다.");
            //데이터 초기화
            $("#mplusAlertInfusionAction").modal("hide");
            initPatientInfo();
            getPatientInfo();
            $("#txtInfusionNumberSearchTxt").focus();
        }else{
            self.alert("처리중 오류가 발생했습니다.");
        }

    };

    var checkInputInfusionData = function(){
        if($("#dateInfusionActionDate").val() == ""){
            self.alert("투약일을 입력해주세요.");
            return false;
        } else if($("#dateInfusionActionTime").val()  == "") {
            self.alert("투약시간을 입력해주세요.");
            return false;
        }

        //바코드 체크
        /*
        var btnInPutActBarcodeCheck = false;
        for(var fCnt = 0 ; fCnt < inputInfusionLength ; fCnt++){
            console.log("TEST : " + $("#btnInPutActBarcode" + fCnt).is("[disabled]"));
            if(!$("#btnInPutActBarcode" + fCnt).is("[disabled]")){
                btnInPutActBarcodeCheck = true;
            }
        }
        if(btnInPutActBarcodeCheck){
            self.alert("투약번호 바코드를 확인해주세요.");
            return false;
        }
        */

        return true;
    };

    var settingSetInputInfusion = function(){
        $("#dateInfusionActionDate").val(moment(new Date()).format("YYYY-MM-DD"));
        $("#dateInfusionActionTime").val(moment(new Date()).format("HH:mm:ss"));

        $("#dateInfusionEndDate").val(moment(new Date()).format("YYYY-MM-DD"));
        $("#dateInfusionEndTime").val(moment(new Date()).format("HH:mm:ss"));
    };

    var checkBarcodeNum = function(barcodeNum){
        for(var fCnt = 0 ; fCnt < inputInfusionLength ; fCnt++){
            if(barcodeNum == $("#spanInputInfusionNum" + fCnt).text()){
                $("#spanInputInfusionNum" + fCnt).css("color" , "blue");
                $("#btnInPutActBarcode" + fCnt).attr("disabled" , true);
            }
        }
    };

    // 투약정보
    var getInfusionList = function(){
        var param = {
            userId: "",
            patientId: "",
            receiptNo: "",
            treatCls: ""
        };
        self.safety.getInfusionList(param, processGetInfusionList);
    };

    var processGetInfusionList = function(lists){
        varResultLists = lists;
        var strDisplayTemp = "";
        $("#divResultRows").empty();
        if( lists.length == 0){
            strDisplayTemp = "<div class=\"row text-center\" style=\"line-height: 60px; vertical-align: middle;\"> 검색 결과가 없습니다. </div>"
                + "<div style=\"height: 55vh;\"></div>";
        }
        for( var i = 0; i < lists.length; i++){

            strDisplayTemp += "<div class=\"chart-content well\">"
                + "  <div class=\"row patient-row\" patientNo=\"" + i + "\" patientId=\"" + lists[i].col1 + "\" patientNm=\"" + lists[i].col2 + "\" age=\"" + lists[i].col2 + "\" gender=\"" + lists[i].col2 + "\" treatCls=\"" + lists[i].col2 + "\" receiptNo=\"" + lists[i].col2 + "\" deptCd=\"" + lists[i].col2.trim() + "\" doctorId=\"" + lists[i].col2 + "\">"
                + "    <div class=\"col-xs-12\">"
                //+ "      <div class=\"col-xs-1 text-center\" style=\"padding:0px;\"><input type=\"checkbox\" class=\"form-check-input checkClassInfusionSel\" id=\"checkInfusionSel" + i + "\"></div>"
                + "      <div class=\"col-xs-2 text-center\" style=\"padding:0px;\">" + moment(lists[i].col1).format("MM-DD") + "</div>"
                + "      <div class=\"col-xs-1 text-center\" style=\"padding:0px;\">" + lists[i].col2 + "</div>"
                + "      <div class=\"col-xs-1 text-center\" style=\"padding:0px;\">" + lists[i].col3 + "</div>"
                + "      <div class=\"col-xs-1 text-center\" style=\"padding:0px;\">" + lists[i].col4 + "</div>"
                + "      <div class=\"col-xs-7 text-center\" style=\"padding:0px;\">" + lists[i].col5 + "</div>"
                + "    </div>"
                + "</div>"
                + "</div>";
            //console.log( i + " : " + lists[i].patientId + " : " + lists[i].patientNm);
        }

        $("#divResultRows").append(strDisplayTemp);	// element 추가
        eventMakeCondition();
    };

    //==== 초기화 설정 } ====

    //==== 이벤트 설정 { ====
    var addEvent = function(){

        // 컬럼 클릭의 정렬 이벤트
        eventColumnClick();

        eventRowClick();

        eventCondition();

        eventBackBlock();
    };

    // 20190228 shinsunwoo 환자의 바코드 추출 toNative 호출
    var readBarcode = function(barMode){
        var callbackFn = "window.activeObj.readBarcodeCallBack";
        if(barMode == "search"){
            callbackFn = "window.activeObj.readBarcodeCallBack";
        }else if(barMode == "check"){
            callbackFn = "window.activeObj.readBarcodeCallBackCheck";
        }else if(barMode == "number"){
            callbackFn = "window.activeObj.readBarcodeCallBackNumber";
        }else{
            callbackFn = "window.activeObj.readBarcodeCallBack";
        }
        console.log('call readBarcode');
        var msg = {
            type : "command",
            functionType : "readBarcode",
            value : {
                callbackFn : callbackFn
            }
        };
        self.toNative( msg );
    };

    // 20190228 shinsunwoo 환자의 바코드 추출 callback 처리
    this.readBarcodeCallBack = function( returnCode){
        var results = new Object();
        if( typeof returnCode === "string" ){
            results = self.util.parseJson( returnCode );
        } else {
            results = returnCode;
        }

        if( results.success != undefined && results.success == "true" ) {
            console.log("readBarcode : success");
            console.log( results.result);
            $("#txtInfusionPatientSearchTxt").val(results.result)
            getPatientInfo();
        } else {
            console.log("readBarcode : failed");
            self.alert( "readBarcode APP function 호출 오류 " );
        }
    };

    this.readBarcodeCallBackCheck = function( returnCode){
        var results = new Object();
        if( typeof returnCode === "string" ){
            results = self.util.parseJson( returnCode );
        } else {
            results = returnCode;
        }

        if( results.success != undefined && results.success == "true" ) {
            console.log("readBarcode : success");
            console.log( results.result);
            checkBarcodeNum(results.result);

        } else {
            console.log("readBarcode : failed");
            self.alert( "readBarcode APP function 호출 오류 " );
        }
    };

    this.readBarcodeCallBackNumber = function( returnCode){
        var results = new Object();
        if( typeof returnCode === "string" ){
            results = self.util.parseJson( returnCode );
        } else {
            results = returnCode;
        }

        if( results.success != undefined && results.success == "true" ) {
            console.log("readBarcode : success");
            console.log( results.result);
            $("#txtInfusionNumberSearchTxt").val(results.result);
            $("#txtInfusionNumberSearchTxt").trigger("keydown");

        } else {
            console.log("readBarcode : failed");
            self.alert( "readBarcode APP function 호출 오류 " );
        }
    };

    this.deviceBarcodeCallBackCheck = function( returnCode){
        var results = new Object();
        if( typeof returnCode === "string" ){
            results = self.util.parseJson( returnCode );
        } else {
            results = returnCode;
        }

        if( results.success != undefined && results.success == "true" ) {
            console.log("readBarcode : success");
            console.log( results.result);
            checkBarcodeNum(results.result);

        } else {
            console.log("readBarcode : failed");
            self.alert( "readBarcode APP function 호출 오류 " );
        }
    };

    //검색 페이지 뒤로가기 막기
    var eventBackBlock = function(){
        $(window).off("pageshow");
        $(window).bind("pageshow", function(event) {
            if (event.originalEvent.persisted) {
                document.location.reload();
            }
        });
    };

    var eventColumnClick = function(){
        /*
        $("#divResultHeaderColumn1").off("click");
        $("#divResultHeaderColumn1").on("click", function(){
            //alert("column1 clicked!!");
            sortColumn( 1, "patientId");
        });

        $("#divResultHeaderColumn2").off("click");
        $("#divResultHeaderColumn2").on("click", function(){
            //alert("column2 clicked!!");
            sortColumn( 2, "patientNm");
        });

        $("#divResultHeaderColumn3").off("click");
        $("#divResultHeaderColumn3").on("click", function(){
            //alert("column3 clicked!!");
            sortColumn( 3, "roomNm");
        });

        $("#divResultHeaderColumn4").off("click");
        $("#divResultHeaderColumn4").on("click", function(){
            //alert("column4 clicked!!");
            sortColumn( 4, "age");
        });

        $("#divResultHeaderColumn5").off("click");
        $("#divResultHeaderColumn5").on("click", function(){
            //alert("column5 clicked!!");
            sortColumn( 5, "operationDt");
        });

        $("#divResultHeaderColumn6").off("click");
        $("#divResultHeaderColumn6").on("click", function(){
            //alert("column6 clicked!!");
            sortColumn( 6, "anesthesiaTy");
        });

        $("#divResultHeaderColumn7").off("click");
        $("#divResultHeaderColumn7").on("click", function(){
            //alert("column7 clicked!!");
            sortColumn( 7, "deptNm");
        });

        $("#divResultHeaderColumn8").off("click");
        $("#divResultHeaderColumn8").on("click", function(){
            //alert("column8 clicked!!");
            sortColumn( 8, "operationStatNm");
        });
        */
    };

    var eventRowClick = function(){

    };

    var eventCondition = function(){

        ///////////////////////////////////////////////////////////////////////////////// 2019.09.04 추가 Start

        //데이터 전체길이 입력시 검색
        $("#txtInfusionPatientSearchTxt").off("keydown");
        $("#txtInfusionPatientSearchTxt").on("keydown" , function(e){
            if (e.keyCode == 13 && $("#txtInfusionPatientSearchTxt").val().length != 0) {
                initPatientInfo();
                getPatientInfo();
            }else if(e.keyCode == 13 && $("#txtInfusionPatientSearchTxt").val().length == 0){
                self.alert("환자번호를 입력해주세요.");
                $("#txtInfusionPatientSearchTxt").focus();
            }
        });

        //포커스 초기화
        $("#txtInfusionPatientSearchTxt").off("focus");
        $("#txtInfusionPatientSearchTxt").on("focus" , function(){
            $(this).val("");
        });

        //데이터 전체길이 입력시 검색
        $("#txtInfusionNumberSearchTxt").off("keydown");
        $("#txtInfusionNumberSearchTxt").on("keydown" , function(e){
            if (e.keyCode == 13 && varResultLists.length != 0) {
                if(!initBarcodeInputInfusion($(this).val())){
                    self.alert("처방되지 않은 투약번호를 입력하셨습니다.");
                    return;
                }
                settingSetInputInfusion();
                $("#mplusAlertInfusionAction").modal("show");
                $(this).val("");
            }else if(e.keyCode == 13 && varResultLists.length == 0){
                self.alert("환자번호를 검색해주세요.");
                $("#txtInfusionPatientSearchTxt").focus();
            }
        });

        //포커스 초기화
        $("#txtInfusionNumberSearchTxt").off("focus");
        $("#txtInfusionNumberSearchTxt").on("focus" , function(){
            $(this).val("");
            $(this).prop("placeholder" , "IVF바코드를 스캔해주세요.");
        });

        // 투약번호 선택 처리
        $("#btnBarcodeSearchSelect").off("click");
        $("#btnBarcodeSearchSelect").on("click", function(){
            $("#txtInfusionNumberSearchTxt").val("");
            $("#txtInfusionNumberSearchTxt").focus();
        });

        // 바코드 검색
        $("#btnBarcodeSearch").off("click");
        $("#btnBarcodeSearch").on("click", function(){
            $("#txtInfusionNumberSearchTxt").val("");
            readBarcode("number");
        });
        
        $("#mplusAlert").off("hidden.bs.modal");
        $("#mplusAlert").on("hidden.bs.modal", function () {
        	$("#txtInfusionNumberSearchTxt").focus();
        });

        ///////////////////////////////////////////////////////////////////////////////// 2019.09.04 추가 End

        // 환자 조회 이벤트 처리
        $("#btnInfusionSearch").off("click");
        $("#btnInfusionSearch").on("click", function(){
            var searchTxt = $("#txtInfusionPatientSearchTxt").val();
            if(searchTxt == ""){
                self.alert("환자번호를 입력해주세요.");
                return;
            }
            initPatientInfo();
            getPatientInfo();
        });

        // 바코드 검색
        $("#btnBarcodeSearch").off("click");
        $("#btnBarcodeSearch").on("click", function(){
            $("#txtInfusionPatientSearchTxt").val("");
            readBarcode("search");
        });

        // 투약처리
        $("#btnInputInfusionAct").off("click");
        $("#btnInputInfusionAct").on("click", function(){
            initInputInfusion();
            settingSetInputInfusion();
            $("#mplusAlertInfusionAction").modal("show");
        });

        // 투약실시 선택
        /*
        $("#checkInfusionAll").off("change");
        $("#checkInfusionAll").on("change", function(){
            if($("#checkInfusionAll").is(":checked")){
                $(".checkClassInfusionSel").prop("checked" , true);
                $(".checkClassInfusionSel").trigger("change");
            }else{
                $(".checkClassInfusionSel").prop("checked" , false);
                $(".checkClassInfusionSel").trigger("change");
            }
        });
        */
        // 투약실시
        $("#btnInfusionAct").off("click");
        $("#btnInfusionAct").on("click", function(){
            if(checkInputInfusionData()){
                inputStartInfusion();
            }
        });

        // 투약종료
        $("#btnInfusionEnd").off("click");
        $("#btnInfusionEnd").on("click", function(){
            if(checkInputInfusionData()){
                inputEndInfusion();
            }
        });

        // 투약취소
        $("#btnInfusionCancel").off("click");
        $("#btnInfusionCancel").on("click", function(){
            $("#mplusAlertInfusionAction").modal("hide");
            $("#txtInfusionNumberSearchTxt").focus();
        });

        //slider 이벤트
        sliderCapacity.on("input change", function(e) {
            $("#txtInfusionSize").val($(this).val());
        });

        sliderDisCapacity.on("input change", function(e) {
            $("#txtInfusionDisSize").val($(this).val());
        });
    };

    var eventMakeCondition = function() {
        $(".checkClassInfusionSel").off("change");
        $(".checkClassInfusionSel").on("change", function(){
            //하나의 수액만 처리
            if($(this).is(":checked")){
                $(".checkClassInfusionSel").prop("checked" , false);
                $(this).prop("checked" , true);
                $("#btnInputInfusionAct").attr("disabled" , false);
            }else{
                $("#btnInputInfusionAct").attr("disabled" , true);
            }
        });
    };

    var eventMakeBarcodeCondition = function() {
        $(".classBtnInPutActBarcode").off("click");
        $(".classBtnInPutActBarcode").on("click", function(){
            //readBarcode("check");
            //TEST 삭제
            console.log("spanInputInfusionNum0 : " + $("#spanInputInfusionNum0").text());
            checkBarcodeNum($("#spanInputInfusionNum0").text());
        });
    };



    //==== 이벤트 설정 } ====

    //==== 환자 정보 조회 처리 { ====

    //==== 환자 정보 조회 처리 } ====

    //==== 컬럼 정렬 프로세스 { ====
    // 컬럼 상태를 초기화 처리
    var initColumnStatus = function(){
        varToggleCol1 = 0;
        varToggleCol2 = 0;
        varToggleCol3 = 0;
        varToggleCol4 = 0;
        varToggleCol5 = 0;
        varToggleCol6 = 0;
        varToggleCol7 = 0;
        varToggleCol8 = 0;

        // 컬럼 정렬 아이콘 초기화 :  ▼ ▲
        $("#spanSortColumn1").text("");
        $("#spanSortColumn2").text("");
        $("#spanSortColumn3").text("");
        $("#spanSortColumn4").text("");
        $("#spanSortColumn5").text("");
        $("#spanSortColumn6").text("");
        $("#spanSortColumn7").text("");
        $("#spanSortColumn8").text("");
    };

    // 컬럼 상태를 설정(정렬 조건 적용)
    var setColumnStatus = function( columnNo){

        // 클릭된 column의 현재 상태를 백업
        var copyToggleStatus = 0;
        if( columnNo == 1)
            copyToggleStatus = varToggleCol1;
        else if( columnNo == 2)
            copyToggleStatus = varToggleCol2;
        else if( columnNo == 3)
            copyToggleStatus = varToggleCol3;
        else if( columnNo == 4)
            copyToggleStatus = varToggleCol4;
        else if( columnNo == 5)
            copyToggleStatus = varToggleCol5;
        else if( columnNo == 6)
            copyToggleStatus = varToggleCol6;
        else if( columnNo == 7)
            copyToggleStatus = varToggleCol7;
        else if( columnNo == 8)
            copyToggleStatus = varToggleCol8;

        // column 상태를 초기화
        initColumnStatus();

        // 클릭된 column의 백업된 상태에서 한단계 진핸된 상태로 변경
        if( columnNo == 1)
            copyToggleStatus == 0 ? varToggleCol1 = 1 : copyToggleStatus == 1 ? varToggleCol1 = 2 : varToggleCol1 = 1;
        else if( columnNo == 2)
            copyToggleStatus == 0 ? varToggleCol2 = 1 : copyToggleStatus == 1 ? varToggleCol2 = 2 : varToggleCol2 = 1;
        else if( columnNo == 3)
            copyToggleStatus == 0 ? varToggleCol3 = 1 : copyToggleStatus == 1 ? varToggleCol3 = 2 : varToggleCol3 = 1;
        else if( columnNo == 4)
            copyToggleStatus == 0 ? varToggleCol4 = 1 : copyToggleStatus == 1 ? varToggleCol4 = 2 : varToggleCol4 = 1;
        else if( columnNo == 5)
            copyToggleStatus == 0 ? varToggleCol5 = 1 : copyToggleStatus == 1 ? varToggleCol5 = 2 : varToggleCol5 = 1;
        else if( columnNo == 6)
            copyToggleStatus == 0 ? varToggleCol6 = 1 : copyToggleStatus == 1 ? varToggleCol6 = 2 : varToggleCol6 = 1;
        else if( columnNo == 7)
            copyToggleStatus == 0 ? varToggleCol7 = 1 : copyToggleStatus == 1 ? varToggleCol7 = 2 : varToggleCol7 = 1;
        else if( columnNo == 8)
            copyToggleStatus == 0 ? varToggleCol8 = 1 : copyToggleStatus == 1 ? varToggleCol8 = 2 : varToggleCol8 = 1;

        // 정렬 상태 화면에 그리기
        varToggleCol1 == 1 ? $("#spanSortColumn1").text("▲") : varToggleCol1 == 2 ? $("#spanSortColumn1").text("▼") : $("#spanSortColumn1").text("");
        varToggleCol2 == 1 ? $("#spanSortColumn2").text("▲") : varToggleCol2 == 2 ? $("#spanSortColumn2").text("▼") : $("#spanSortColumn2").text("");
        varToggleCol3 == 1 ? $("#spanSortColumn3").text("▲") : varToggleCol3 == 2 ? $("#spanSortColumn3").text("▼") : $("#spanSortColumn3").text("");
        varToggleCol4 == 1 ? $("#spanSortColumn4").text("▲") : varToggleCol4 == 2 ? $("#spanSortColumn4").text("▼") : $("#spanSortColumn4").text("");
        varToggleCol5 == 1 ? $("#spanSortColumn5").text("▲") : varToggleCol5 == 2 ? $("#spanSortColumn5").text("▼") : $("#spanSortColumn5").text("");
        varToggleCol6 == 1 ? $("#spanSortColumn6").text("▲") : varToggleCol6 == 2 ? $("#spanSortColumn6").text("▼") : $("#spanSortColumn6").text("");
        varToggleCol7 == 1 ? $("#spanSortColumn7").text("▲") : varToggleCol7 == 2 ? $("#spanSortColumn7").text("▼") : $("#spanSortColumn7").text("");
        varToggleCol8 == 1 ? $("#spanSortColumn8").text("▲") : varToggleCol8 == 2 ? $("#spanSortColumn8").text("▼") : $("#spanSortColumn8").text("");
    };

    // 컬럼 상태를 조회(정렬 조건 적용)
    var getColumnStatus = function( columnNo){
        if( columnNo == 1)
            return varToggleCol1;
        else if( columnNo == 2)
            return varToggleCol2;
        else if( columnNo == 3)
            return varToggleCol3;
        else if( columnNo == 4)
            return varToggleCol4;
        else if( columnNo == 5)
            return varToggleCol5;
        else if( columnNo == 6)
            return varToggleCol6;
        else if( columnNo == 7)
            return varToggleCol7;
        else if( columnNo == 8)
            return varToggleCol8;
    };

    // 컬럼의 정렬을 실행
    var sortColumn = function( columnNo, fieldName){
        // 컬럼 상태 설정
        setColumnStatus( columnNo);

        // 오름차순, 내림차순 선택
        var isAscending = true;
        getColumnStatus( columnNo) == 1 ? isAscending = true : getColumnStatus( columnNo) == 2 ? isAscending = false : isAscending = true;

        // 정렬 실행
        self.common.sortArrayObject( varResultLists, fieldName, isAscending);

        // 환자 정보 다시 그리기
        drawPatientList();
    };
    //==== 컬럼 정렬 프로세스 } ====


};