
/* ##############################################################
	설  명 : [Modal]멤버십 포인트적립 등록 팝업
	작성일 : 2023. 01. 19.
	작성자 : NTsoft
	수정일 	        수정자 	 수정내용
	-------         -------- ---------------------------
	2023. 01. 19.	NTsoft 	 최초 생성
*/
let membershipPointModal = {
    callback : null,
    reqParam : null,
    dataList : null,
    /* 모달 초기화 작업 */
    init : function () {
        $('#membershipPointModal').remove();
        $('body').append( this.HtmlTemplate);
        //그리드 이벤트
        this.setEvent();
    },
    /* 그리드 이벤트 설정 */
    setEvent:function() {
        $('#membershipPointModal .btnCancle').on("click", function(){
            membershipPointModal.close();
        });

        $('#membershipPointModal .btnSave').on("click", function(){
            membershipPointModal.save("I");
        });
    },
    /* 데이터 선택 */
    setSelect:function(selectNumber) {
        let checkedData = dataList[selectNumber];
        if(dataList==null||checkedData==undefined){
            return ;
        }

        if( membershipPointModal.callback )  {
            membershipPointModal.callback(checkedData);
        }
        membershipPointModal.close();
    },
    //저장
    save:function(state) {
        let membershipYn = userData.membershipYn;
        let chargePoint =  parseInt($('#membershipPointModal #chargePoint').val(),0)

        let membershipBalance = parseInt(userData.membershipBalance.replace(/,/g, ''), 0)+chargePoint
        let etc =  $('#membershipPointModal #etc').val();

        if(true != fn_emptyCheck(muUserId)){
            modalEvent.warning("등록", "고객정보가 없습니다.");
            return;
        }
        if(true != fn_emptyCheck(membershipYn)){
            modalEvent.warning("등록", "멤버십 가입정보가 없습니다.");
            return;
        }
        if(true != fn_emptyCheck(chargePoint)){
            modalEvent.warning("등록", "적립할 멤버십 포인트가 없습니다.");
            return;
        }
        if(true != fn_emptyCheck(membershipBalance)){
            modalEvent.warning("등록", "멤버십 잔액 정보가 없습니다.");
            return;
        }
        modalEvent.info("등록", "멤버십 포인트를 적립하시겠습니까?", function(){
            let formData = new FormData();
            formData.append("menuClass", menuClass);
            formData.append("rechargeAmount", chargePoint);
            formData.append("muUserId", muUserId);
            formData.append("membershipBalance", membershipBalance);
            formData.append("membershipYn", membershipYn);
            formData.append("memo", etc);

            $.ajax({
                url: encodeURI('/webmembership/insertMembershipPoint.do'),
                data: formData,
                dataType: "json",
                processData: false,
                contentType: false,
                type: 'POST',
                async: true,
                success: function(data){
                    if('0'==data.msgCode){
                        modalEvent.success("등록 성공", data.msgDesc, function(){
                            if( membershipPointModal.callback )  {
                                membershipPointModal.callback();
                            }
                            membershipPointModal.close();
                        });
                    }
                    else{
                        modalEvent.danger("등록 오류", data.msgDesc);
                    }
                },
                error : function(xhr, status, error) {
                    modalEvent.danger("등록 오류", "등록 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
                },
                beforeSend:function(){

                },
                complete:function(){

                }
            });
        });
    },
    /*
     * 팝업
     * callback : 콜백함수, reqParam: 팝업파라미터
     */
    popup:function(callback, reqParam) {
        // 초기화 후 열기
        membershipPointModal.init();

        //콜백함수 지정.
        membershipPointModal.callback = callback;
        membershipPointModal.reqParam = reqParam;
        membershipPointModal.dataList = null;

        setTimeout(function(){
            $('#membershipPointModal').modal('show')
        }, 200);
    },
    /*
     * 팝업 닫기
     */
    close:function() {
        $('#membershipPointModal').modal('hide');

		setTimeout(function(){
			$('#membershipPointModal').remove();
		},500);
    },
    HtmlTemplate : `
	<div class="modal save_modal fade" id="membershipPointModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<p>멤버십 포인트 적립금액</p>
					</div>
					<div class="modal-body">
						<div class="input_box">
							<input type="text" id="chargePoint" placeholder="적립할 멤버십 금액을 입력하세요">
						</div>
						<div class="textarea_box">
							<label>메모</label>
							<textarea id="etc"></textarea>
						</div>
					</div>
					<div class="modal-footer">
						<div class="button_box">
							<button class="cancel_btn btnCancle">취소</button>
							<button class="check_btn btnSave">확인</button>
						</div>
					</div>							
				</div>
			</div>
		</div>
    `
};

