/****************************************************************************
 * 멤버십사용 내역 조회
 ****************************************************************************/
function fn_selectListMembershipUseJson(){
    let formData = new FormData();
    formData.append("membershipSearchDate",membershipSearchDate);
    formData.append("sortBy",sortBy);

    $.ajax({
        url: encodeURI('/mobilemembership/selectListMembershipUse.do'),
        data: formData,
        dataType: "json",
        processData: false,
        contentType: false,
        type: 'POST',
        async: false,
        success: function(data){
            if('0'==data.msgCode){
                const ListWrapper = $('.list_btm');
                ListWrapper.empty();
                if(0<data.totalCount){
                    let innerHtml = '<ul>';
                    data.rows.forEach((useData,index)=>{
                        innerHtml+=`<li>
                                            <div>
                                                <p>${useData.writeDate}</p>
                                                <p>
                                                    <span>${useData.treatmentName}</span>
                                                    <span>${useData.procedureName}</span>
                                                </p>
                                            </div>
                                            <div class="right_box">
                                                <p>사용 금액</p>
                                                <p>${useData.usedAmount}</p>
                                            </div>
                                        </li>`
                    })
                    innerHtml += '</ul>';
                    ListWrapper.append(innerHtml)
                } else {
                    const innerHtml = '<ul><li style="padding: 15px 20px; margin-top: 10px;">멤버십 사용내역이 없습니다.</li>';
                    ListWrapper.append(innerHtml);
                }
            } else if (data.msgCode == '-1') {
                modalEvent.warning("조회 오류", data.msgDesc);
            } else {
                modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
            }
        },
        error: function(xhr, status, error) {
            modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
        },
        beforeSend:function(){

        },
        complete:function(){

        }
    });
}

/****************************************************************************
 * 검색하기
 ****************************************************************************/
function fn_membershipUseSearch(param){
    sortBy = $(".button_box .on").attr("id");
    fn_selectListMembershipUseJson();
}

/****************************************************************************
 * 페이지 init
 ****************************************************************************/
function fn_pageInit(){

    if(isMobile.Android()){
        // 안드로이드
        let value;
        let year;
        let month;

        $("#mmDatepicker").click(function(e){
            e.preventDefault();
            value = prompt("monthPicker");

            if(null === value || !value){
                // 오늘
                let toDate = new Date();
                year  = toDate.getFullYear();
                month = toDate.getMonth() + 1;
                if(month < 10) month = "0" + month;

            }else{
                let valueArr = value.split("-");
                year  = valueArr[0];
                month = valueArr[1];
                if(month < 10) month = "0" + month;
            }

            membershipSearchDate = year+"-"+month;
            $(this).val(membershipSearchDate);

            fn_selectListMembershipUseJson();
        });

    }else if(isMobile.IOS()){
        // IOS
        let value; /**/
        let year;
        let month;

        $("#mmDatepicker").change(function(){
            value = $(this).val();
            if(!value){
                // 오늘
                let toDate = new Date();
                year  = toDate.getFullYear();
                month = toDate.getMonth() + 1;
                if(month < 10) month = "0" + month;
            }else{
                let valueArr = $(this).val().split("-");
                year  = valueArr[0];
                month = valueArr[1];
                if(month < 10) month = "0" + month;
            }

            membershipSearchDate = year+"-"+month;
            $(this).val(membershipSearchDate);

            fn_selectListMembershipUseJson();
        });
    }

    if(membershipSearchDate){
        $("#mmDatepicker").val(membershipSearchDate)
    }else{
        // 오늘
        let toDate = new Date();
        let year  = toDate.getFullYear();
        let month = toDate.getMonth() + 1;
        if(month < 10) month = "0" + month;
        $("#mmDatepicker").val( year+'-'+month).trigger("change");

        membershipSearchDate = year+'-'+month;
    }

    //멤버십 충전내역 조회
    fn_membershipUseSearch();
}

/****************************************************************************
 * 페이지 event
 ****************************************************************************/
function fn_pageEvent(){
    $(".button_box button").on('click', function(){
        $(".button_box .on").removeClass('on');
        $(this).addClass('on');
        fn_membershipUseSearch();
    });
}

$(function(){
    // 페이지 init
    fn_pageInit();

    // 페이지 event
    fn_pageEvent();
});