<!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.*"  %>
<%@ include file="../CHECKPROFILE.jsp" %>
<% request.setCharacterEncoding("UTF-8"); %>
<jsp:useBean id="IMPORT_MTIME" class="com.csc.library.system.Task" scope="page"/>
<jsp:setProperty name="IMPORT_MTIME" property="appname" value="empview" />

<%@page import="java.util.*,java.net.*,java.io.*,com.csc.upload.*,com.csc.library.cscimport.MyImport;"%>
<jsp:useBean id="upload" class="com.csc.library.system.Task" scope="page"/>
<jsp:useBean id="hmul" class="com.csc.upload.HttpMultiPartParser" scope="page"/>

<%
		CheckNull chkNull = new CheckNull();

		IMPORT_MTIME.setChannel(request ,response);
		HelpEntry screen=(HelpEntry) IMPORT_MTIME.process("HelpEntry","REV004HELP");
		if(screen==null)return;	
	    screen.getInquiry().setFilter(" (interviewstatus is null or interviewstatus='' or interviewstatus ='0')  and requestid in(select requestid from mcommittee where employeeid='"+screen.getUProfile().get("employeeid")+"') and requestid in(select requestid from mcandidatere where candidatestatus<='3')");
		screen.getInquiry().getCurrentDbRecord().getChild("MAPPLICANT").setFilter("applicantstatus!='4'");
		//screen.getInquiry().getChild("mcandidatere").setFilter("candidatestatus='2'");
		screen.getInquiry().setRequireFoundTable("MCANDIDATERE,MAPPLICANT"); 
		screen.process();
		
		upload.setChannel(request,response); 
		upload.checkProfile();	
		UProfile u=upload.getUProfile();

		InitialEnvironment en = new InitialEnvironment("GLOBAL");
		String filePath=en.getValue("dirimport-dir");

		String tempdir = ".";

		request.setAttribute("dir", request.getParameter("dir"));
		String browser_name = request.getRequestURI();	
		String status="";
		Hashtable ht=new Hashtable();	
		String dir="";
		String table = "ttimetemp";
		String xml="";
		
%>
<%
	if ((request.getContentType()!=null)&&(request.getContentType().toLowerCase().startsWith("multipart"))){
		response.setContentType("text/html");
		try{		
			int bstart = request.getContentType().lastIndexOf("oundary=");
			String bound = request.getContentType().substring(bstart+8);
			ht = hmul.processData(request.getInputStream(), bound, tempdir);
    		hmul.uploadFiles(ht);									
			u.set("upload","true");			
			dir=(String) ht.get("dir");
			xml=(String) ht.get("__xml");
			
			FileInfo f = (FileInfo) ht.get("myFile");
			String fileFullPath = f.file.toString();			
			String fileName="";
			StringTokenizer str = new StringTokenizer(fileFullPath, "/");
			while (str.hasMoreTokens()) {
				fileName = str.nextToken();							
			}
			
			response.sendRedirect("IMPORT_MTIME_PRO.jsp?__fileName="+fileName+"&__cmd=process_data");
		}
		catch (Exception e){ e.printStackTrace(); }
	}
	
	CscCalendar calendar = new CscCalendar();
	long attach_time = Long.parseLong(chkNull.chkNullString(request.getParameter("attach_time"),String.valueOf(calendar.getTimeInMillis())));
	String group_name = chkNull.chkNullString(request.getParameter("__wf__group_name"),"");
	if(!group_name.equals("")){
	//	moveData(group_name,screen.getUProfile());
		moveDataToMtime2(group_name,screen.getUProfile());
	}
%>
<%!
	public void moveDataToMtime2(String listid, UProfile up) {
		CscCalendar now = new CscCalendar();
		try{			
			DbInquiry temp = new InitialInquiry(up).getDbInquiry("MTIME2_TEMP_IMPORT");
			temp.setColumn("*");
			temp.setFilter("listid = '"+listid+"'");
			temp.refresh();
			DbRecord rec = null;
			String field = "";
			String value = "";

			while(temp.next()){
				if(rec==null){
					rec = new InitialRecord(up).getDbRecord("MTIME2");
				}
				rec.set("COMPANYID",temp.getString("companyid"));
				rec.set("MONTHID",temp.getString("monthid"));
				rec.set("YEARID",temp.getString("yearid"));
				rec.set("TIME0ID",temp.getString("time0id"));
				rec.search();
				System.out.print(">>>>> COMPANYID : "+temp.getString("companyid")+" >>> MONTHID : "+temp.getString("monthid")+" >>> YEARID : "+temp.getString("yearid")+" TIME0ID : "+temp.getString("time0id"));
				
				for(int i=1 ; i<=31 ; i++){
					field = "TIME"+getDay(i)+temp.getString("groupid");
					value = temp.getString("time"+getDay(i)+temp.getString("groupid"));
					if(!value.equals("")){
						rec.set(field,value);
					}
				}
				rec.set("WF_REF_DOC","");
				rec.set("EDIT_DATE",now.getDDMMYYYY());
				rec.set("EDIT_TIME",now.getHHMMSS());
				rec.set("EDIT_BY",temp.getString("create_by"));  
				rec.save();
			}
		}catch(Exception e){
			//MyLog.error(this,this.getUProfile(),e);
		}
	}
	public String getDay(int day){
		if(day<10){
			return "0"+day;
		}else{
			return ""+day;
		}
	}
%>

<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myHR">
	<head>
		
		<title>Interview Schedule</title>
		
		<meta http-equiv="Content-Type" content="text/html; charset=TIS620" />
		
		<script type='text/javascript' src='/hr/dwr/interface/SearchDWR.js'></script>
		<script type='text/javascript' src='/hr/dwr/engine.js'></script>
		<script type='text/javascript' src='/hr/dwr/util.js'></script>
		<script type="text/javascript" src="/hr/dwr/interface/SystemCode.js"></script>
		<script type="text/javascript" src="/hr/dwr/interface/CscCalendar.js"></script>

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

			var num = 1;
			
			function showView(){
				num += 1;
				if(num %2 ==0){
					$("#content01").css("display","");
				}
				else{
					$("#content01").css("display","none");
				}
			};
			
			function MM_preloadImages() { //v3.0
				var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
				var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
				if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
			}
			function MM_swapImgRestore() { //v3.0
			  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
			}

			function MM_findObj(n, d) { //v4.01
			  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
				d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
			  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
			  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
			  if(!x && d.getElementById) x=d.getElementById(n); return x;
			}

			function MM_swapImage() { //v3.0
			  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
			   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
			}
			
			function checktype(){
				var stdstr="";
				with(document.cscform){
					if(myFile.value.length>0){
						stdstr=myFile.value.substring(myFile.value.lastIndexOf("\\")+1,myFile.value.length);
						if(stdstr.indexOf(".csv")>-1){					
							submit(); 
						}else if(stdstr.indexOf(".txt")>-1){					
							submit(); 
						} else{
							alert(MyCode[81]);
						}
					} else if (myFile.value.length==0)
					alert(MyCode[80]);
				}
			}

			function notInput(){
				with(document.cscform){
					alert(myFile.value);
					myFile.value="5";
				}
			}
			function uploadFileEmpOT() {
				var oldfilename="";
				with(document.cscform){
					if( __wf__filename.value!=""){
						oldfilename = __wf__filename.value;
					}
					page = "/hr/EMPVIEW/UPLOADFILE_SHIFT_ADMIN_WF.jsp?uploadfield=import_shift.file_name&subfolder=" +attach_time.value+"&__del="+__wf__group_name.value+"&oldfilename="+oldfilename+"&emp_boss=&fieldreturn=__wf__group_name";
					var win = window.open(page,"","height=600,width=800,resizable=1,toolbar=0,scrollbars=1,status=1");
					win.focus();
				}
			}
			
			function setcframe(source,mframe){
			   var vframe=document.getElementById(mframe); 
			   vframe.src=source;		  
			}
			function initiframe(type){
				var source = "";
				with( document.cscform){
					if(__wf__group_name.value != "" || type == undefined){
						__fixCon.value = "listid ='"+__wf__group_name.value+"'";
						var A = 1;
						source="SHOWSHIFTLIST_ADMIN.jsp?__step_id=1&__fixCon="+__fixCon.value+"&__GROUPNAME="+__wf__group_name.value+"&__hometype="+A+"&__boss=";
						setcframe(source,"viewframe");
					}else if(type != undefined){
						alert("Please Upload File");
					}
				}
			}
			function goSave(){
				var isRed = document.getElementById('viewframe').contentWindow.document.getElementById('__isRed').value;
			//	alert("isRed : "+isRed);

				with(document.cscform){
					if(__wf__group_name.value==""){
						alert("กรุณาอัพโหลดไฟล์..!!");
					}else if(isRed=="1"){
						alert("ไม่สามารถบันทึกข้อมูลได้ \n กรุณาตรวจสอบข้อมูลให้ถูกต้อง..!!")
					}else{
						var bCon = confirm("ต้องการอัพโหลดข้อมูลหรือไม่");
						if(bCon){
							confirmSave();
						}
					}
				}
			}
			function showperview(){
				initiframe('x'); 
			}
			function confirmSave(){
				with(document.cscform){		    	
						save();
				}
			}
		
		</script>
		
		<section>
			<div class="container csc-content">
				<form name="cscform" class="form-horizontal" method="post" action="IMPORT_MTIME.jsp">
					<input type="hidden" name="_text_Count" value="0">	
					<input type="hidden" name="__wf__filename" id="__wf__filename" class="disabled" readonly value="<%=chkNull.chkNullString(request.getParameter("__wf__filename"),"") %>">
					<input type="hidden" name="attach_time" value="<%=attach_time%>">	
					<input type="hidden" name="__fixCon" value="">

					<input type="hidden" name="__screen"  value="IMPORT_MTIME">
					<input type="hidden" name="__help" value="">
					<input type="hidden" name="__status"  value="">
					<input type="hidden" name="__cmd"  value="">
					<input type="hidden" name="__emp_request"  value="<%//=screen.getUProfile().get("employeeid")%>">
					<input type="hidden" name="__companyid" value='<%=screen.getUProfile().get("companyid")%>'>
					<input type="hidden" name="__calendar" value="">
					<script>getInputFormatDate();</script>
					<input type="hidden" name="__language" value ="<%=screen.getUProfile().get("lang")%>">  
					<input type="hidden" name="__wf_status" value="6" >
					<input type="hidden" name="__wfcmd" value="false">    	
					<input type="hidden" name="__autonum" value="shiftid, ">
					<input type="hidden" name="__wf__pathname"  id="__wf__pathname" maxlength="50" value="<%=chkNull.chkNullString(request.getParameter("__wf__pathname"),"") %>" class="disabled" readonly >
					<input type="hidden" name="__oldfilename" id="__oldfilename" class="disabled" readonly value="<%=chkNull.chkNullString(request.getParameter("__oldfilename"),"") %>">
					<input type="hidden" name="__uploadFile" value="<%=chkNull.chkNullString(request.getParameter("__uploadFile"),"false")%>">
					<input type="hidden" name="__isRed" value="">
					<div id="toolTipLayer" style="position:absolute; visibility: hidden"></div>
					<script>initToolTips();</script>
					
					<form  name="cscform"  enctype="multipart/form-data" method="POST">
						
						<div class="csc-block">
							<div class="center">
								<h2 class="csc-block-head" swlang code='SW002422'>Import Mtime Excel</h2>
							</div>
							
							<div class="form-horizontal">
								<div class="row">
									<div class="form-group">
										<div class="col-md-5 col-sm-5 col-xs-5">
											<label class="control-label label-right" swlang code='SW002422'>Import Mtime Excel</label>
										</div>

										<div class="col-md-3 col-sm-3 col-xs-3">
											<div class="input-group">
												<input class="form-control" name="__wf__group_name" id="__wf__group_name" type="text" maxLength="50" readOnly=""/>
												<a class="input-group-addon" href="javascript:uploadFileEmpOT();" >
													<i class="fa fa-upload"></i>
												</a>
											</div>
										</div>
									</div>
								</div>
								
								<div class="center">
									<a class="btn btn-primary" href="javascript:showView();" swlang code='SW012192'>Example</a>
									<a class="btn btn-primary" href="javascript:showperview();" swlang code='SW012193'>Preview</a>
									<a class="btn btn-primary" href="javascript:goSave();" swlang code='SW012170'>Upload</a>
								</div>
							</div>
						</div>
						
						<%
							UProfile up = (UProfile)session.getAttribute("userprofile");	
							
							String name = "";
							String surname = "";
						%>
						
						<div class="table-responsive">
							<table class="table table-head table-hover table-bordered table-title-left table-content-center" id="content01" style="display:none">
								<tr>
									<th class="table-title" colspan=13 swlang code='SW012194'>File Format Example</th> 
								</tr>
								<tr>
									<th swlang code='SW000800'>Month</th>
									<th swlang code='SW000717'>Year</th>
									<th swlang code='SW000274'>Employee ID</th>
									<th swlang code='SW000195'>Name</th>
									<th swlang code='SW000730'>Lastname</th>
									<th swlang code='SW000261'>Shift Pattern</th>
									<th swlang code='SW008607'>shift</th>
									<th>1</th>
									<th>2</th>
									<th>3</th>
									<th>4</th>
									<th>...</th>
									<th>31</th>
								</tr>
								<%
									if(up.get("lang").equals("eng")){
										name = "Arnon";
										surname = "Bunying";
									} else{
										name = "อานนท์";
										surname = " บุญยิ่ง";
									}
								%>
								<tr>
									<td>8</td>
									<td>2014</td>
									<td>10601540004</td>
									<td class="table-title"><%=name%></td>
									<td class="table-title"><%=surname%></td>
									<td>10601540004.1</td>
									<td>1</td>
									<td>APF003</td>
									<td>APF003</td>
									<td>APF010</td>
									<td>APF010</td>
									<td>...</td>
									<td>APF043</td>
								</tr>
								<%
									if(up.get("lang").equals("eng")){
										name = "Latisha";
										surname = "Kanpan";
									} else{
										name = "ลาทิชา";
										surname = " แก่นพันธ์";
									}
								%>
								<tr>
									<td>8</td>
									<td>2014</td>
									<td>10601550008</td>
									<td class="table-title"><%=name%></td>
									<td class="table-title"><%=surname%></td>
									<td>10601550008.2</td>
									<td>3</td>
									<td>2APF003</td>
									<td>APF010</td>
									<td>APF010</td>
									<td>APF003</td>
									<td>...</td>
									<td>APF213</td>
								</tr>
								<%
									if(up.get("lang").equals("eng")){
										name = "Vanessa";
										surname = "Yimsungnern";
									} else{
										name = "วนิศษา";
										surname = " ยิ้มสูงเนิน";
									}
								%>
								<tr>
									<td>8</td>
									<td>2014</td>
									<td>10601550010</td>
									<td class="table-title"><%=name%></td>
									<td class="table-title"><%=surname%></td>
									<td>10601550010.1</td>
									<td>2</td>
									<td>2APF003</td>
									<td>APF010</td>
									<td>APF010</td>
									<td>APF003</td>
									<td>...</td>
									<td>APF043</td>
								</tr>
							</table>
						</div>
						
						<iframe id="viewframe" name="detail1" src="" width="100%" height="300"></iframe>
						
						<%screen.referLangOff();%>
						<script>initiframe();</script> 
						<%=screen.endJSP()%>
					</form>
				</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>