
/* ##############################################################
	설  명 : [Modal]출/퇴 수정 팝업
	작성일 : 2023. 01. 19.
	작성자 : NTsoft
	수정일 	        수정자 	 수정내용
	-------         -------- ---------------------------
	2023. 01. 19.	NTsoft 	 최초 생성
*/
let attendanceUpdateModal = {
	callback : null,
	reqParam : null,
	dataList : null,
	/* 모달 초기화 작업 */
	init : function () {
		$('#attendanceUpdateModal').remove();
		$('body').append( this.HtmlTemplate);
		//그리드 이벤트
		this.setEvent();
	},
	/* 그리드 이벤트 설정 */
	setEvent:function() {
		$('#attendanceUpdateModal .btnCancle').on("click", function(){
			attendanceUpdateModal.close();
		});
		
		$('#attendanceUpdateModal .btnSave').on("click", function(){
			attendanceUpdateModal.save("I");
		});
	},
	/* 데이터 선택 */
	setSelect:function(selectNumber) {
		let checkedData = dataList[selectNumber];
		if(dataList==null||checkedData==undefined){
			return ;
		}
		
		if( attendanceUpdateModal.callback )  {
			attendanceUpdateModal.callback(checkedData);
		}
		attendanceUpdateModal.close();
	},
	// 상세 조회
	search:function(state) {
		let muAttendanceId  = attendanceUpdateModal.reqParam.muAttendanceId;
		let muAttendanceId2 = attendanceUpdateModal.reqParam.muAttendanceId2;
		let muMemberId      = attendanceUpdateModal.reqParam.muMemberId;
		let attendanceDate  = attendanceUpdateModal.reqParam.attendanceDate;
		
		if(true != fn_emptyCheck(muAttendanceId) && true != fn_emptyCheck(muAttendanceId2)){
			modalEvent.warning("수정", "근태 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(muMemberId)){
			modalEvent.warning("수정", "직원 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(attendanceDate)){
			modalEvent.warning("수정", "근태날짜 정보가 없습니다.");	
			return;
		}
		
		let formData = new FormData();
		formData.append("menuClass", menuClass);
		formData.append("muAttendanceId", muAttendanceId);
		formData.append("muAttendanceId2", muAttendanceId2);

		$.ajax({
			url: encodeURI('/webattendance/selectAttendanceCommute.do'),
			data: formData,
			dataType: "json",
			processData: false,
			contentType: false,
			type: 'POST',
			async: true,
			success: function(data){
				if('0'==data.msgCode){
					if(0<data.rows.length){
						$("#attendanceUpdateModal #modalMemberNameId").text(data.rows[0].memberName+'('+data.rows[0].loginId+')');
						$("#attendanceUpdateModal #modalAttendanceDate").text(attendanceDate);

						// $("#attendanceUpdateModal #modalStartTime").siblings("button").text(data.rows[0].startTime);
						$("#attendanceUpdateModal #modalStartTime").val(data.rows[0].startTime||"00:00");
						// $("#attendanceUpdateModal #modalEndTime").siblings("button").text(data.rows[0].endTime);
						$("#attendanceUpdateModal #modalEndTime").val(data.rows[0].endTime||"00:00");
					}
				}
				else{
					modalEvent.danger("조회 오류", data.msgDesc);
				}
			},
			error : function(xhr, status, error) {
				modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
			},
			beforeSend:function(){
				
			},
			complete:function(){
				
			}
		});
	},
	//저장
	save:function(state) {
		let muAttendanceId  = attendanceUpdateModal.reqParam.muAttendanceId;
		let muAttendanceId2 = attendanceUpdateModal.reqParam.muAttendanceId2;
		let muMemberId      = attendanceUpdateModal.reqParam.muMemberId;
		let attendanceDate  = attendanceUpdateModal.reqParam.attendanceDate;
		let startTime       = $("#attendanceUpdateModal #modalStartTime").val();
		let endTime         = $("#attendanceUpdateModal #modalEndTime").val();
		
		if(true != fn_emptyCheck(muAttendanceId) && true != fn_emptyCheck(muAttendanceId2)){
			modalEvent.warning("수정", "근태 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(muMemberId)){
			modalEvent.warning("수정", "직원 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(attendanceDate)){
			modalEvent.warning("수정", "날짜 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(startTime)){
			modalEvent.warning("수정", "출근 시간 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(endTime)){
			modalEvent.warning("수정", "퇴근 시간 정보가 없습니다.");	
			return;
		}
		if(startTime>=endTime){
			modalEvent.warning("수정", "출/퇴근기록을 다시 확인해주세요");
			return;
		}
		
		modalEvent.info("수정", "출/퇴근 정보를 수정하시겠습니까?", function(){
			let formData = new FormData();
			formData.append("menuClass", menuClass);
			formData.append("muAttendanceId", muAttendanceId);
			formData.append("muAttendanceId2", muAttendanceId2);
			formData.append("muMemberId", muMemberId);
			formData.append("attendanceDate", attendanceDate);
			formData.append("startTime", startTime);
			formData.append("endTime", endTime);
	
			$.ajax({
				url: encodeURI('/webattendance/updateAttendanceCommute.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( attendanceUpdateModal.callback )  {
								attendanceUpdateModal.callback();
							}
							attendanceUpdateModal.close();
						});
					}
					else{
						modalEvent.danger("수정 오류", data.msgDesc);
					}
				},
				error : function(xhr, status, error) {
					modalEvent.danger("수정 오류", "등록 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
				},
				beforeSend:function(){
					
				},
				complete:function(){
					
				}
			});
		});
	},
	/*
	 * 팝업
	 * callback : 콜백함수, reqParam: 팝업파라미터
	 */
	popup:function(callback, reqParam) {
		// 초기화 후 열기
		attendanceUpdateModal.init();
		
		//콜백함수 지정.
		attendanceUpdateModal.callback = callback;
		attendanceUpdateModal.reqParam = reqParam;
		attendanceUpdateModal.dataList = null;

		fn_dateTimePicker1("#modalStartTime", "00:00");
		fn_dateTimePicker1("#2", "00:00");
		
		setTimeout(function(){
			$('#attendanceUpdateModal').modal('show')
		}, 200);
		
		attendanceUpdateModal.search();
	},
	/*
	 * 팝업 닫기
	 */
	close:function() {
		$('#attendanceUpdateModal').modal('hide');

		setTimeout(function(){
			$('#attendanceUpdateModal').remove();
		},500);
	},
	HtmlTemplate : `
	<div id="attendanceUpdateModal" class="modal cencell_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">
						<ul class="info_list">
							<li>
								<label>직원</label>
								<p id="modalMemberNameId"></p>
							</li>
							<li>
								<label>기준일</label>
								<p id="modalAttendanceDate"></p>
							</li>
							<li>
								<div class="input_box">
									<label>출/퇴근기록</label>
									
									<div class="select_time_list_box updateAttendanceTime">
										<div class="calendar_box first">
											<div class="date_box">
												<img src="/image/web/Schedule.svg" alt="calendar"/>
												<input class="updateAttendanceStartTime" type="text" id="modalStartTime">
											</div>
										</div>
											
										<span class="slash">~</span>
									
										<div class="calendar_box">
											<div class="date_box">
												<img src="/image/web/Schedule.svg" alt="calendar"/>
												<input class="updateAttendanceEndTime" type="text" id="modalEndTime">
											</div>
										</div>
									</div>
								</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="seve_bth btnSave">수정</button>
					</div>
				</div>
			</div>
		</div>
	</div>
    `
};