<!DOCTYPE html>
<%@page contentType="text/html; charset=UTF-8" language="java" %>
<%@page import="com.csc.library.entry.HelpEntry,com.csc.library.system.*,com.csc.library.utilities.*"%>
<%@page import="com.csc.library.entry.*,com.csc.library.database.*,com.csc.library.session.*,java.util.*,java.text.*"  %>
<%@ include file="../CHECKPROFILE.jsp" %>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setHeader("X-XSS-Protection","0"); %>

<jsp:useBean id="EMV" class="com.csc.library.system.Task" scope="page" />
<jsp:setProperty name="EMV" property="appname" value="empview" />
<%
	EMV.setChannel(request,response);
	UIManager screen = (UIManager) EMV.process("SingleEntry","MG_PLAN");
	CscCalendar c = new CscCalendar();

	String holidayDate = "";
	HashMap hmHoliday = new HashMap();
	if ((screen.getRecord().getString("time0id")!=null && !screen.getRecord().getString("time0id").equals("") || request.getParameter("__time0id")!=null)) {
		DbInquiry inqHol = new InitialInquiry(screen.getUProfile()).getDbInquiry("Mholiday1");
		inqHol.setSchemaName(screen.getUProfile().getSchemaName());
		inqHol.setColumn("*");

		DbInquiry inqMtime0 = new InitialInquiry(screen.getUProfile()).getDbInquiry("Mtime0");
		inqMtime0.setSchemaName(screen.getUProfile().getSchemaName());
		if (!request.getParameter("__time0id").equals("")) {
			inqMtime0.setFilter("time0id='"+request.getParameter("__time0id")+"'");
		} else inqMtime0.setFilter("time0id='"+screen.getRecord().getString("time0id")+"'");
		inqMtime0.setColumn("time0id, companyid, tdesc, edesc, holidaycode");
		inqMtime0.putChild(inqHol);
		inqMtime0.refresh();
		while(inqMtime0.next()){
			DbInquiry child = (DbInquiry) inqMtime0.getChild("Mholiday1");
			if (child.recCount()>0) {
				do{
					CscCalendar cscHol = new CscCalendar(child.getString("hdate"));
					hmHoliday.put(cscHol.getDDMMYYYY(), child.getString("tdesc"));
				} while(child.next());
			}
		}
	}
	//out.println(hmHoliday);

	DecimalFormat df = new DecimalFormat("00");
%>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myHR">
	<head>

		<title>Employee View</title>

		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

		<style>
			.input-small{
				width:120px !important;

			}

			.form-small{
				width:320px !important;
			}
		</style>

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

		<script>

			setLang("<%=screen.getUProfile().get("lang")%>");
			var swap = new swaplang();
            var titlename = swap.swap2String("SW012380");
			setPageTitle(titlename);

			function goHelp(helpName,inputName){
				with(document.cscform){
					var helpReturn="";
					var fcon=__fixCon.value;
					var sub_value = "";
					if (inputName=="__time0id") {
						console.log("case1");
						var fil = "status='1'";
						__fixCon.value = fil ;

						helpReturn = inputName+":time0id,__desc:edesc"
						linkHelp_Return_Mgplan(helpName,helpReturn);
					} else if (inputName.indexOf("__time0") > -1) {
						console.log("case2");
						__fixCon.value = "status='0'";
						sub_value = inputName.substring(6,inputName.length);
						helpReturn = inputName+":time0id,__tdesc"+sub_value+":edesc";
						if (fcon=="")
							linkHelp_Return(helpName,helpReturn);
						else
							helpPageCon(helpName,helpReturn,fcon);
					}

					__fixCon.value = "";
					__isHelp.value = "1";
				}
			}

			function linkHelp_Return_Mgplan(help,input, fixCon){
				with(document.cscform){

					var nInput = input;
					var filter = fixCon || __fixCon.value;
					if(input.indexOf(":")>0){
						nInput=input.substring(0,input.indexOf(":"));
					}
					obj = eval("document.cscform."+nInput);
					if((obj=="")||(obj.value=="")){
							var param="PRU084SEARCH.jsp?__ismg_plan=true&__helpName="+help+"&__helpReturn="+input+"&__pageCall="+__screen.value+"&__fixCon="+filter+"&__checkVerify=off";
					}else{
							var param="PRU084SEARCH.jsp?__ismg_plan=true&__helpName="+help+"&__helpReturn="+input+"&__pageCall="+__screen.value+"&__fixCon="+filter+"&__checkVerify=off"+"&__valueSearch="+obj.value;
					}

					win = window.open(param,"Help","left=150,top=150,width=730,height=600,toolbar=no,status=yes,scrollbars=yes,resizable=yes");
					if(window.focus){win.focus();}
				}
			}

			function linkHelp_Return2(help,input, fixCon){
				with(document.cscform){

					var nInput = input;
					var filter = fixCon || __fixCon.value;
					if(input.indexOf(":")>0){
						nInput=input.substring(0,input.indexOf(":"));
					}
					obj = eval("document.cscform."+nInput);
					if((obj=="")||(obj.value=="")){
							var param="PRU084SEARCH.jsp?__helpName="+help+"&__helpReturn="+input+"&__pageCall="+__screen.value+"&__fixCon="+filter+"&__checkVerify=off";
					}else{
							var param="PRU084SEARCH.jsp?__helpName="+help+"&__helpReturn="+input+"&__pageCall="+__screen.value+"&__fixCon="+filter+"&__checkVerify=off"+"&__valueSearch="+obj.value;
					}

					win = window.open(param,"Help","left=150,top=150,width=730,height=600,toolbar=no,status=yes,scrollbars=yes,resizable=yes");
					if(window.focus){win.focus();}
				}
			}

			/* link help for mg_plan */
			function linkHelp(help,input){
				with(document.cscform){
					obj = eval("document.cscform."+input);
					__fixCon.value = "status='0'";

					if(obj.value==""){
							var param="PRU084.jsp?__helpName="+help+"&__fieldSearch="+input+"&__pageCall="+__screen.value+"&__fixCon="+__fixCon.value;
					}else{
							var param="PRU084.jsp?__helpName="+help+"&__fieldSearch="+input+"&__pageCall="+__screen.value+"&__fixCon="+__fixCon.value+"&__valueSearch="+obj.value;
					}

					win = window.open(param,"Help","left=150,top=150,1gh2=730,height=600,resizable=yes,scrollbars=yes,toolbar=no,status=yes");
					if(window.focus){win.focus();}
				}
			}

			function GoSearch(){
				with(document.cscform){
					if (__time0id.value=="") {
						alert(MyCode[0]);
						__time0id.focus();
					} else if (__yearid.value=="") {
						alert(MyCode[0]);
						__yearid.focus();
					} else search();
				}
			}

			function GoSave(){
				with (document.cscform) {
					if (__time0id.value=="") {
						alert(MyCode[0]);
						__time0id.focus();
					} else if (__yearid.value=="") {
						alert(MyCode[0]);
						__yearid.focus();
					} else confirmSave();
				}
			}

			function getMonthName(m){
				with(document.cscform){
					var monthName = "";
					var engMonth = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
					var thaiMonth = new Array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
					if (__language.value=='tha') {
						monthName = thaiMonth[parseInt(m)-1];
					} else monthName = engMonth[parseInt(m)-1];
					//alert(m+" "+engDate);
					document.write(monthName);
				}
			}
		</script>

		<section>
			<div class="container csc-content">
				<form name="cscform" class="form-horizontal" method="post" action="MG_PLAN.jsp">
					<%=screen.InitialVariable()%>
					<input name="__cmd" type="hidden" value="">
					<input type="hidden" name="__calendar">
					<script>getInputFormatDate();</script>
					<input type="hidden" name="__language" value="<%=screen.getUProfile().get("lang")%>">
					<input type="hidden" name="__notNull" value='<%//=screen.getRequire()%>'>
					<input type="hidden" name="__screen" value="MG_PLAN">
					<input type="hidden" name="__help" value="">
					<input type="hidden" name="__gorec" value="">
					<input type="hidden" name="__ack" value="">
					<input type="hidden" name="__helpReturn" value="">
					<input type="hidden" name="__companyid" value="<%=screen.getUProfile().get("companyid")%>">
					<input type="hidden" name="__employeeid" value="<%=screen.getUProfile().get("actor")%>">
					<input type="hidden" name="__fixCon" value="">
					<input type="hidden" name="__cmdchild" value="">
					<input type="hidden" name="__status" value="1">
					<input type="hidden" name="__isHelp" value="">
					<div id="toolTipLayer" style="position:absolute; visibility: hidden"></div>
					<script>initToolTips();</script>

					<div class="csc-block">
						<div class="center">
							<h2 class="csc-block-head" swlang code='SW012380'>WorkingTime Pattern</h2>
						</div>

						<div class="form-horizontal">
							<div class="row">
								<div class="form-group">
									<div class="col-md-1 col-sm-1 col-xs-1"></div>
									<div class="col-md-2 col-sm-2 col-xs-2">
										<label class="control-label label-right" swlang code='SW001851'>Time ID</label>
									</div>

									<div class="col-md-2 col-sm-2 col-xs-2">
										<% if (request.getParameter("__time0id")!=null) { %>
										<div class="input-group">
											<input class="form-control" name="__time0id" type="text" value="<%=request.getParameter("__time0id")%>"/>
											<a class="input-group-addon" href="javascript:goHelp('MTIME0HELP','__time0id')">
												<i class="fa fa-search"></i>
											</a>
										</div>
										<% } else { %>
										<div class="input-group">
											<input class="form-control" name="__time0id" type="text"/>
											<a class="input-group-addon" href="javascript:goHelp('MTIME0HELP','__time0id')">
												<i class="fa fa-search"></i>
											</a>
										</div>
										<% } %>
									</div>

									<div class="col-md-2 col-sm-2 col-xs-2">
										<label class="control-label label-right" swlang code='SW000012'>Description</label>
									</div>
									<div class="col-md-2 col-sm-2 col-xs-2">
										<% if (request.getParameter("__desc")!=null) { %>
										<input class="form-control" name="__desc" type="text" value="<%=request.getParameter("__desc")%>"/>
										<% } else { %>
										<input class="form-control" name="__desc" type="text" />
										<% } %>
									</div>
								</div>
							</div>

							<div class="row">
								<div class="form-group">
									<div class="col-md-1 col-sm-1 col-xs-1"></div>
									<div class="col-md-2 col-sm-2 col-xs-2">
										<label class="control-label label-right"><%=screen.getLabel("monthid")%></label>
									</div>

									<div class="col-md-2 col-sm-2 col-xs-2">
										 <select class="form-control" name="__monthid">
											<%
												String m = "";
												if (!screen.getRecord().getString("monthid").equals("0")) {
													m = df.format(screen.getRecord().getDouble("monthid"));
												} else if (request.getParameter("__monthid")!=null) {
													m = df.format(Double.parseDouble(request.getParameter("__monthid")));
												} else {
													m = c.getMM();
												}
											%>
											<option value="01" <% if (m.equals("01")) { %> selected <%}%>><label swlang code='SW012039'>January</label></option>
											<option value="02" <% if (m.equals("02")) { %> selected <%}%>><label swlang code='SW001840'>February</label></option>
											<option value="03" <% if (m.equals("03")) { %> selected <%}%>><label swlang code='SW001841'>March</label></option>
											<option value="04" <% if (m.equals("04")) { %> selected <%}%>><label swlang code='SW001842'>April</label></option>
											<option value="05" <% if (m.equals("05")) { %> selected <%}%>><label swlang code='SW001843'>May</label></option>
											<option value="06" <% if (m.equals("06")) { %> selected <%}%>><label swlang code='SW001844'>June</label></option>
											<option value="07" <% if (m.equals("07")) { %> selected <%}%>><label swlang code='SW001845'>July</label></option>
											<option value="08" <% if (m.equals("08")) { %> selected <%}%>><label swlang code='SW012040'>August</label></option>
											<option value="09" <% if (m.equals("09")) { %> selected <%}%>><label swlang code='SW001847'>September</label></option>
											<option value="10" <% if (m.equals("10")) { %> selected <%}%>><label swlang code='SW012041'>October</label></option>
											<option value="11" <% if (m.equals("11")) { %> selected <%}%>><label swlang code='SW011910'>November</label></option>
											<option value="12" <% if (m.equals("12")) { %> selected <%}%>><label swlang code='SW001850'>December</label></option>
										</select>
									</div>

									<div class="col-md-2 col-sm-2 col-xs-2">
										<label class="control-label label-right"><%=screen.getLabel("yearid")%></label>
									</div>
									<div class="col-md-2 col-sm-2 col-xs-2">
										<% if (!screen.getRecord().getString("yearid").equals("0")) { %>
										<input type="text" class="form-control" name="__yearid" value="<%=screen.getRecord().getString("yearid")%>"maxlength="4" onkeypress="chkInteger()" onkeyup="formatYear(this)" onblur="chkTrueYear(this)">
										<% } else if (request.getParameter("__yearid")!=null) { %>
										<input type="text" class="form-control" name="__yearid" value="<%=request.getParameter("__yearid")%>" maxlength="4" onkeypress="chkInteger()" onkeyup="formatYear(this)" onblur="chkTrueYear(this)">
										<% } else { %>
										<input type="text" class="form-control" name="__yearid" value="<%=c.getYear()%>" maxlength="4" onkeypress="chkInteger()" onkeyup="formatYear(this)" onblur="chkTrueYear(this)">
										<% }%>
									</div>
								</div>
							</div>

							<div class="center">
								<a class="btn btn-primary" href="javascript:GoSearch();" swlang code='SW003537'>Search</a>
							</div>
						</div>
					</div>

					<div class="csc-block-white">

						<div class="table-responsive">
							<table class="table table-head table-hover table-bordered table-title-left ">
								<thead>
									<tr>
										<th swlang code='SW000002'>Date</th>
										<th swlang code='SW000262'>Day</th>
										<th colspan=2 swlang code='SW000263'>Shift ID 1</th>
										<th colspan=2 swlang code='SW000264'>Shift ID 2</th>
									</tr>
								</thead>
								<tbody>
									<%
										String mm = screen.getRecord().getString("monthid");
										String yy = screen.getRecord().getString("yearid");
										String mm_box = request.getParameter("__monthid");
										String yy_box = request.getParameter("__yearid");
										CscCalendar cDate = null;

										if (!mm.equals("") && !yy.equals("") && !mm.equals("0") && !yy.equals("0")) {
											cDate = new CscCalendar("01"+"-"+mm+"-"+yy);
										} else if (mm_box!=null && yy_box!=null && !mm_box.equals("") && !yy_box.equals("") && !mm_box.equals("0") && !yy_box.equals("0")) {
											cDate = new CscCalendar("01"+"-"+mm_box+"-"+yy_box);
										} else {
											cDate = new CscCalendar();
											cDate.setDate(1);
										}
										//cDate = new CscCalendar();
										//out.println("------->" + cDate+" >> "+mm+" >> "+yy);
									%>
									<%
										String num="0";
										String dayText = "";
										int day=cDate.getEndDateOfMonth();
										for(int i=1;i<=day;i++ ){
											num="0";
											if(i<10) {
												num=num+i;
											} else {
												num=i+"";
											}
											if (screen.getUProfile().get("lang").equalsIgnoreCase("tha")) {
												dayText = cDate.getDOWTHI();
											} else dayText = cDate.getDOWENG();

											//เช็ควันเสาร์และอาทิตย์ให้แสดง W
											String cFont = "";
											if (cDate.getDOW()==7 || cDate.getDOW()==1) {
												cFont ="week";
											}
											if (hmHoliday.containsKey(cDate.getDDMMYYYY())) {
												cFont = "holiday";
												holidayDate += cDate.getDDMMYYYY()+" "+((String) hmHoliday.get(cDate.getDDMMYYYY()))+", ";
											}
									%>
									<tr>
										<td class='<%=cFont%>'><center><%=cDate.getDDMMYYYY()%></center></td>
										<td class='<%=cFont%>'><center><%=dayText%></center></td>
										<td>
											<center>
												<%screen.setProperties("TIME"+num+"1","help_return_field","true");%>
												<div class="input-group input-small">
													<input class="form-control <%=cFont%>" name="__time<%=num%>1" maxlength="10" value="<%=screen.getDataLabel("TIME"+num+"1")%>" onkeypress="chkSpecialStr(event);" onkeyup="sysIsEdit();" onblur="trimValue(this);" onfocus="this.select();" type="text">
													<a class="input-group-addon" href="javascript:goHelp('MTIME0HELP','__time<%=num%>1');">
														<i class="fa fa-search"></i>
													</a>
												</div>
											</center>
										</td>
										<td>
											<center>
												<input class="form-control form-small <%=cFont%>" type="text" name="__tdesc<%=num%>1" value="<%=screen.getRecord().getString("TIME"+num+"1DESC")%>" readonly>
											</center>
										</td>
										<td>
											<center>
												<%screen.setProperties("TIME"+num+"2","help_return_field","true");%>
												<div class="input-group input-small">
													<input class="form-control <%=cFont%>" name="__time<%=num%>2" maxlength="10" value="<%=screen.getDataLabel("TIME"+num+"2")%>" onkeypress="chkSpecialStr(event);" onkeyup="sysIsEdit();" onblur="trimValue(this);" onfocus="this.select();" type="text">
													<a class="input-group-addon" href="javascript:goHelp('MTIME0HELP','__time<%=num%>2');">
														<i class="fa fa-search"></i>
													</a>
												</div>
											</center>
										</td>
										<td>
											<center>
												<input class="form-control form-small <%=cFont%>" type="text" name="__tdesc<%=num%>2" value="<%=screen.getRecord().getString("TIME"+num+"2DESC")%>" readonly>
											</center>
										</td>
									</tr>
									<%
											cDate.incDate(1);
										 }
									%>
									<tr>
										<td colspan=6>* <%=holidayDate%></td>
									</tr>
								</tbody>
							</table>
						</div>
						<div class="center">
							<a class="btn btn-primary" href="javascript:GoSave();" swlang code='SW000117'>Save</a>
							<a class="btn btn-primary" href="javascript:GoSearch();" swlang code='SW012132'>Cancel</a>
						</div>
					</div>
					<%=screen.endJSP()%>
				</form>
			</div>
		</section>
		<jsp:include page="FOOTER.jsp" flush="true" />
	</body>
</html>