<!DOCTYPE html>
<%@page contentType="text/html; charset=UTF-8" language="java" %>
<%@page import="com.csc.library.entry.*,com.csc.library.system.*,com.csc.library.utilities.*,java.util.*"%>
<%@page import="com.csc.library.utilities.CscCalendar,com.csc.library.entry.HelpEntry,com.csc.library.system.*"%>
<%@page import="com.csc.library.entry.*,com.csc.library.database.*,com.csc.library.session.*,com.csc.library.formula.FormulaString"  %>
<%@page import="com.csc.library.timeattendance.DayShift,com.csc.library.timeattendance.Shift,com.csc.library.timeattendance.WorkingShift"  %>
<%@page import="java.text.DecimalFormat"  %>
<%@ 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" />
<%
	EMV.setChannel(request, response);
	EmvHelpEntry screen = (EmvHelpEntry) EMV.process("EmvHelpEntry", "PI_SWIPETIMEHELP");

	CheckNull chk=new CheckNull();
	String ft_empid = chk.chkNullString(request.getParameter("__employeeid"));
//	screen.inq.setFilter("employeeid = '"+EMV.getUProfile().getEmvEmpId()+"' ");
	screen.inq.setFilter("employeeid = '"+ft_empid+"' ");

	screen.inq.refresh();
	screen.nextRec();
	screen.referLangOn();

	CscCalendar cs = new CscCalendar();
	FormulaString fs = new FormulaString();
	DecimalFormat df = new DecimalFormat("0.00");
	DecimalFormat df2 = new DecimalFormat("00.00");

	String empID = EMV.getUProfile().getEmvEmpId();
	DbInquiry inqSwipeTime = new InitialInquiry(screen.getUProfile()).getDbInquiry();
	inqSwipeTime.initMyTable("Ttimetemp", "", "employeeid,swipedate,swipetime");
	inqSwipeTime.setColumn("*");
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
	td.uholiday {color:blue;}
</style>
<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" />
	</head>

	<script type='text/javascript' src='/hr/dwr/engine.js'></script>
	<script type="text/javascript" src="/hr/dwr/interface/UserChangeShift.js"></script>
	<script type='text/javascript' src='/hr/dwr/util.js'></script>

	<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("SW012470");
			setPageTitle(titlename);


			$(document).ready(function(){

				//datepicker function
				var myLang='<%=screen.getUProfile().get("lang")%>';
				if (myLang=="tha"){
					$.datepicker.setDefaults($.datepicker.regional['tha']);
				}

				$("input[name=date_start]").hover(function(){
					$("input[name=date_start]").datepicker({
						dateFormat:"dd-mm-yy",
						maxDate:0,
						defaultDate:-1,
						changeMonth: true,
						changeYear: true
					});
					$("input[name=date_end]").datepicker("destroy");
				});


				$("input[name=date_end]").change(function(){
					if ($("input[name=date_start]").val()>$("input[name=date_end]").val()){
						$("input[name=date_start]").val($("input[name=date_end]").val());
					} else if ($("input[name=date_start]").val()==""){
						$("input[name=date_start]").val($("input[name=date_end]").val());
					}
				});


				$("input[name=date_end]").hover(function(){
					$("input[name=date_end]").datepicker({
						minDate: new Date(ConvertDate($("input[name=date_start]").val())),
						dateFormat:"dd-mm-yy",
						changeMonth: true,
						changeYear: true
					});
				});


			});

			<!--
			function getMsg(msg){
				var lang = document.getElementById("__language");

				if(lang.value == "th"){
					return msg[0] ;
				}else{
					return msg[1] ;
				}
			}

			function setHoliday(obj){
				var rawdat = obj.value ;
				var cols = rawdat.split("#");
				var conb = false ;
				var newShift = "";

				var msg1 = ["ต้องการเปลี่ยนเป็นวันหยุด", "Change to holiday ?"];
				var msg2 = ["ต้องการเปลี่ยนเป็นวันทำงาน", "Change to work ?"];

				var emp = document.getElementById("__employeeid");
				var time0 = document.getElementById("__time0");


				if(cols[0] == "H" || cols[0] == "I"){
					conb = confirm(getMsg(msg2));
					if(conb){
						obj.checked = false;

						//hideLoadPopup(false);
						UserChangeShift.changeShiftByUser(emp.value, cols[2], "D", callbackResult);
					}else{
						obj.checked = true ;
					}
				}else{
					conb = confirm(getMsg(msg1));
					if(conb){
						obj.checked = true;

						//hideLoadPopup(false);
						UserChangeShift.changeShiftByUser(emp.value, cols[2], "A", callbackResult);
					}else{
						obj.checked = false ;
					}
				}
			}

			var callbackResult = function(data){
				search();
			}

			function hideLoadPopup(stat){
				var lp = document.getElementById("loading_leave");

				if(stat){
					lp.style.display = "none";
				}else{
					lp.style.display = "";
				}
			}

			function goHelp(helpName,inputName) {
				var helpReturn;
				var fixCon = "";
				if(inputName=="__employeeid" ) {
					helpReturn="__employeeid:employeeid,MEMPLOYEE@FULLNAME";
				}

				linkHelp_Return(helpName,helpReturn, fixCon);
			}
			//-->
		</script>

		<section>
			<div class="container csc-content">
				<div id="loading_leave" class="popup" align="center" style="display:none">
					<IMG SRC="../IMAGES/loading.gif">
				</div>
				<form name="cscform" class="form-horizontal" method="post" action="PI_CHANGE_SWIPETIME_ADMIN.jsp">
					<input type="hidden" name="__cmd" value="">
					<input type="hidden" name="__screen"  value="PI_CHANGE_SWIPETIME_ADMIN">
					<input type="hidden" name="__help" value="">
					<input type="hidden" name="__helpReturn" value="">
					<input type="hidden" name="__helpName" value="">
					<input type="hidden" name="__pageCall" >
					<input type="hidden" name="__referPage" value="">
					<input type="hidden" name="__calendar">
					<script language="javascript">getInputFormatDate();</script>
					<input type="hidden" name="__companyid"  value="<%=screen.getUProfile().get("companyid")%>">
					<input type="hidden" name="__fixCon"  >
					<input type="hidden" id="__language" name="__language" value="<%=screen.getUProfile().get("lang")%>">
					<input type="hidden" name="__childFilter" >
					<input type="hidden" name="__empID" value="<%=screen.getString("employeeid")%>">
					<input type="hidden" name="__empName" value="<%=screen.getString("fullname")%>">
					<div id="toolTipLayer" style="position:absolute; visibility: hidden"></div>
					<script language="javascript">initToolTips();</script>

					<div class="csc-block">
						<div class="center">
							<h2 class="csc-block-head" swlang code='SW012470'>SET DAYOFF</h2>
						</div>

						<div class="form-horizontal">

							<div class="row">
								<div class="form-group">
									<div class="col-md-4 col-sm-4 col-xs-4">
										<label class="control-label label-right" swlang code='SW000274'>Employee ID</label>
									</div>
									<div class="col-md-2 col-sm-2 col-xs-2">
										<div class="input-group">
											<input type="text" class="form-control" name="__employeeid" id="__employeeid" value="<%=chk.chkNullString(request.getParameter("__employeeid")) %>" size="8">
											<a class="input-group-addon" href="javascript: goHelp('MEMPLOYEEHELP','__employeeid');">
												<i class="fa fa-search"></i>
											</a>
										</div>
									</div>
									<div class="col-md-3 col-sm-3 col-xs-3">
										<input type="text" class="form-control" name="MEMPLOYEE@FULLNAME" id="MEMPLOYEE@FULLNAME" size="40" value="<%=chk.chkNullString(request.getParameter("MEMPLOYEE@FULLNAME")) %>" readonly class="disabled">
									</div>
								</div>
							</div>

							<div class="row">
								<div class="form-group">
									<div class="col-md-4 col-sm-4 col-xs-4">
										<label class="control-label label-right" swlang code='SW000275'>Begin Date</label>
									</div>
									<div class="col-md-2 col-sm-2 col-xs-2">
										<% if (request.getParameter("date_start")==null) { %>
										<input class="form-control" name="date_start" type="text" value="<%="01-"+cs.getMM()+"-"+cs.getYear()%>"  maxlength="10" onKeyup = "formatDate(this)" onBlur = "chkInputDate2(this,date_end)" onKeypress = "chkInteger()" >
										<% } else {%>
										<input class="form-control" name="date_start" type="text" value="<%=request.getParameter("date_start")%>" maxlength="10" onKeyup = "formatDate(this)" onBlur = "chkInputDate2(this,date_end)" onKeypress = "chkInteger()" >
										<% }%>
									</div>
									<div class="col-md-1 col-sm-1 col-xs-1">
										<label class="control-label label-right" swlang code='SW002095'>To</label>
									</div>
									<div class="col-md-2 col-sm-2 col-xs-2">
										<% if (request.getParameter("date_end")==null) { %>
										<input class="form-control" name="date_end" type="text" value="<%=cs.getDDMMYYYY()%>" size="9" maxlength="10" onKeyup = "formatDate(this)" onBlur = "chkInputDate2(date_start,this)" onKeypress = "chkInteger()" >
										<% } else {%>
										<input class="form-control" name="date_end" type="text" value="<%=request.getParameter("date_end")%>" size="9" maxlength="10" onKeyup = "formatDate(this)" onBlur = "chkInputDate2(date_start,this)" onKeypress = "chkInteger()" >
										<% }%>
									</div>
								</div>
							</div>

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

					<div class="table-responsive">
						<table class="table table-head table-hover table-bordered table-content-center">
							<thead>
								<div align = "right" swlang code='SW013247'>unit : HH.MM</div>
								<tr>
									<th swlang code='SW001033'>Date</th>
									<th swlang code='SW000262'>Day</th>
									<th swlang code='SW001815'>Working Time</th>
									<th swlang code="SW012763">Punch Time</th>
									<th swlang code='SW000367'>Day Type</th>
									<th swlang code='SW001888'>Holiday</th>
								</tr>
							</thead>
							<tbody>
									<%
										if (!screen.getDataLabel("employeeid").equals("")) {
											//out.println("employeeid -->> "+screen.getDataLabel("employeeid"));
											//out.println("if test  emp-->> "+screen.inq.getCurrentDbRecord().getParam().get("ws"));
											if (screen.inq.getCurrentDbRecord().getParam().get("ws")!=null) {
												WorkingShift pws = (WorkingShift) screen.inq.getCurrentDbRecord().getParam().get("ws");
												//out.println("-->> "+pws);
												HashMap hm = new HashMap();
												try {
													DbInquiry inq = new InitialInquiry(screen.getUProfile()).getDbInquiry();
													inq.setSchemaName(screen.getUProfile().getSchemaName());
													inq.initMyTable("Meventgrp", "", "");
													inq.setColumn("*");
													inq.refresh();
													inq.referLangOn();
													while (inq.next()) {
														DbRecord rec = inq.getCurrentDbRecord();
														hm.put(rec.getString("eventgrpid"), rec.getString("tdesc"));
													}
												} catch (Exception e) {}

												HashMap hmHoliday = new HashMap();
												try {
													DbInquiry inqHoliday = new InitialInquiry(screen.getUProfile()).getDbInquiry();
													inqHoliday.setSchemaName(screen.getUProfile().getSchemaName());
													inqHoliday.initMyTable("Mholiday1", "", "");
													inqHoliday.setColumn("*");

													DbInquiry inqTime = new InitialInquiry(screen.getUProfile()).getDbInquiry();
													inqTime.setSchemaName(screen.getUProfile().getSchemaName());
													inqTime.initMyTable("Mtime0", "time0id='"+screen.getDataLabel("time0")+"'", "");
													inqTime.setColumn("*");
													inqTime.putChild(inqHoliday);
													inqTime.refresh();
													inqTime.referLangOn();
													while (inqTime.next()) {
														DbRecord rec = inqTime.getCurrentDbRecord();
														DbInquiry child = (DbInquiry) inqTime.getChild("Mholiday1");
														do {
															CscCalendar cHol = new CscCalendar(child.getString("hdate"));
															hmHoliday.put(cHol.getDDMMYYYY(), child.getString("tdesc"));
														} while (child.next());
													}
												} catch (Exception e) {}
												//out.println(hmHoliday);
									%>
									<%
												//ทำการหาข้อมูลการรูดบัตร
												CscCalendar c1 = new CscCalendar();
												CscCalendar c2 = new CscCalendar();
												if (request.getParameter("date_start")==null && request.getParameter("date_end")==null) {
													c1 = new CscCalendar("01-"+cs.getMM()+"-"+cs.getYear());
													c2 = new CscCalendar(cs.getDDMMYYYY());
												} else {
													c1 = new CscCalendar(request.getParameter("date_start"));
													c2 = new CscCalendar(request.getParameter("date_end"));
												}
												MyHashMap hmSwipeTime = new MyHashMap();
												int count = 0;
												String tmpDate = "", swipeTime = "";
												inqSwipeTime.setFilter("employeeid='"+screen.getDataLabel("employeeid")+"' and (swipedate between '"+c1.getYYYYMMDD()+"' and '"+c2.getYYYYMMDD()+"')");
												inqSwipeTime.refresh();
												while (inqSwipeTime.next()) {
													if (count==0 || tmpDate.equals(inqSwipeTime.getString("swipedate"))) {
														swipeTime += " , "+df.format(inqSwipeTime.getCurrentDbRecord().getDouble("swipetime"));
													} else {
														if (!tmpDate.equals("") && !swipeTime.equals("")) {
															CscCalendar d = new CscCalendar(tmpDate);
															hmSwipeTime.put(d.getDDMMYYYY(), swipeTime.substring(3, swipeTime.length()));
															swipeTime = " , "+df.format(inqSwipeTime.getCurrentDbRecord().getDouble("swipetime"));
														}
													}
													count++;
													tmpDate = inqSwipeTime.getString("swipedate");
												}
												if (!tmpDate.equals("") && !swipeTime.equals("")) {
													CscCalendar d = new CscCalendar(tmpDate);
													hmSwipeTime.put(d.getDDMMYYYY(), swipeTime.substring(3, swipeTime.length()));
												}
									%>
									<%
											CscTime ot = new CscTime();
											CscTime leave = new CscTime();
											CscTime working = new CscTime();
											CscTime working_total = new CscTime();
											String cFont = "";
											String tmp = "";
											int rows = 1;
											for (Iterator IT = pws.keySet().iterator(); IT.hasNext(); ) {
												String key = (String) IT.next();
												DayShift dShift = (DayShift) pws.get(key);
												rows = dShift.size();
												for (Iterator IT1 = dShift.keySet().iterator(); IT1.hasNext(); ) {
													String key1 = (String) IT1.next();
													Shift sh = (Shift) dShift.get(key1);

													cFont = "";
													if (sh.getEventGroup().equals("H")) {
														cFont ="week";
													} else if (sh.getEventGroup().equals("I")) {
														cFont = "holiday";
													}

													if(sh.getTR_Type().equals("HD")){/* user define holiday */
														cFont = "uholiday" ;
													}

													if (sh.getDocType().equals("O") || sh.getDocType().equals("B") || sh.getDocType().equals("OM")) { //ทำการบวกชม ที่ทำโอที
														ot.add(sh.getAp_ot());
														working_total.add(sh.getAp_ot());
													} else if (sh.getDocType().equals("A") || sh.getDocType().equals("S")) { //ทำการบวกชม ที่ลา
														leave.add(sh.getLv());
													} else {
														working.add(sh.getHour_d());
														working_total.add(sh.getHour_d());
													}
												%>
								<tr>
									<%
										String date = "";
										if (tmp.equals(sh.getShiftDate().getDDMMYYYY())) {
											date = "";
										} else date = sh.getShiftDate().getDDMMYYYY();
									%>

									<% if (rows>1 && !date.equals("")) { %>

									<td rowspan="<%=rows%>" class="<%=cFont%>"><%=date%></td>

									<% } else if (rows==1) { %>

									<td rowspan="<%=rows%>" class="<%=cFont%>"><%=date%></td>

									<% } %>

									<%
										String day = "";
										if (tmp.equals(sh.getShiftDate().getDDMMYYYY())) {
											day = "";
										} else {
											if (((String) screen.getUProfile().get("lang")).equalsIgnoreCase("THA")) { //แสดงวันจันทร์ , อังคาร, พุธ เป็นต้น
												day = sh.getShiftDate().getDOWTHI();
											} else day = sh.getShiftDate().getDOWENG();
										}
									%>

									<% if (rows>1 && !date.equals("")) { %>

									<td rowspan="<%=rows%>" class="table-title <%=cFont%>"><%=day%></td>

									<% } else if (rows==1) { %>

									<td rowspan="<%=rows%>" class="table-title <%=cFont%>"><%=day%></td>

									<% } %>

									<td class="<%=cFont%>">
										<%=
											sh.getResult_c_dt_in().getDDMMYYYY()+" : "+df2.format(sh.getResult_c_dt_in().getCSCTime())+ " - "+
											sh.getResult_c_dt_out().getDDMMYYYY()+" : "+df2.format(sh.getResult_c_dt_out().getCSCTime())
										%>
									</td>

									<%
										String txtSwipeTime = "";
										if (hmSwipeTime.containsKey(sh.getShiftDate().getDDMMYYYY())) {
											txtSwipeTime = hmSwipeTime.getString(sh.getShiftDate().getDDMMYYYY());
										}
									%>

									<% if (rows>1 && !date.equals("")) { %>

									<td rowspan="<%=rows%>" class="<%=cFont%>"><%=txtSwipeTime%></td>

									<% } else if (rows==1) { %>

									<td rowspan="<%=rows%>" class="<%=cFont%>"><%=txtSwipeTime%></td>

									<% } %>

									<td class="<%=cFont%>">
										<%
											//out.println(sh.getEventGroup());
											if (sh.getTR_Type().indexOf("T")>-1 && (!sh.getDocType().equals("C") && !sh.getDocType().equals("E"))) {
												out.println();
											} else {
												if (sh.getEventGroup().equalsIgnoreCase("T"))	 { //ถ้าเป็นวันปกติ (T) ให้แสดงช่องว่าง
												out.println();
												} else	{
													String lv_type = (String) hm.get(sh.getEventGroup());
													if (lv_type==null ||  lv_type.equals("null"))  {
														out.println();
													} else {
														if (sh.getEventGroup().equals("I")) {
															if (hmHoliday.get(sh.getShiftDate().getDDMMYYYY())!=null) {
																out.println(hmHoliday.get(sh.getShiftDate().getDDMMYYYY()));
															} else out.println(lv_type);
														} else out.println(lv_type);
													}
												}
											}
										%>
									</td>

									<td class="<%=cFont%>">
										<%if(!sh.getEventGroup().equals("I")){%>
	                                	<input type="checkbox" name="chk$<%=sh.getShiftDate().getYYYYMMDD()%>" value="<%=sh.getEventGroup()+"#"+sh.getTimeCode()+"#"+sh.getShiftDate().getYYYYMMDD()%>" <%=(sh.getEventGroup().equals("H")||sh.getEventGroup().equals("I"))?"checked":""%> onclick="setHoliday(this)">
										<%}%>
									</td>
								</tr>
								<%
									tmp = sh.getShiftDate().getDDMMYYYY();
									} //End for (Iterator IT1 = dShift.keySet().iterator(); IT1.hasNext(); )
									} //End for (Iterator IT = pws.keySet().iterator(); IT.hasNext(); )
									} //End if (screen.inq.getCurrentDbRecord().getParam().get("ws")!=null)
									} //End if (!screen.getDataLabel("employeeid").equals(""))
								%>
							</tbody>
						</table>
					</div>
				</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>