<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" session="false"%>
<%@ include file="/WEB-INF/jsp/include/taglib.jsp"%>

<c:set var="pageKorName" scope="request">일정관리</c:set>
<div class="content-wrapper" data-doctx="${pageName}">
	<div class="container-xxl flex-grow-1 px-0">
		<div class="card wrapper-list">
			<div name="calendar" class="m-4 px-4 w-px-1500 h-px-700">

			</div>
		</div>
	</div>
</div>



<script>
LoadScript("fullCalendarScript","/webjars/3rd-party/sneat/libs/fullcalendar/fullcalendar.js");
LoadScript("mngt02010Script",wctx.url("/resources/js/fims/mngt/mngt02010.js?ver=${ver}"));

pageObject["${pageName}"] = newDoctxFinder("${pageName}");

$(document).ready(function(){

	var $P = pageObject["${pageName}"];

	$P.calendar = new Calendar($P.findn("calendar"), {
	    initialView: 'dayGridMonth',
	    plugins : [dayGridPlugin, interactionPlugin],
	    locale: 'ko',
	    editable: false,
	    selectable: true,
	    height: '700px',
	    handleWindowResize : false,
	    eventColor: '#ffeecc',
	    select: function(arg) {
	    	$P.getInfo("", arg.startStr.replaceAll("-",""), DateSupport.getPrevDay(arg.endStr.replaceAll("-",""),1));
	    },
	    eventClick:function(obj, element) {
	    	$P.getInfo(obj.event.id, "", "");
	    }
	});

	$P.getInfo = (schdlId, schdlBgngYmd, schdlEndYmd) => {

		ajax.get({
    		url : wctx.url("/mngt/mngt02/020/info.do"),
    		data : {
    			schdlId : schdlId,
    			schdlBgngYmd : schdlBgngYmd,
    			schdlEndYmd : schdlEndYmd
    		},
    		success : (resp) => {

    			let dialogName = "scheduleInfoDialog";
				let dialogId = dialogName+"-"+uuid();

    			dialog.open({
					id : dialogId,
					title : "일정 정보",
					size : "xl",
					content : resp,
					init : () => {
						$("#"+dialogId).attr("name", dialogName);
						$("#"+dialogId).attr("data-ref-doctx","${pageName}");

						var parentRes = new Object();
						var childReq = pageObject.childReq.pop();

						for(var reqKey in childReq) {
							if($P.provide[reqKey]){
						    	parentRes[reqKey] = $P.provide[reqKey];
						    } else {
						    	parentRes[reqKey] = function(){};
						    }
						}

						pageObject.parentRes.push(parentRes);
					}
				});
    		}
    	});

	}

	$P.add = (info) => {
		$P.calendar.addEvent({
			id     : info.schdlId,
			title  : info.schdlCn,
			start  : dateFormat.format(info.schdlBgngYmd),
			end    : dateFormat.format(DateSupport.getNextDay(info.schdlEndYmd,1)),
			allDay : true
		});
	}

	$P.modify = (info) => {
		var ev = $P.calendar.getEventById(info.schdlId);
		ev.setProp("title", info.schdlCn);
		ev.setDates(dateFormat.format(info.schdlBgngYmd), dateFormat.format(DateSupport.getNextDay(info.schdlEndYmd,1)), { allDay : true });
	};

	$P.del = (info) => {
		$P.calendar.getEventById(info.schdlId).remove();
	};

	$P.provide = {
		"refresh" : function(type, info){
			if(type == "add"){
				$P.add(info);
			} else if(type == "modify"){
				$P.modify(info);
			} else if(type == "del"){
				$P.del(info);
			}
    	}
	};

	//
	$P.calendar.render();

	//
	ajax.get({
		url : wctx.url("/mngt/mngt02/010/list.do"),
		data : {},
		success : (resp) => {
			if(resp.list != null && resp.list.length > 0){
				for(var i=0; i < resp.list.length; i++){
					var info = resp.list[i];

					$P.calendar.addEvent({
						id     : info.SCHDL_ID,
					    title  : info.SCHDL_CN,
					    start  : dateFormat.format(info.SCHDL_BGNG_YMD),
					    end    : dateFormat.format(DateSupport.getNextDay(info.SCHDL_END_YMD,1)),
					    allDay : true
					});

				}
			}
		}
	});

});
</script>