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

        $('#membershipPayCheckModal .btnSave').on("click", function(){
            membershipPayCheckModal.save("I");
        });

    },
    // 미 수납건 조회
    search:function(state) {
        let data = membershipPayCheckModal.reqParam;
        // 진료명과 할인금액 합계 계산
        let itemNames = data.map(item => item.item).join('<br>');
        let totalDiscountAmount = data.reduce((sum, item) => sum + item.discountAmount, 0);
        let balance = data.finalMembershipBalance;
        // HTML 요소 업데이트
        $('#membershipPayCheckModal .modal-content .modal-header p:nth-child(2)').html(itemNames);
        $('#membershipPayCheckModal .modal-content .modal-body p:nth-child(2)').text(totalDiscountAmount.toLocaleString() + '원');
        $('#membershipPayCheckModal .modal-content .modal-body p:nth-child(4)').text(balance.toLocaleString() + '원');
    },
    save:function(){
        if( membershipPayCheckModal.callback )  {
            membershipPayCheckModal.callback();
        }
        membershipPayCheckModal.close();
    },
    /*
     * 팝업
     * callback : 콜백함수, reqParam: 팝업파라미터
     */
    popup:function(callback, reqParam) {
        // 초기화 후 열기
        membershipPayCheckModal.init();

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

        // 진료 콤보데이타 설정
        // membershipPayCheckModal.setTreatmentCombo();

        membershipPayCheckModal.search();

        // fn_searchDatePicker("#membershipPayCheckModal #payment_detail input[name='modalStartDate']", "#membershipPayCheckModal #payment_detail input[name='modalEndDate']");

        setTimeout(function(){
            $('#membershipPayCheckModal').modal({keyboard:false, backdrop:'static'});
        }, 200);
    },
    /*
     * 팝업 닫기
     */
    close:function() {
        $('#membershipPayCheckModal').modal('hide');

		setTimeout(function(){
			$('#membershipPayCheckModal').remove();
		},500);
    },
    HtmlTemplate : `
	<div id="membershipPayCheckModal" class="modal membership_pay_modal fade tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="">
		<div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <div>
                        <p>진료명</p>
                        <p>피부,이벤트)루비제이저</p>
                    </div>
                </div>
                <div class="modal-body">
                    <p>멤버십 사용금액</p>
                    <p>200,000원</p>
                    <p>사용 후 잔액</p>
                    <p>0원</p>
                    <p class="subP">위 내용으로 해당 진료항목에<br/>멤버십 금액을 사용 하시겠습니까?</p>
                </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>
    `
};