<!DOCTYPE html>
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page import="com.csc.library.system.*,com.csc.library.utilities.*"%>
<%@ page import="com.csc.library.entry.*,com.csc.library.database.*,com.csc.library.session.*"  %>
<%@ page import="java.util.*"  %>
<%@page import="com.csc.library.timeattendance.DayShift,com.csc.library.timeattendance.Shift"  %>
<%@page import="com.csc.library.timeattendance.WorkingShift,com.csc.library.report.ReportUDF"  %>
<%@page import="java.text.DecimalFormat,java.util.*,com.csc.library.databean.*;"  %>

<%@ include file="../CHECKPROFILE.jsp" %>
<% request.setCharacterEncoding("UTF-8"); %>
<jsp:useBean id="EMV" class="com.csc.library.system.Task" scope="page" />
<jsp:setProperty name="EMV" property="appname" value="empview" />
<jsp:useBean id="TA" class="com.csc.library.system.Task" scope="page" />
<jsp:setProperty name="TA" property="appname" value="empview" />
<%
	EMV.setChannel(request, response);
	TA.setChannel(request,response);
	UIManager screenTA = (UIManager)TA.process("SingleEntry","TAU147");
	HelpEntry screen = (HelpEntry)EMV.process("HelpEntry", "OI-HOLIDAYHELP");
	if(screen==null){
		return;
	}

	ReportUDF rp=new ReportUDF();
	CscCalendar cs=new CscCalendar();

	String lang = screen.getUProfile().get("lang");
	String subQuery = "select holidaycode from mtime0 where time0id='" +screen.getUProfile().get("time0id") +"' and companyid='"+screen.getUProfile().get("companyid") +"'";
	CscCalendar calendar = new CscCalendar();
	CscCalendar lastcalen = new CscCalendar();
	CscCalendar nextcalen = new CscCalendar();
	lastcalen.decYear(5);
	nextcalen.incYear(1);
	int lastmon = lastcalen.getMonth()+1;
	int nextmon = nextcalen.getMonth()+1;
	//screen.getInquiry().setFilter("hdate", calendar.getEngYYYY()+"-%","","like");
	//screen.getInquiry().setFilter("holidayid", subQuery, "" ,"in");
	screen.getInquiry().setFilter("holidayid = ("+subQuery+") AND (hdate >= '"+lastcalen.getYear()+"-"+lastmon+"-01' AND hdate <= '"+nextcalen.getYear()+"-"+nextmon+"-31')");
	screen.getInquiry().refresh();
	CheckNull chk = new CheckNull();

	HtmlCalendar htmlCal = new HtmlCalendar();

	//htmlCal.setLanguage(HtmlCalendar.THAI);
	htmlCal.setLanguage(screen.getUProfile().get("lang"));
	htmlCal.setHeader("MMMM");
	htmlCal.setWeekEnd(1);
	htmlCal.setWeekEnd(7);

	while(screen.nextRec()){
		screen.inq.referLangOn();
		htmlCal.setHoliday(screen.getInquiry().getString("hdate") , screen.getString("edesc"));
 		screen.referLangOff();
   }

	HashMap hm =new HashMap();
	DbRecord dbrec = new InitialRecord(screen.getUProfile()).getDbRecord("memployee");
	dbrec.set("employeeid",screen.getUProfile().get("employeeid"));
	dbrec.set("companyid",screen.getUProfile().get("companyid"));
	dbrec.setColumn("*");
	dbrec.search();
	if (dbrec.recCount()>0){

		Mtime0 rec = (Mtime0)new InitialRecord(screen.getUProfile()).getDbRecord("Mtime0");
		rec.setColumn("*");
		rec.set("time0id",dbrec.getString("time0"));
		rec.search();
		if (!rec.getString("time0id").equalsIgnoreCase("")){
			CscCalendar cs1 = new CscCalendar("01-01-"+cs.getYear());
			CscCalendar cs2 = new CscCalendar("31-12-"+cs.getYear());
			// cs1.incYear(1);
			cs2.incYear(1);
			rec.setEmpRec(dbrec);
			try{
				WorkingShift ws = rec.genWorkingShift(new CscCalendar(cs1.getYYYYMMDD()),new CscCalendar(cs2.getYYYYMMDD()),"true");
				ws.setUProfile(screen.getUProfile());
				ws.meargeWithShiftTrans(dbrec, new CscCalendar(cs1.getYYYYMMDD()), new CscCalendar(cs2.getYYYYMMDD()));
				if (!ws.isEmpty()){
					// out.print(">>" + ws);
					ws.setUProfile(screen.getUProfile());
					ws.setEmprec(dbrec);
					ws.setEmployeeid(screen.getUProfile().get("employeeid"));
					ws.setEmpTimeCode(dbrec.getString("time0"));
					ws.adjust();
					ws.createLink();
					hm.put("ws", ws);
				}
			}catch(Exception ex){

			}
		}
	}

	WorkingShift pws = (WorkingShift)hm.get("ws");
	/*try{
		for (Iterator IT = pws.keySet().iterator();IT.hasNext();){
			String key = (String)IT.next();
			DayShift dShift = (DayShift) pws.get(key);
			for (Iterator IT1 = dShift.keySet().iterator(); IT1.hasNext();){
				String key1 = (String)IT1.next();
				Shift sh = (Shift)dShift.get(key1);

				if (sh.getEventGroup().equalsIgnoreCase("H")){
					out.println(sh.getShiftDate().getYYYYMMDD());
				}

			}
		}
	}catch(Exception ex){
		out.println("error");
	}*/

%>
<%!
	private String getLongDate(UProfile upf, String date){
    	CscCalendar c = null;
        try{
            c=new CscCalendar(date);
        }catch(Exception ex){
			MyLog.error(this, ex);
            c=new CscCalendar();
        }
        if(upf.get("lang").toString().equalsIgnoreCase("ENG")){
            return c.getEngLongMonth()+", "+c.getDate();
        }else{
        	return c.getDate()+" "+c.getThaiLongMonth();
        }
    }

	private String truncate(String msg, int limit) {
		String pad = "...";
		String saparator = " ";
		if(msg.length() <= limit){
			return msg;
		}
		msg = msg.substring(0, limit);

		int breakpoint = msg.lastIndexOf( saparator );
		if( breakpoint >0) {
			msg = msg.substring(0, breakpoint);
		}
	 	return msg +pad;
	}
%>

<%
	char[] cyear;
	if("eng".equalsIgnoreCase(screen.getUProfile().get("lang"))) {
		cyear = calendar.getEngYYYY().toCharArray();
	} else {
		cyear = calendar.getThaiYYYY().toCharArray();
	}
%>
<html ng-app="myHR" xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Employee View</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<style>
			.eventhover{
			    display: none;
			    position: fixed;
			    width: 20%;
			    height: auto;
			    background-color: #fdfdfd;
			    top: 120%;
			    z-index: 100;
			    border-radius: 4px;
			   /* border: 1px solid #fff;*/
			    box-shadow: 1px 1px 2px 2px #B3B3B3;
			    line-height:200%;
			    font-size:16px;
			    padding-left:20px;
			    padding-right:20px;
			    /*right:5%;*/
			    text-align:center;
			}

			.fc-event-container:hover .eventhover{
				 display: block;
			}

			.darkbg{
				position:fixed;
				display:none;
				width:100%;
				height:100%;
				background:black;
				z-index:20;
				opacity:0.5;
				top:0%;
			}
			.full-calendar{
				padding-bottom: 2% !important;
			}
			.statuscalendar{
				display:block;
				float:right;
				width:64%;
				margin-bottom:30px;
			}
			.blockcolor{
				width:20px;
				height:20px;
				display:block;
				float:left;
			}
			.dayoffcolor{
				background :#feff1a;
			}
			.holidaycolor{
				background :#ff0000;
			}
			.mytxt{
				display:block;
				float:left;
				width:24%;
				padding-left:10px;
				text-align:left;
			}
			.container{
					width:100% !important;
			}
			@media screen and (max-width: 1333px){
				.eventhover{
					 font-size:14px;
				}
				.mytxt{
					width:22% !important;
				}

			}
			@media screen and (max-width: 1330px){
				.mytxt{
					width:22% !important;
				}

			}
		</style>
	</head>
	<body>
		<div class="bg-com"></div>
		<jsp:include page="EMV_HEADMENU.jsp" flush="true" />

		<script type="text/javascript">

			setLang("<%=screen.getUProfile().get("lang")%>");
			var swap = new swaplang();
            var titlename = swap.swap2String("SW002193");
            var evwidth = parseInt($("body").width())*0.2;
			setPageTitle(titlename);

			$("document").ready(function(){
				var nowdate=new Date();
				var nowlang='<%=screen.getUProfile().get("lang")%>';
				var tmp_month,tmp_year,tmp_date,varkey,detailkey,totalev=[],mypointer,tmp_date_month;
				var headname="";
				var myholiday="";
				var weekdayoff="";
				var myhollist="",mylang="";
				var beyear=nowdate.getFullYear()-5;
				var mydayOff="";
				var mypublicholiday="";
				setCalendarLang(nowlang);

				if (nowlang=="eng"){
					headname="Yearly Calendar";
					mylang="en";
					mydayOff="Day Off";
					mypublicholiday="Public Holiday";
				} else {
					headname="ปฏิทินประจำปี";
					mylang="th";
					mydayOff="วันหยุดประจำสัปดาห์";
					mypublicholiday="วันหยุดนักขัตฤกษ์";
				}

				for (beyear;beyear<=nowdate.getFullYear()+5 ; beyear++){
					for (incmonth=1;incmonth<=12 ;incmonth++){
						if (incmonth<=9){
							tmp_month="0"+incmonth;
						}else {
							tmp_month=incmonth;
						}
						myholiday=myholiday+',"ho'+beyear+tmp_month+'":[]';
						weekdayoff=weekdayoff+',"ho'+beyear+tmp_month+'":[]';
					}
				}

				myholiday=JSON.parse("{"+myholiday.substring(1)+"}");
				weekdayoff=JSON.parse("{"+weekdayoff.substring(1)+"}");

				<%
					MyHashMap holiday = htmlCal.getHoliday();
					for ( Iterator it = holiday.getKeySortIterator(); it.hasNext(); ){
						String key = it.next().toString();
				%>
						varkey="<%=key%>";
						detailkey="<%=holiday.get(key)%>";
						console.log(varkey, detailkey);

						tmp_date="ho"+getYearFYYYYMMDD(varkey)+getMonthFYYYYMMDD(varkey);
						myholiday[tmp_date].push({
							date_event:varkey,
							date_detail:detailkey,
							colorev :'#ff0000',
							txtcolor : '#fff',
							date_status : "holiday"
						});
				<%
					}

					try{
						for (Iterator IT = pws.keySet().iterator();IT.hasNext();){
							String key = (String)IT.next();
							DayShift dShift = (DayShift) pws.get(key);
							for (Iterator IT1 = dShift.keySet().iterator(); IT1.hasNext();){
								String key1 = (String)IT1.next();
								Shift sh = (Shift)dShift.get(key1);

								if (sh.getEventGroup().equalsIgnoreCase("H")){
				%>
									varkey="<%=sh.getShiftDate().getYYYYMMDD()%>";
									detailkey=mydayOff;

									tmp_date="ho"+getYearFYYYYMMDD(varkey)+getMonthFYYYYMMDD(varkey);
									weekdayoff[tmp_date].push({
										date_event:varkey,
										date_detail:detailkey,
										colorev : '#feff1a',
										txtcolor : '#202020',
									});
				<%
								}

							}
						}
					}catch(Exception ex){
						out.println("error");
					}
				%>
				for (mypointer in myholiday){
					for (tmp_month in myholiday[mypointer]){
						totalev.push({
							title:myholiday[mypointer][tmp_month].date_detail,
							start:myholiday[mypointer][tmp_month].date_event,
							end:myholiday[mypointer][tmp_month].date_event,
							color : myholiday[mypointer][tmp_month].colorev,
							textColor : myholiday[mypointer][tmp_month].txtcolor
						});
					}
				}

				for (mypointer in weekdayoff){
					for (tmp_month in weekdayoff[mypointer]){
						totalev.push({
							title:weekdayoff[mypointer][tmp_month].date_detail,
							start:weekdayoff[mypointer][tmp_month].date_event,
							end:weekdayoff[mypointer][tmp_month].date_event,
							color:weekdayoff[mypointer][tmp_month].colorev,
							textColor:weekdayoff[mypointer][tmp_month].txtcolor
						});
					}
				}

				$("#mycalendar").fullCalendar({
					header: {
						left: 'prev,next today',
						center: 'title',
						right: 'month,year'
					},
					yearColumns: 3,
					defaultView: 'year',
					eventLimit:true,
					events: totalev,
					lang: mylang
				});

				$("h3.center-detail").html(headname+" "+nowdate.getFullYear());
				$(".fc-center h2").html(headname+" "+nowdate.getFullYear());

				tmp_year=nowdate.getFullYear();
				tmp_month=getMonthAndCon(nowdate);

				$.setDateFromSelect=function(paramMonth,paramYear){
					tmp_month=paramMonth;
					tmp_year=paramYear;
				}


				$.getListMonth=function(param_incdec){
					tmp_date=tmp_year+"-"+tmp_month+"-01";

					console.log(tmp_date);

					nowdate = new Date(tmp_date);
					nowdate.setMonth(nowdate.getMonth()+param_incdec);

					tmp_year=nowdate.getFullYear();
					tmp_month=getMonthAndCon(nowdate);

					myhollist="";
					tmp_date_month="ho"+tmp_year+tmp_month;

					for(incmonth=1;incmonth<=12;incmonth++){
						myhollist+="<div class='body-event'><div class='class-title'>"+getMonthFullName(incmonth)+"</div>";
						if (incmonth<=9){
							tmp_date="ho"+tmp_year+"0"+incmonth;
						}else{
							tmp_date="ho"+tmp_year+incmonth;
						}
						if (tmp_date==tmp_date_month){
							for (mypointer in myholiday[tmp_date]){

								myhollist+="<div class='listof-body-event'><span class='highlight'>"+getDayFYYYYMMDD(myholiday[tmp_date][mypointer].date_event)+" - "+myholiday[tmp_date][mypointer].date_detail+"</span></div>";
							}
						}else{
							for (mypointer in myholiday[tmp_date]){

								myhollist+="<div class='listof-body-event'>"+getDayFYYYYMMDD(myholiday[tmp_date][mypointer].date_event)+" - "+myholiday[tmp_date][mypointer].date_detail+"</div>";
							}
						}

						myhollist+="</div>";

					}
					$(".class-list").html(myhollist);
				}

				$.getListYear=function(param_incdec){

					tmp_date=tmp_year+"-"+tmp_month+"-01";

					nowdate = new Date(tmp_date);
					nowdate.setFullYear(nowdate.getFullYear()+param_incdec);

					tmp_year=nowdate.getFullYear();

					myhollist="";
					for (incmonth=1;incmonth<=12;incmonth++){
						myhollist+="<div class='body-event'><div class='class-title'>"+getMonthFullName(incmonth)+"</div>";

						if (incmonth<=9){
							tmp_date="ho"+tmp_year+"0"+incmonth;
						}else{
							tmp_date="ho"+tmp_year+incmonth;
						}

						for (mypointer in myholiday[tmp_date]){
							myhollist+="<div class='listof-body-event'>"+getDayFYYYYMMDD(myholiday[tmp_date][mypointer].date_event)+" - "+myholiday[tmp_date][mypointer].date_detail+"</div>";
						}

						myhollist+="</div>";
					}

					$(".class-list").html(myhollist);
				}

				$.getListYear(0);


				$(".fc-month-button").click(function(){
					$.resizeHover();

					$.getListMonth(0);
				});

				$(".fc-year-button").click(function(){
					$.unResizeHover();

					$("h3.center-detail").html(headname+" "+tmp_year);
					$(".fc-center h2").html(headname+" "+tmp_year);
				});

				$(".fc-prev-button").click(function(){

					if ($(".fc-month-button").hasClass("fc-state-active")){
						$.getListMonth(-1);
					}else if ($(".fc-year-button").hasClass("fc-state-active")){
						tmp_month="01";
						$.getListYear(-1);
						$("h3.center-detail").html(headname+" "+tmp_year);
						$(".fc-center h2").html(headname+" "+tmp_year);
					}

				});

				$(".fc-next-button").click(function(){

					if ($(".fc-month-button").hasClass("fc-state-active")){
						$.getListMonth(1);
					}else if ($(".fc-year-button").hasClass("fc-state-active")){
						tmp_month="01";
						$.getListYear(1);
						$("h3.center-detail").html(headname+" "+tmp_year);
						$(".fc-center h2").html(headname+" "+tmp_year);
					}
				});

				$(".fc-today-button").click(function(){


					nowdate = new Date();

					tmp_month=getMonthAndCon(nowdate);
					tmp_year=nowdate.getFullYear();
					console.log(tmp_year);
					$("h3.center-detail").html(headname+" "+nowdate.getFullYear());
					$(".fc-center h2").html(headname+" "+nowdate.getFullYear());
					setTimeout($.getListMonth(0),1000);
				});

				$.resizeHover = function (){
					//$(".eventhover").css("width","40%");
				}

				$.unResizeHover = function(){
					//$(".eventhover").css("width","90%");
				}
				$(".fc-toolbar").append("<div class=\"statuscalendar\"><div class=\"blockcolor dayoffcolor\"></div><span class=\"mytxt\">"+mydayOff+"</span><div class=\"blockcolor holidaycolor\"></div><span class=\"mytxt\">"+mypublicholiday+"</span></div>");
			});

			window.onmousemove = function(ev){
				var x = ev.clientX;
				var y = ev.clientY;
				//var evwidth = $(".eventhover").width();
				var nowbodywidth = $("body").width()-$(".list-event").width()-100;

				$(".eventhover").css("top",y+20);
				if ((evwidth+x)>=nowbodywidth){
					$(".eventhover").css("left",x-evwidth);
				}else{
					$(".eventhover").css("left",x-40);
				}
			}


			function showHover(myId){
				var mystr = $("#"+myId).find(".fc-title").html();

				console.log($("#"+myId).find(".fc-event").css("background-color"));
				var bgcolor=$("#"+myId).find(".fc-event").css("background-color");
				var txtcolor=$("#"+myId).find(".fc-event").css("color")
				$("#"+myId).find(".eventhover").css("background-color",bgcolor);
				$("#"+myId).find(".eventhover").css("color",txtcolor);
				$("#"+myId).find(".eventhover").html(mystr);
				//$("#"+myId).find(".eventhover").css("display","block");
			}

			function disableHover(){
				//$(".eventhover").css("display","none");
			}

			function selectMonth(thismonth,thisyear){
				$.resizeHover();
				var tmpMonth
				thismonth++;

				if (thismonth<=9){
					tmpMonth="0"+thismonth;
				} else {
					tmpMonth=thismonth;
				}

				tmpYear=thisyear;
				$.setDateFromSelect(tmpMonth,tmpYear);
				$.getListMonth(0);
			}
		</script>

		<section>
			<div class="container csc-content" style="width:100% !important">
				<form class="form-horizontal" name="cscform" method="post" action="?">
					<!-- BEGIN CSC INITIAL ZONE -->
					<%=screen.InitialVariable()%>
					<!-- END CSC INITIAL ZONE -->
					<input type="hidden" name="__cmd" value="">
					<input type="hidden" name="__screen" value="OI_HOLIDAY">
					<input type="hidden" name="__language" value ="<%=screen.getUProfile().get("lang")%>">

					<div class="csc-block-white">
						<div class="list-event">
							<div class="list-block-event">
								<h3 class="center-detail"></h3>
								<div class="class-list">
								</div>
							</div>
						</div>

						<div id="mycalendar" class="full-calendar"></div>
						<!-- <div class="statuscalendar">
							<div class="blockcolor dayoffcolor"></div><span class="mytxt">Day Off</span>
							<div class="blockcolor holidaycolor"></div><span class="mytxt">Holiday</span>
						</div> -->
					</div><!-- end csc-block-white -->
				</form>
			</div>
			<a href="#top" id="top-button">
				<i class="fa fa-angle-up"></i>
				<div class="top-word">Back to top</div>
			</a>
		</section>
		<jsp:include page="FOOTER.jsp" flush="true" />
	</body>
</html>