
/* ##############################################################
	설  명 : [Modal]재고마감 팝업
	작성일 : 2023. 01. 19.
	작성자 : NTsoft
	수정일 	        수정자 	 수정내용
	-------         -------- ---------------------------
	2023. 01. 19.	NTsoft 	 최초 생성
*/
let stockCloseInsertModal = {
	callback : null,
	reqParam : null,
	dataList : null,
	/* 모달 초기화 작업 */
	init : function () {
		// $('#stockCloseInsertModal').remove();
		// $('body').append( this.HtmlTemplate);
		//그리드 이벤트
		// this.setEvent();
	},
	/* 그리드 이벤트 설정 */
	setEvent:function() {
		// $('#stockCloseInsertModal .btnCancle').on("click", function(){
		// 	stockCloseInsertModal.close();
		// });
		//
		// $('#stockCloseInsertModal .btnSave').on("click", function(){
		// 	stockCloseInsertModal.save("I");
		// });
		$("#stockCloseInsertModal #modalEndDate").datetimepicker({
			allowInputToggle: true,
			format: 'YYYY-MM-DD',  // 날짜 형식을 'YYYY-MM'에서 'YYYY-MM-DD'로 변경
			defaultDate: moment().date() <= 10 ? moment().subtract(1, 'months').endOf('month') : moment()
		});
		date.format
		// 날짜 datepicker
		//fn_insertModal_searchDatePicker("#stockCloseInsertModal #modalStartDate", "#stockCloseInsertModal #modalEndDate");
	},
	/* 데이터 선택 */
	setSelect:function(selectNumber) {
		let checkedData = dataList[selectNumber];
		if(dataList==null||checkedData==undefined){
			return ;
		}
		
		if( stockCloseInsertModal.callback )  {
			stockCloseInsertModal.callback(checkedData);
		}
		stockCloseInsertModal.close();
	},
	//저장
	save:function(state) {
		let endDate        = state?.format('YYYY-MM-DD');

		if(true != fn_emptyCheck(endDate)){
			modalEvent.warning("등록", "마감종료시간 정보가 없습니다.");	
			return;
		}
		
		modalEvent.info("등록", "재고마감 생성 정보를 등록하시겠습니까?", function(){
			let formData = new FormData();
			formData.append("menuClass", menuClass);
			//formData.append("startDate", startDate);
			formData.append("endDate", endDate);
	
			$.ajax({
				url: encodeURI('/webstockclose/insertStockClose.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( stockCloseInsertModal.callback )  {
								stockCloseInsertModal.callback();
							}
							stockCloseInsertModal.close();
						});
					}
					else{
						modalEvent.danger("등록 오류", data.msgDesc);
					}
				},
				error : function(xhr, status, error) {
					modalEvent.danger("등록 오류", "등록 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
				},
				beforeSend:function(){
					
				},
				complete:function(){
					
				}
			});
		});
	},
	/*
	 * 팝업
	 * callback : 콜백함수, reqParam: 팝업파라미터
	 */
	popup:function(callback, reqParam) {
		// 초기화 후 열기
		stockCloseInsertModal.init();
		
		//콜백함수 지정.
		stockCloseInsertModal.callback = callback;
		stockCloseInsertModal.reqParam = reqParam;
		stockCloseInsertModal.dataList = null;
		stockCloseInsertModal.save(moment().date() <= 10 ? moment().subtract(1, 'months').endOf('month') : moment());
		setTimeout(function(){
			$('#stockCloseInsertModal').modal({keyboard:false, backdrop:'static'});
		}, 200);
	},
	/*
	 * 팝업 닫기
	 */
	close:function() {
		$('#stockCloseInsertModal').modal('hide');

		setTimeout(function(){
			$('#stockCloseInsertModal').remove();
		},500);
	},
	HtmlTemplate : `
	<div id="stockCloseInsertModal" class="modal stock_inventory_close_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="calendar_box">
								<!--<div class="date_box">
									<img src="/image/web/calendar.svg" alt="calendar"/>
									<input type="date" class="date_picker startDate" 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 endDate" 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>
    `
};


/****************************************************************************
* datePicker 
****************************************************************************/
function fn_insertModal_searchDatePicker(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);   
		}
	});
}