
/* ##############################################################
	설  명 : [Modal]근무설정 등록 팝업
	작성일 : 2023. 01. 19.
	작성자 : NTsoft
	수정일 	        수정자 	 수정내용
	-------         -------- ---------------------------
	2023. 01. 19.	NTsoft 	 최초 생성
*/
let workInsertModal = {
	callback : null,
	reqParam : null,
	dataList : null,
	/* 모달 초기화 작업 */
	init : function () {
		$('#workInsertModal').remove();
		$('body').append( this.HtmlTemplate);
		//그리드 이벤트
		this.setEvent();
	},
	/* 그리드 이벤트 설정 */
	setEvent:function() {
		$('#workInsertModal .btnCancle').on("click", function(){
			workInsertModal.close();
		});
		
		$('#workInsertModal .btnSave').on("click", function(){
			workInsertModal.save("I");
		});
	},
	/* 데이터 선택 */
	setSelect:function(selectNumber) {
		let checkedData = dataList[selectNumber];
		if(dataList==null||checkedData==undefined){
			return ;
		}
		
		if( workInsertModal.callback )  {
			workInsertModal.callback(checkedData);
		}
		workInsertModal.close();
	},
	//저장
	save:function(state) {
		let workName     = $("#workInsertModal #modalWorkName").val();
		let startTime    = $("#workInsertModal #modalStartTime").val();
		let endTime      = $("#workInsertModal #modalEndTime").val();
		let muGroupId    = $("#workInsertModal #modalGroupId").val();
		let muDutyId     = $("#workInsertModal #modalDutyId").val();
		let etc          = $("#workInsertModal #modalEtc").val();
		
		if(true != fn_emptyCheck(workName)){
			modalEvent.warning("등록", "근무일정명 정보가 없습니다.");	
			return;
		}
		else{
			if(true != fn_lengthCheck(workName, 255)) {
				modalEvent.warning("등록", "근무일정명 정보는 255자를 넘을 수 없습니다.");	
				return;
			}
		}
		
		if(true != fn_emptyCheck(startTime)){
			modalEvent.warning("등록", "시작시간 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(endTime)){
			modalEvent.warning("등록", "종료시간 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(muGroupId)){
			modalEvent.warning("등록", "조직 정보를 선택하세요");

			return;
		}
		
		if(true != fn_emptyCheck(muDutyId)){
			modalEvent.warning("등록", "직무 정보를 선택하세요");

			return;
		}
		
		modalEvent.info("등록", "근무설정 정보를 등록하시겠습니까?", function(){
			let formData = new FormData();
			formData.append("menuClass", menuClass);
			formData.append("workName", workName);
			formData.append("startTime", startTime);
			formData.append("endTime", endTime);
			formData.append("muGroupId", muGroupId);
			formData.append("muDutyId", muDutyId);
			formData.append("etc", etc);
	
			$.ajax({
				url: encodeURI('/webwork/insertWork.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( workInsertModal.callback )  {
								workInsertModal.callback();
							}
							workInsertModal.close();
						});
					}
					else{
						modalEvent.danger("등록 오류", data.msgDesc);
					}
				},
				error : function(xhr, status, error) {
					modalEvent.danger("등록 오류", "등록 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
				},
				beforeSend:function(){
					
				},
				complete:function(){
					
				}
			});
		});
	},
	/*
	 * 팝업
	 * callback : 콜백함수, reqParam: 팝업파라미터
	 */
	popup:function(callback, reqParam) {
		// 초기화 후 열기
		workInsertModal.init();
		
		//콜백함수 지정.
		workInsertModal.callback = callback;
		workInsertModal.reqParam = reqParam;
		workInsertModal.dataList = null;
		
		// 조직 조회
		fn_insertModal_setGroupCombo();
		
		// 직무 조회
		fn_insertModal_setDutyCombo();
		
		// 기본 셋팅
		fn_setTimeValue("selModalStartTime", "09:00");
		fn_setTimeValue("selModalEndTime", "19:00");
		
		setTimeout(function(){
			$('#workInsertModal').modal('show')
		}, 200);
	},
	/*
	 * 팝업 닫기
	 */
	close:function() {
		$('#workInsertModal').modal('hide');

		setTimeout(function(){
			$('#workInsertModal').remove();
		},500);
	},
	HtmlTemplate : `
	<div id="workInsertModal" class="modal create_schedule_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>
							<input type="text" id="modalWorkName" placeholder="근무일정명" maxlength="255">
						</div>

						<div class="input_box">
							<label>시간</label>

							<div class="select_time_list_box">
								<div class="select_list first">
									<div class="select_box dropdown">
										<button type="button" class="label dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">00:00</button>
										<input type="hidden" id="modalStartTime">
										<ul class="select_option_list dropdown-menu" id="selModalStartTime"></ul>
									</div><!--select_box-->
								</div>

								<span class="slash">~</span>
									
								<div class="select_list">
									<div class="select_box dropdown">
										<button type="button" class="label dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">00:00</button>
										<input type="hidden" id="modalEndTime">
										<ul class="select_option_list dropdown-menu" id="selModalEndTime"></ul>
									</div><!--select_box-->
								</div>
							</div>
						</div>

						<div class="select_list_box">
							<label>조직/직무</label>

							<div class="select_bip_box">
								<div class="select_list first">
									<div class="select_box dropdown">
										<button class="label" class="label dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">조직</button>
										<input type="hidden" id="modalGroupId"></input>
										
										<ul class="select_option_list dropdown-menu"></ul>
									</div>
								</div>
	
								<div class="select_list" style="float:right;">
									<div class="select_box dropdown">
										<button class="label" class="label dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">직무</button>
										<input type="hidden" id="modalDutyId"></input>
										
										<ul class="select_option_list dropdown-menu"></ul>
									</div>
								</div>
							</div>
						</div>

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

/****************************************************************************
* 조직 옵션 조회
****************************************************************************/
function fn_insertModal_setGroupCombo(){
	// 대상
	const target = $("#workInsertModal #modalGroupId").siblings("ul.select_option_list");

	// 데이터 조회
	let comboData = fn_selectListGroupOption();
	
	// 조회 데이터 셋팅
	let comboHTML = '';
	for(let i = 0; i < comboData.length; i++){
		let muGroupId     = comboData[i].muGroupId;
		let groupName     = comboData[i].groupName;
		
		comboHTML += '<li id="li_'+muGroupId+'" class="option_list_item">'+groupName+'</li>';
	}
	target.empty().html(comboHTML).trigger("create");

	// 콤보 이벤트
	target.find("li.option_list_item").click(function () {
		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);
	});
}

/****************************************************************************
* 직무 옵션 조회
****************************************************************************/
function fn_insertModal_setDutyCombo(){
	// 대상
	const target = $("#workInsertModal #modalDutyId").siblings("ul.select_option_list");

	// 초기화
	target.siblings("button").text("직무");
	target.siblings("input").val('');

	// 데이터 조회
	let comboData = fn_selectListDutyOption();
	
	// 조회 데이터 셋팅
	let comboHTML = '';
	
	for(let i = 0; i < comboData.length; i++){
		let muDutyId     = comboData[i].muDutyId;
		let dutyName     = comboData[i].dutyName;
		
		comboHTML += '<li id="li_'+muDutyId+'" class="option_list_item">'+dutyName+'</li>';
	}
	target.empty().html(comboHTML).trigger("create");

	// 콤보 이벤트
	target.find("li.option_list_item").click(function () {
		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);
	});
}
