
/* ##############################################################
	설  명 : [Modal]근태관리 - 연차 상세/조회 팝업
	작성일 : 2023. 01. 19.
	작성자 : NTsoft
	조회일 	        조회자 	 조회내용
	-------         -------- ---------------------------
	2023. 01. 19.	NTsoft 	 최초 생성
*/
let attendanceVacationManagerSelectModal = {
	callback : null,
	reqParam : null,
	dataList : null,
	/* 모달 초기화 작업 */
	init : function () {
		$('#attendanceVacationManagerSelectModal').remove();
		$('body').append( this.HtmlTemplate);
		//그리드 이벤트
		this.setEvent();
	},
	/* 그리드 이벤트 설정 */
	setEvent:function() {
		$('#attendanceVacationManagerSelectModal .btnCancle').on("click", function(){
			attendanceVacationManagerSelectModal.close();
		});

		// 날짜 datepicker
		fn_selectModal_searchAttendanceVacationManagerDatePicker("#attendanceVacationManagerSelectModal #modalStartDate", "#attendanceVacationManagerSelectModal #modalEndDate");
	},
	/* 데이터 선택 */
	setSelect:function(selectNumber) {
		let checkedData = dataList[selectNumber];
		if(dataList==null||checkedData==undefined){
			return ;
		}
		
		if( attendanceVacationManagerSelectModal.callback )  {
			attendanceVacationManagerSelectModal.callback(checkedData);
		}
		attendanceVacationManagerSelectModal.close();
	},
	// 상세 조회
	search:function(state) {
		let muAttendanceId  = attendanceVacationManagerSelectModal.reqParam.muAttendanceId;
		let muMemberId      = attendanceVacationManagerSelectModal.reqParam.muMemberId;
		
		if(true != fn_emptyCheck(muAttendanceId) && true != fn_emptyCheck(muAttendanceId2)){
			modalEvent.warning("조회", "근태 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(muMemberId)){
			modalEvent.warning("조회", "직원 정보가 없습니다.");	
			return;
		}
		
		let formData = new FormData();
		formData.append("menuClass", menuClass);
		formData.append("muAttendanceId", muAttendanceId);
		formData.append("muMemberId", muMemberId);

		$.ajax({
			url: encodeURI('/webattendance/selectAttendanceVacation.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) {
						$("#attendanceVacationManagerSelectModal #modalMemberId").val(data.rows[0].muMemberId);
						$("#attendanceVacationManagerSelectModal #modalMemberNameId").text(data.rows[0].memberName + '(' + data.rows[0].loginId + ')');


						$('#attendanceVacationManagerSelectModal #li_' + data.rows[0].muVacationId).click();

						$("#attendanceVacationManagerSelectModal #modalStartDate").val(data.rows[0].startDate).trigger("change");
						$("#attendanceVacationManagerSelectModal #modalEndDate").val(data.rows[0].endDate).trigger("change");

						if ("D" != data.rows[0].attendanceType) {
							$("#attendanceVacationManagerSelectModal #modalStartTime").siblings("button").text(data.rows[0].startTime);
							$("#attendanceVacationManagerSelectModal #modalStartTime").val(data.rows[0].startTime);
							$("#attendanceVacationManagerSelectModal #modalEndTime").siblings("button").text(data.rows[0].endTime);
							$("#attendanceVacationManagerSelectModal #modalEndTime").val(data.rows[0].endTime);
						}

						$("#attendanceVacationManagerSelectModal #modalDays").text(data.rows[0].days);
						$("#attendanceVacationManagerSelectModal #modalEtc").val(data.rows[0].etc);
						$("#attendanceVacationManagerSelectModal #modalCancleEtc").val(data.rows[0].cancelEtc);
					}
				}
				else{
					modalEvent.danger("조회 오류", data.msgDesc);
				}
			},
			error : function(xhr, status, error) {
				modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
			},
			beforeSend:function(){
				
			},
			complete:function(){
				
			}
		});
	},
	//저장

	/*
	 * 팝업
	 * callback : 콜백함수, reqParam: 팝업파라미터
	 */
	popup:function(callback, reqParam) {
		// 초기화 후 열기
		attendanceVacationManagerSelectModal.init();
		
		//콜백함수 지정.
		attendanceVacationManagerSelectModal.callback = callback;
		attendanceVacationManagerSelectModal.reqParam = reqParam;
		attendanceVacationManagerSelectModal.dataList = null;
		
		
		// 연차유형 콤보데이타 설정 조회
		fn_selectModal_setVacationCombo();
		
		// 기본 셋팅
		fn_setTimeValue("selectVacationStartTime", "00:00");
		fn_setTimeValue("selectVacationEndTime", "00:00");
		
		setTimeout(function(){
			$('#attendanceVacationManagerSelectModal').modal('show')
		}, 200);
		
		attendanceVacationManagerSelectModal.search();
	},
	/*
	 * 팝업 닫기
	 */
	close:function() {
		$('#attendanceVacationManagerSelectModal').modal('hide');

		setTimeout(function(){
			$('#attendanceVacationManagerSelectModal').remove();
		},500);
	},
	HtmlTemplate : `
	<div id="attendanceVacationManagerSelectModal" class="modal hyuga_info_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>
								<input type="hidden" id="modalMemberId">
							</li>
							<li>
								<div class="input_box three_wrap">
									<label>연차 사용단위</label>
		
									<div class="select_box dropdown">
										<button class="label" class="label dropdown-toggle" aria-haspopup="true" aria-expanded="false">연차 사용단위</button>
										<input type="hidden" id="modalVacationId"></input>
										<ul class="select_option_list dropdown-menu"></ul>
									</div>
		
									<div class="calendar_box">
										<div class="date_box">
											<img src="/image/web/calendar.svg" alt="calendar"/>
											<input type="date" class="date_picker startDate" id="modalStartDate" readonly>
										</div>
		
										<span class="slash">-</span>
		
										<div class="date_box last">
											<img src="/image/web/calendar.svg" alt="calendar"/>
											<input type="date" class="date_picker endDate" id="modalEndDate" readonly>
										</div>
									</div>
								</div>
							</li>
							<li class="toggle_box">
								<div class="select_time_list_box">
									<div class="select_list first">
										<div class="select_box dropdown">
											<button class="label" class="label dropdown-toggle" aria-haspopup="true" aria-expanded="false">00:00</button>
											<input type="hidden" id="modalStartTime">
											
											<ul class="select_option_list dropdown-menu" id="selectVacationStartTime"></ul>
										</div><!--select_box-->
									</div>
	
									<span class="slash">~</span>
										
									<div class="select_list">
										<div class="select_box">
											<button class="label" class="label dropdown-toggle" aria-haspopup="true" aria-expanded="false">00:00</button>
											<input type="hidden" id="modalEndTime">
											
											<ul class="select_option_list dropdown-menu" id="selectVacationEndTime"></ul>
										</div><!--select_box-->
									</div>
								</div>
							</li>
							<li class="hide_box">
								<label>사용 일 수</label>
								<p id="modalDays">0</p>
							</li>
							<li>
								<div class="input_box">
									<label>메모</label>
	
									<div class="textarea_box">
										<textarea id="modalEtc" placeholder="" readonly></textarea>
									</div>
								</div>
							</li>
							<li>
								<div class="input_box">
									<label>취소사유</label>
	
									<div class="textarea_box">
										<textarea id="modalCancleEtc" placeholder="" readonly></textarea>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>

				<div class="modal-footer">
					<div class="btn_box">
						<button type="button" class="check_bth btnCancle">확인</button>
					</div>
				</div>
			</div>
		</div>
	</div>
    `
};

/****************************************************************************
* 연차유형 콤보데이타 설정
****************************************************************************/
function fn_selectModal_setVacationCombo(){
	// 대상
	const target = $("#attendanceVacationManagerSelectModal #modalVacationId").siblings("ul.select_option_list");

	// 데이터 조회
	let comboData = fn_selectListVacationOption();
	
	// 조회 데이터 셋팅
	let comboHTML = '';
	for(let i = 0; i < comboData.length; i++){
		let muVacationId     = comboData[i].muVacationId;
		let vacationName     = comboData[i].vacationName;
		let hourType         = comboData[i].hourType;
		
		comboHTML += '<li id="li_'+muVacationId+'" class="option_list_item">';
		comboHTML += '	<input type="hidden" name="MODAL_HOUR_TYPE" value="'+hourType+'">';
		comboHTML += '	<span>'+vacationName+'</span>';
		comboHTML += '</li>';
	}
	target.empty().html(comboHTML).trigger("create");

	// 콤보 이벤트
	target.find("li.option_list_item").click(function () {
		// 1. 선택값 셋팅
		let selectIdArr = $(this).prop("id").split('_');
		let selectId    = selectIdArr[1];
		let selectName  = $(this).text();
		
		$(this).parent().siblings("button").text(selectName);
		$(this).parent().siblings("input").val(selectId);
		
		let hourType = $(this).find("input[name='MODAL_HOUR_TYPE']").val();
		if('A'==hourType){
			// 연차일경우
			$("#attendanceVacationManagerSelectModal .toggle_box").hide();
		}
		else{
			// 반차일경우
			$("#attendanceVacationManagerSelectModal .toggle_box").show();
		}
	});
}

/****************************************************************************
* datePicker 
****************************************************************************/
function fn_selectModal_searchAttendanceVacationManagerDatePicker(startId, endId){
	$(startId).datetimepicker({
		allowInputToggle: true,
		format: 'YYYY-MM-DD'
	});
	
	$(endId).datetimepicker({
		allowInputToggle: true,
		useCurrent: false, //Important! See issue #1075
		format: 'YYYY-MM-DD'
	});
	
	/*$(endId).data("DateTimePicker").minDate(fn_getToDate());*/
	
	//$(endId).data("DateTimePicker").maxDate(fn_getToDate());
	//$(startId).data("DateTimePicker").maxDate(fn_getToDate());
	
	$(startId).on("dp.change", function (e) {
		$(endId).data("DateTimePicker").minDate(e.date);
		
		if($(endId).data("DateTimePicker").date() < e.date){
			$(endId).data("DateTimePicker").date(e.date);   
		}
		
		fn_selectModal_getAttendanceVacationManagerDateDiff(startId);
	});
	
	
	$(endId).on("dp.change", function (e) {
		fn_selectModal_getAttendanceVacationManagerDateDiff(startId);
	});
}

/****************************************************************************
* datePicker 일수구하기
****************************************************************************/
function fn_selectModal_getAttendanceVacationManagerDateDiff(target){
	const startVal = $(target).closest(".calendar_box").find(".startDate").val();
	const endVal   = $(target).closest(".calendar_box").find(".endDate").val();
	const date1 = new Date(startVal);
	const date2 = new Date(endVal);
	
	const diffDate = date1.getTime() - date2.getTime();
	
	const days = Math.abs(diffDate / (1000 * 60 * 60 * 24)); // 밀리세컨 * 초 * 분 * 시 = 일
	
	$("#attendanceVacationManagerSelectModal #modalDays").text(days+1);
}

