/* ##############################################################
	설  명 : [Modal]고객 신체기록 조회 팝업
	작성일 : 2024. 03. 14.
	작성자 : NTsoft
	수정일 	        수정자 	 수정내용
	-------         -------- ---------------------------
	2024. 03. 14.	NTsoft 	 최초 생성
*/
let	userPhysicalSelctModal = {
	callback : null,
	reqParam : null,
	dataList : null,
	/* 모달 초기화 작업 */
	init : function () {
		$('#userPhysicalSelctModal').remove();
		$('body').append(this.HtmlTemplate);
		//그리드 이벤트
		this.setEvent();
	},
	/* 그리드 이벤트 설정 */
	setEvent:function() {
		$('#userPhysicalSelctModal .btnCancle').on("click", function(){
			userPhysicalSelctModal.close();
		});
		
		$('#userPhysicalSelctModal .btnSave').on("click", function(){
			userPhysicalSelctModal.save();
		});
		
		
		
		// 담당자 수정하기 이벤트
		$('#userPhysicalSelctModal .manager_btn').on("click", function(){
			$('#userPhysicalSelctModal .read_box').hide();
			$('#userPhysicalSelctModal .search_box.member_box').show();
		});
		
		// 담당자 수정 취소하기 이벤트
		$('#userPhysicalSelctModal .cancel_btn').on("click", function(){
			$('#userPhysicalSelctModal .read_box').show();
			$('#userPhysicalSelctModal .search_box.member_box').hide();
		});
		
		// 담당자 변경 이벤트
		let modalMemberTarget = $('#userPhysicalSelctModal input[name="modalMemberName"]');
		modalMemberTarget.on("click", function(){
			let reqParam = {};
			memberSearchModal.popup(function (obj) {
				modalMemberTarget.val(obj.selectMemberName);
				modalMemberTarget.closest('.search_box').siblings('input[name="modalMemberId"]').val(obj.selectMemberId);
			}, reqParam);
		});
	},
	/* 데이터 선택 */
	setSelect:function(selectNumber) {
		let checkedData = dataList[selectNumber];
		if(dataList==null||checkedData==undefined){
			return ;
		}
		
		if( userPhysicalSelctModal.callback )  {
			userPhysicalSelctModal.callback(checkedData);
		}
		userPhysicalSelctModal.close();
	},
	// 상세 조회
	search:function(state) {
		let muUserPhysicalId = userPhysicalSelctModal.reqParam.muUserPhysicalId;
		
		if(true != fn_emptyCheck(muUserPhysicalId)){
			modalEvent.warning("조회", "고객 신체기록 정보가 없습니다.");	
			return;
		}
		
		let formData = new FormData();
		formData.append("menuClass", menuClass);
		formData.append("muUserPhysicalId", muUserPhysicalId);

		$.ajax({
			url: encodeURI('/webuser/selectUserPhysical.do'),
			data: formData,
			dataType: "json",
			processData: false,
			contentType: false,
			type: 'POST',
			async: true,
			success: function(data){
				if('0'==data.msgCode){
					$('#userPhysicalSelctModal #writeDate').text(data.rows[0].writeDate);
					$('#userPhysicalSelctModal #userName').text(data.rows[0].userName);
					$('#userPhysicalSelctModal #nationality').text(data.rows[0].nationality);
					$('#userPhysicalSelctModal #birth').text(data.rows[0].birthday);
					$('#userPhysicalSelctModal #phoneNumber').text(data.rows[0].phoneNumber);
					
					
					$('#userPhysicalSelctModal #height').val(data.rows[0].height);
					$('#userPhysicalSelctModal #weight').val(data.rows[0].weight);
					$('#userPhysicalSelctModal #muscleMass').val(data.rows[0].muscle);
					$('#userPhysicalSelctModal #fatMass').val(data.rows[0].fatMass);
					$('#userPhysicalSelctModal #fatPercent').val(data.rows[0].fatPercent);
					$('#userPhysicalSelctModal #bmi').val(data.rows[0].bmi);
					$('#userPhysicalSelctModal input[name="modalMemberId"]').val(data.rows[0].muMemberId);
					$('#userPhysicalSelctModal input[name="modalMemberName"]').val(data.rows[0].memberName);
					$('#userPhysicalSelctModal #memo').val(data.rows[0].memo);
				}
				else{
					modalEvent.danger("조회 오류", data.msgDesc);
				}
			},
			error : function(xhr, status, error) {
				modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
			},
			beforeSend:function(){
				
			},
			complete:function(){
				
			}
		});
	},
	//저장 (담당자 저장)
	save:function() {
		let muUserPhysicalId = userPhysicalSelctModal.reqParam.muUserPhysicalId;
		let muMemberId = $('#userPhysicalSelctModal input[name="modalMemberId"]').val();
		
		if(true != fn_emptyCheck(muUserPhysicalId)){
			modalEvent.warning("수정", "고객 신체기록 정보가 없습니다.");	
			return;
		}
		
		modalEvent.info("수정", "신체기록 정보를 저장하시겠습니까?", function() {
			let formData = new FormData();
			formData.append("muUserId", muUserId);
			formData.append("menuClass", menuClass);
			formData.append("muUserPhysicalId", muUserPhysicalId);
			formData.append("muMemberId", muMemberId);
			
			$.ajax({
				url: encodeURI('/webuser/updateUserPhysical.do'),
				data: formData,
				dataType: "json",
				processData: false,
				contentType: false,
				type: 'POST',
				async:true,
				success: function (data) {
					if (data.msgCode == '0') {
						modalEvent.success("성공", data.msgDesc, function () {
							if( userPhysicalSelctModal.callback )  {
								userPhysicalSelctModal.callback();
							}
							userPhysicalSelctModal.close();
						});
					
					} else {
						modalEvent.danger("오류", data.msgDesc);
					}
				},
				error: function (xhr, status, error) {
				    modalEvent.danger("등록 오류", "신체정보 등록 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
				},
				beforeSend: function () {
				},
				complete: function () {
				}
			});
		})
	},
    /*
     * 팝업
     * callback : 콜백함수, reqParam: 팝업파라미터
     */
    popup:function(callback, reqParam) {
        // 초기화 후 열기
        userPhysicalSelctModal.init();

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

        setTimeout(function(){
            $('#userPhysicalSelctModal').modal({keyboard:false, backdrop:'static'});
        }, 200);
		
		userPhysicalSelctModal.search();

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

		setTimeout(function(){
			$('#userPhysicalSelctModal').remove();
		},500);
    },
    HtmlTemplate :`
        <div class="modal body_info_modal fade" id="userPhysicalSelctModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <p class="title">신체기록 상세</p>
                    </div>

                    <div class="modal-body">
                        <div class="form_box">
                            <ul>
                                <li class="first">
                                    <p>성함</p>
                                    <p id="userName">-</p>
                                </li>
                                <li>
                                    <p>국적</p>
                                    <p id="nationality">-</p>
                                </li>
                                <li>
                                    <p>생년월일</p>
                                    <p id="birth">-</p>
                                </li>
                                <li>
                                    <p>연락처</p>
                                    <p id="phoneNumber">-</p>
                                </li>
                            </ul>
                            <ul>
                                <li>
                                    <p>날짜</p>
                                    <p id="writeDate">-</p>
                                </li>
                                <li>
                                    <div class="input_box">
                                        <label>키</label>
                                        <input id="height" type="text" placeholder="0" disabled/>
                                        <p>cm</p>
                                    </div>
                                </li>
                                <li>
                                    <div class="input_box">
                                        <label>몸무게</label>
                                        <input id="weight" type="text" placeholder="0" disabled/>
                                        <p>kg</p>
                                    </div>
                                </li>
                                <li>
                                    <div class="input_box">
                                        <label>골격근량</label>
                                        <input id="muscleMass" type="text" placeholder="0" disabled/>
                                        <p>kg</p>
                                    </div>
                                </li>
                                <li>
                                    <div class="input_box">
                                        <label>체지방량</label>
                                        <input id="fatMass" type="text" placeholder="0" disabled/>
                                        <p>kg</p>
                                    </div>
                                </li>
                                <li>
                                    <div class="input_box">
                                        <label>체지방률</label>
                                        <input id="fatPercent" type="text" placeholder="0" disabled/>
                                        <p>%</p>
                                    </div>
                                </li>
                                <li>
                                    <div class="input_box">
                                        <label>BMI</label>
                                        <input id="bmi" type="text" placeholder="0" disabled />
                                        <p>kg/m2</p>
                                    </div>
                                </li>
                            </ul>
                            <ul>
                                <li>
                                    <div class="input_box">
                                        <label>담당자</label>
                                        <input type="hidden" name="modalMemberId"/>
                                        
                                        <div class="search_box member_box" style="display:none;">
                                            <img src="/image/web/search_B.svg" alt="search"/>
                                            <input type="text" name="modalMemberName" readonly style="padding-left: 40px;"/>
                                            <button class="cancel_btn" style="position: relative; font-size: 14px; font-weight: 700; color: rgb(57, 133, 234); padding: 0px 10px; background: none;">취소</button>
                                        </div>
                                        
                                        <div class="read_box">
                                            <input type="text" name="modalMemberName" placeholder="" disabled>
                                            <button class="manager_btn"><img src="/image/web/menu_6_off.svg" alt="수정하기"></button>
                                        </div>
                                    </div>
                                </li>
                                <li>
                                    <div class="textarea_box">
                                        <label>메모</label>
                                        <textarea id="memo" disabled></textarea>
                                    </div>
                                </li>
                            </ul>
                        </div>
                    </div>

                    <div class="modal-footer">
                        <div class="btn_box">
                            <button type="button" class="cancel_btn btnCancle">취소</button>
                            <button type="button" class="save_bth btnSave">저장</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>`
};
