
/* ##############################################################
	설  명 : [Modal]근태관리 - 출/퇴근 등록 팝업
	작성일 : 2023. 01. 19.
	작성자 : NTsoft
	수정일 	        수정자 	 수정내용
	-------         -------- ---------------------------
	2023. 01. 19.	NTsoft 	 최초 생성
*/
let attendanceInsertModal = {
	callback : null,
	reqParam : null,
	dataList : null,
	/* 모달 초기화 작업 */
	init : function () {
		$('#attendanceInsertModal').remove();
		$('body').append( this.HtmlTemplate);
		//그리드 이벤트
		this.setEvent();
	},
	/* 그리드 이벤트 설정 */
	setEvent:function() {
		let modalMemberId = $('#attendanceInsertModal #modalMemberId');
		modalMemberId.on("click", function(){
			let reqParam = {};
			memberSearchModal.popup(function (obj) {
				modalMemberId.data('modalMemberId',obj.selectMemberId);
				modalMemberId.val(obj.selectMemberName);
			}, reqParam);
		});

		$('#attendanceInsertModal .btnCancle').on("click", function(){
			attendanceInsertModal.close();
		});
		
		$('#attendanceInsertModal .btnSave').on("click", function(){
			attendanceInsertModal.save("I");
		});
		
		// 날짜 datepicker
		$("#attendanceInsertModal #modalAttendanceDate").datetimepicker({
			allowInputToggle: true,
			format: 'YYYY-MM-DD',
			defaultDate: moment()
		});
		fn_searchDatePicker("#attendanceInsertModal #modalStartDate", "#attendanceInsertModal #modalEndDate");
	},
	/* 데이터 선택 */
	setSelect:function(selectNumber) {
		let checkedData = dataList[selectNumber];
		if(dataList==null||checkedData==undefined){
			return ;
		}
		
		if( attendanceInsertModal.callback )  {
			attendanceInsertModal.callback(checkedData);
		}
		attendanceInsertModal.close();
	},
	//저장
	save:function(state) {
		let muMemberId     = $("#attendanceInsertModal #modalMemberId").data('modalMemberId');
		let attendanceType = $("#attendanceInsertModal #modalAttendanceType").val();
		let attendanceDate = $("#attendanceInsertModal #modalAttendanceDate").val();
		let startDate      = $("#attendanceInsertModal #modalStartDate").val();
		let endDate        = $("#attendanceInsertModal #modalEndDate").val();
		let attendanceTime = $("#attendanceInsertModal #modalAttendanceInput").val();
		
		if(true != fn_emptyCheck(muMemberId)){
			modalEvent.warning("등록", "직원 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(attendanceType)){
			modalEvent.warning("등록", "근태유형 정보가 없습니다.");	
			return;
		}
		
		if('C'==attendanceType){
			if(true != fn_emptyCheck(startDate)){
				modalEvent.warning("등록", "시작날짜 정보가 없습니다.");	
				return;
			}

			if(true != fn_emptyCheck(endDate)){
				modalEvent.warning("등록", "종료날짜 정보가 없습니다.");	
				return;
			}

			attendanceTime = null;
		}
		else{
			if(true != fn_emptyCheck(attendanceDate)){
				modalEvent.warning("등록", "날짜 정보가 없습니다.");	
				return;
			}

			if(true != fn_emptyCheck(attendanceTime)){
				modalEvent.warning("등록", "시간 정보가 없습니다.");	
				return;
			}

			startDate = attendanceDate;
			endDate   = attendanceDate;
		}
		
		modalEvent.info("등록", "근태관리의 출/퇴근 정보를 등록하시겠습니까?", function(){
			let formData = new FormData();
			formData.append("menuClass", menuClass);
			formData.append("muMemberId", muMemberId);
			formData.append("attendanceType", attendanceType);
			formData.append("startDate", startDate);
			formData.append("endDate", endDate);
			formData.append("attendanceTime", attendanceTime);
	
			$.ajax({
				url: encodeURI('/webattendance/insertAttendance.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( attendanceInsertModal.callback )  {
								attendanceInsertModal.callback();
							}
							attendanceInsertModal.close();
						});
					}
					else{
						modalEvent.danger("등록 오류", data.msgDesc);
					}
				},
				error : function(xhr, status, error) {
					modalEvent.danger("등록 오류", "등록 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
				},
				beforeSend:function(){
					
				},
				complete:function(){
					
				}
			});
		});
	},
	/*
	 * 팝업
	 * callback : 콜백함수, reqParam: 팝업파라미터
	 */
	popup:function(callback, reqParam) {
		// 초기화 후 열기
		attendanceInsertModal.init();
		
		//콜백함수 지정.
		attendanceInsertModal.callback = callback;
		attendanceInsertModal.reqParam = reqParam;
		attendanceInsertModal.dataList = null;
		
		// 기본 셋팅
		let muMemberId     = attendanceInsertModal.reqParam.muMemberId;
		let memberName     = attendanceInsertModal.reqParam.memberName;
		let attendanceDate = attendanceInsertModal.reqParam.attendanceDate;

		$("#attendanceInsertModal #modalMemberId").data('modalMemberId',muMemberId);
		$("#attendanceInsertModal #modalMemberId").val(memberName);
		fn_dateTimePicker1("#modalAttendanceInput", "09:00");

		if(true != fn_emptyCheck(attendanceDate)){
			
		}
		else{
			$("#attendanceInsertModal #modalAttendanceDate").val(attendanceDate);
			$("#attendanceInsertModal #modalStartDate").val(attendanceInsertModal.reqParam.attendanceDate).trigger("change");
			$("#attendanceInsertModal #modalEndDate").val(attendanceInsertModal.reqParam.attendanceDate).trigger("change");
		}
		
		setTimeout(function(){
			$('#attendanceInsertModal').modal('show')
		}, 200);

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

		setTimeout(function(){
			$('#attendanceInsertModal').remove();
		},500);
	},
	HtmlTemplate : `
	<div id="attendanceInsertModal" class="modal registration_wrap_modal fade" 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">
						<div class="input_box">
							<label>직원</label>

							<div class="search_box">
								<img src="/image/web/search_G.svg" alt="search"/>
								<input type="text" id="modalMemberId" placeholder="직원 검색" readonly>
							</div>
						</div>

						<div class="input_box">
							<label>등록</label>

							<div class="select_box dropdown">
								<button type="button" class="label dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">출근</button>
								<input type="hidden" id="modalAttendanceType" value="A">

								<ul class="select_option_list dropdown-menu">
									<li class="option_list_item hide_item" onclick="fn_insertModal_clickSelectValue(this, 'A', '출근');">출근</li>
									<li class="option_list_item hide_item" onclick="fn_insertModal_clickSelectValue(this, 'B', '퇴근');">퇴근</li>
									<li class="option_list_item open_hide" onclick="fn_insertModal_clickSelectValue(this, 'C', '휴무');">휴무</li>
								</ul>
							</div>

							<div class="calendar_box hide_box attendanceInputBox">
								<div class="date_box">
									<img src="/image/web/calendar.svg" alt="calendar"/>
									<input type="date" class="date_picker startDate" id="modalAttendanceDate">
								</div>
							</div>
							
<!--							<div class="select_box hide_box dropdown attendanceInputBox">-->
<!--								<button type="button" class="label dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">00:00</button>-->
							<div class="calendar_box hide_box attendanceTime">
								<div class="date_box">
									<img src="/image/web/Schedule.svg" alt="calendar"/>
									<input class="attendanceTimeInput" type="text" id="modalAttendanceInput">
								</div>
							</div>
<!--								<ul class="select_option_list dropdown-menu" id="modalAttendanceTime"></ul>-->
<!--							</div>-->

							<div class="calendar_box show_box holidayInputBox">
								<div class="date_box">
									<img src="/image/web/calendar.svg" alt="calendar"/>
									<input type="date" class="date_picker" id="modalStartDate">
								</div>

								<span class="slash">-</span>

								<div class="date_box last">
									<img src="/image/web/calendar.svg" alt="calendar"/>
									<input type="date" class="date_picker" id="modalEndDate">
								</div>
							</div>
						</div>
					</div>
				</div>

				<div class="modal-footer">
					<div class="btn_box">
						<button type="button" class="cancel_btn btnCancle">취소</button>
						<button type="button" class="registration_bth btnSave">등록</button>
					</div>
				</div>
			</div>
		</div>
	</div>
    `
};

/****************************************************************************
* select box 선택 값 셋팅
****************************************************************************/
function fn_insertModal_clickSelectValue(target, param, paramText){
	$(target).parent().siblings("button").text(paramText);
	$(target).parent().siblings("input").val(param);

	if('C'==param){
		// 휴무일 경우
		$("#attendanceInsertModal .attendanceInputBox").hide();
		$("#attendanceInsertModal .holidayInputBox").show();
	}
	else{
		// 출/퇴근일 경우
		$("#attendanceInsertModal .attendanceInputBox").show();
		$("#attendanceInsertModal .holidayInputBox").hide();
	}
}
