
/* ##############################################################
	설  명 : [Modal]연차관리 수정 팝업
	작성일 : 2023. 01. 19.
	작성자 : NTsoft
	수정일 	        수정자 	 수정내용
	-------         -------- ---------------------------
	2023. 01. 19.	NTsoft 	 최초 생성
*/
let day;
let date 	  = new Date();
let today  = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);
let vacationManagementUpdateModal = {
	callback : null,
	reqParam : null,
	dataList : null,
	/* 모달 초기화 작업 */
	init : function () {
		$('#vacationManagementUpdateModal').remove();
		$('body').append( this.HtmlTemplate);
		//그리드 이벤트
		this.setEvent();
	},
	/* 그리드 이벤트 설정 */
	setEvent:function() {
		fn_searchDatePicker("#vacationManagementUpdateModal #modalStartDate", "#vacationManagementUpdateModal #modalEndDate");
		$("#vacationManagementUpdateModal #modalYear").datetimepicker({
			format: 'YYYY',
			viewMode: "years",
			defaultDate: moment().format('YYYY')
		});

		$('#vacationManagementUpdateModal .btnCancle').on("click", function(){
			vacationManagementUpdateModal.close();
		});
		
		$('#vacationManagementUpdateModal .btnSave').on("click", function(){
			vacationManagementUpdateModal.save("I");
		});
		$('#vacationManagementUpdateModal #modalDays').on("change", function(){
			//사용된 연차
			let useDay = $('#vacationManagementUpdateModal #useDay').val();
			//발행연차
			let days = $('#vacationManagementUpdateModal #modalDays').val();
			//잔여연차 갱신
			if(today<$('#vacationManagementUpdateModal #endDate').val())
				$('#vacationManagementUpdateModal #leftDays').text(days-useDay);
		});
	},
	/* 데이터 선택 */
	setSelect:function(selectNumber) {
		let checkedData = dataList[selectNumber];
		if(dataList==null||checkedData==undefined){
			return ;
		}
		
		if( vacationManagementUpdateModal.callback )  {
			vacationManagementUpdateModal.callback(checkedData);
		}
		vacationManagementUpdateModal.close();
	},
	// 상세 조회
	search:function(state) {
		let muMemberId  = vacationManagementUpdateModal.reqParam.muMemberId;
		let endDate  = vacationManagementUpdateModal.reqParam.endDate;
		let startDate  = vacationManagementUpdateModal.reqParam.startDate;
		let memberName  = vacationManagementUpdateModal.reqParam.memberName;
		let etc  = vacationManagementUpdateModal.reqParam.etc;

		if(true != fn_emptyCheck(muMemberId)){
			modalEvent.warning("조회", "직원 정보가 없습니다.");
			return;
		}
		if(true != fn_emptyCheck(startDate)){
			modalEvent.warning("조회", "사용기한 시작일자 정보가 없습니다.");
			return;
		}
		if(true != fn_emptyCheck(endDate)){
			modalEvent.warning("조회", "사용기한 종료일자 정보가 없습니다.");
			return;
		}
		let formData = new FormData();
		formData.append("menuClass", menuClass);
		formData.append("muMemberId", muMemberId);
		formData.append("endDate", endDate);
		formData.append("startDate", startDate);

		$.ajax({
			url: encodeURI('/webvacationmanagement/selectVacationManagementDays.do'),
			data: formData,
			dataType: "json",
			processData: false,
			contentType: false,
			type: 'POST',
			async: true,
			success: function(data){
				if('0'==data.msgCode){
					if(data.rows.length==1) {
						const vacationData  =data.rows[0];
						console.log(data.rows);
						$('#vacationManagementUpdateModal #modalMemberId').data('modalMemberId',muMemberId);
						$('#vacationManagementUpdateModal #modalMemberId').val(memberName);
						$('#vacationManagementUpdateModal #modalYear').val(vacationManagementSearchYear);
						$('#vacationManagementUpdateModal #modalDays').val(vacationData.days);
						$('#vacationManagementUpdateModal #leftDays').text(vacationData.leftDays);
						$('#vacationManagementUpdateModal #modalStartDate').val(vacationData.startDate);
						$('#vacationManagementUpdateModal #modalEndDate').val(vacationData.endDate);
						$('#vacationManagementUpdateModal #modalEtc').val(etc);
						$('#vacationManagementUpdateModal #useDay').val(vacationData.days-vacationData.leftDays);
						day = vacationData.days;
					}
				}
				else{
					modalEvent.danger("조회 오류", data.msgDesc);
				}
			},
			error : function(xhr, status, error) {
				modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
			},
			beforeSend:function(){

			},
			complete:function(){

			}
		});
	},
	//저장
	save:function(state) {
		let muMemberId     = $("#vacationManagementUpdateModal #modalMemberId").data('modalMemberId');
		let days           = $("#vacationManagementUpdateModal #modalDays").val();
		let leftDays       = parseInt($("#vacationManagementUpdateModal #leftDays").text())+parseInt(days)-day;
		let etc            = $("#vacationManagementUpdateModal #modalEtc").val();
		let muVacationManagementId = vacationManagementUpdateModal.reqParam.muVacationManagementId;
		let endDate 	  = $('#vacationManagementUpdateModal #modalEndDate').val();
		let startDate 	  = $('#vacationManagementUpdateModal #modalStartDate').val();
		let year 	  = $('#vacationManagementUpdateModal #modalYear').val();


		if(true != fn_emptyCheck(muVacationManagementId)){
			modalEvent.warning("수정", "연차관리 정보가 없습니다.");
			return;
		}
		if(true != fn_emptyCheck(muMemberId)){
			modalEvent.warning("수정", "직원 정보가 없습니다.");	
			return;
		}

		if(true != fn_emptyCheck(days)){
			modalEvent.warning("수정", "발행일수 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(leftDays)) {
			modalEvent.warning("수정", "잔여일수 정보가 없습니다.");
			return;
		}
		// }
		// if(today>endDate){
		// 	modalEvent.warning("수정", "사용기한이 지난 연차입니다.");
		// 	return;
		// }
		

		
		modalEvent.info("수정", "연차관리 정보를 수정하시겠습니까?", function(){
			let formData = new FormData();
			formData.append("menuClass", menuClass);
			formData.append("muMemberId", muMemberId);
			formData.append("muVacationManagementId", muVacationManagementId);
			formData.append("startDate", startDate);
			formData.append("endDate", endDate);
			formData.append("year", year);
			formData.append("days", days);
			formData.append("leftDays", leftDays);
			formData.append("etc", etc);
	
			$.ajax({
				url: encodeURI('/webvacationmanagement/updateVacationManagement.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( vacationManagementUpdateModal.callback )  {
								vacationManagementUpdateModal.callback();
							}
							vacationManagementUpdateModal.close();
						});
					}
					else{
						modalEvent.danger("수정 오류", data.msgDesc);
					}
				},
				error : function(xhr, status, error) {
					modalEvent.danger("수정 오류", "수정 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
				},
				beforeSend:function(){
					
				},
				complete:function(){
					
				}
			});
		});
	},
	/*
	 * 팝업
	 * callback : 콜백함수, reqParam: 팝업파라미터
	 */
	popup:function(callback, reqParam) {
		// 초기화 후 열기
		vacationManagementUpdateModal.init();
		
		//콜백함수 지정.
		vacationManagementUpdateModal.callback = callback;
		vacationManagementUpdateModal.reqParam = reqParam;
		vacationManagementUpdateModal.dataList = null;

		setTimeout(function(){
			$('#vacationManagementUpdateModal').modal('show')
		}, 200);

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

		setTimeout(function(){
			$('#vacationManagementUpdateModal').remove();
		},500);
	},
	HtmlTemplate : `
	<div id="vacationManagementUpdateModal" class="modal hyuga_modal fade" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<div class="nav_box">
						<p style="font-size: 18px;font-weight: 700;">연차 발행 수정</p>
					</div>
				</div>

				<div class="modal-body">
					<div class="tab-content">
						<div role="tabpanel" class="tab-pane active" id="publish">
							<div class="form_box">
								<div class="input_box">
									<label>직원</label>

									<div class="search_box" style="width: calc(100% - 100px);">
										<input style="padding: 0 10px; width:200px; float: left" type="text" id="modalMemberId" readonly>
										<input type="hidden" id="useDay">
										<div class="leftDaysBox" style="width: 100px; line-height: 36px; color: #3985EA; text-align: right; float: left;">
											<span style="color: #3985EA;">잔여 연차</span>
											<span id="leftDays" style="color: #3985EA;">-</span>일
										</div>
									</div>
								</div>

								<div class="input_box">
									<label>발행연도</label>
									<div class="calendar_box">
										<div class="date_box">
											<img src="/image/web/calendar.svg">
											<input type="text" class="date_picker" id="modalYear">
										</div>
									</div>
								</div>

								<div class="input_box">
									<label>발행 일수</label>
									<div class="text_box">
										<input type="text" id="modalDays" placeholder="00일" maxlength="2">
										<!--<p>00h</p>-->
									</div>
								</div>

								<div class="input_box">
									<label>사용 기한</label>

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

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

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

								<div class="input_box">
									<label>연차 발행 메모</label>
								
									<div class="textarea_box">
										<textarea id="modalEtc" placeholder="메모를 입력해주세요."></textarea>
									</div>
								</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>
    `
};
