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

    $.ajax({
        url: encodeURI('/mobilemembership/selectListMembershipCharge.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((chargeData,index)=>{
                            innerHtml+=`<li>
                                            <div>
                                                <p>${chargeData.writeDate}</p>
                                                <p>${chargeData.chargePaymentItemName}</p>
                                            </div>
                                            <div class="right_box">
                                                <p>충전 금액</p>
                                                <p>${chargeData.rechargeAmount}</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_membershipChargeSearch(param){
    sortBy = $(".button_box .on").attr("id");
    fn_selectListMembershipChargeJson();
}

/****************************************************************************
 * 페이지 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_selectListMembershipChargeJson();
        });

    }else if(isMobile.IOS()){
        // IOS
        let value; /*$(this).val() */
        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_selectListMembershipChargeJson();
        });
    }

    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_membershipChargeSearch();
}

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

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

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