<%@page contentType="text/html; charset=UTF-8"%>
<%@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.*"  %>
<%@ page import="java.io.*,java.util.*"  %>
<jsp:useBean id="ViewData" class="com.csc.library.system.Task" scope="page"/><%
ViewData.setChannel(request,response);
HelpEntry screen = (HelpEntry) ViewData.process("HelpEntry","ViewData");

String fileFullPath = request.getParameter("datafile");
String fileName="";
StringTokenizer str = new StringTokenizer(fileFullPath, "\\");
while (str.hasMoreTokens()) {
	fileName = str.nextToken();							
}
String xml = request.getParameter("__xmlType");
//out.print(xml);

%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<link href="../CSS/TISCO.css" rel="stylesheet" type="text/css">
<script type='text/javascript' src='../JS/SCREEN.js'></script>
<script language="javascript">setLang('<%=screen.getUProfile().get("lang")%>');</script>
<script language="javascript">getTitleName();</script>
<script language='javascript' src='../JS/EMPLOYEE.js'></script>
<script src="../JS/VIEWCALENDAR.js"></script>
<script language="JavaScript" src="../JS/DATEINPUT.js"></script>
<script language="JavaScript" src='../JS/ICONSMENU.js'></script>
<script language='javascript' src='../JS/HOTKEY.js'></script>
<script language="JavaScript" type="text/JavaScript">

function goProcess(fileName,xml){
	with(document.cscform){
	  	action="PRU316_IMPORT.jsp?__del=0&__fileName="+fileName+"&__cmd=process&__xml="+xml;
		submit();
	}
}

//-->
</script>
<style type="text/css">
<!--
.red{
color:#FF0000

}
.black{
color:#000000

}
-->
</style>
</head>
<body leftmargin="0" topmargin="0" onLoad="MM_preloadImages('../IMAGES/BUTTON/SWAP/SEARCH_SWAP20.gif','../IMAGES/BUTTON/SWAP/CALENDAR_SWAP20.gif')"  >
<form name="cscform" method="post" >
  <!----------BEGIN CSC INITIAL ZONE ------------------------>
  <%=screen.InitialVariable()%>
  <!----------END CSC INITIAL ZONE -------------------------->
  <input type="hidden"name="__screen"  value="ViewData">
  <input type="hidden" name="__help" value="">
  <input name="__fixCon" type="hidden" value="">
  <input type="hidden" name="__cmd"  value="">
  <input type="hidden" name="__companyid" value='<%=screen.getUProfile().get("companyid")%>'>
  <input type="hidden" name="__calendar" value="">
  <script language="javascript">getInputFormatDate();</script>
  <input type="hidden" name="__language" value="<%=screen.getUProfile().get("lang")%>">
  <input type="hidden" name="__temp" value="">
  <input type="hidden" name="__referPage" value="">
  <input type="hidden" name="__afterUpdate" value="">
  <input type="hidden" name="__emp_level" value="<%=screen.getString("emp_level")%>">
  <input type="hidden" name="__id_people" value="<%=screen.getString("id_people")%>">
  <div id="toolTipLayer" style="position:absolute; visibility: hidden"></div>
  <script language="javascript">initToolTips();</script>
  <div align="center">
    <table cellpadding="0"  cellspacing="0" >
      <tr>
        <td class="header"><script language="javascript">swapLang("View Data File");</script>
          &nbsp;</td>
      </tr>
      <tr>
        <td>
          <br>
          <table cellpadding="0" cellspacing="0">
                      <tr>
              <td colspan='4' align="center">
                <b><script language="javascript">swapLang("Data to be Import");</script></b></td>
            </tr>
<%
	if( request.getParameter("datafile") != null ){
		String filePath = request.getParameter("datafile");
		FileInputStream fin = new FileInputStream(filePath);
		BufferedInputStream bin = new BufferedInputStream(fin);
		DataInputStream din =  new DataInputStream(bin);
			
		DbInquiry fixtab = new InitialInquiry(screen.getUProfile()).getDbInquiry("mfixtab");	
		fixtab.setColumn("fixtabid,fixtab_type");
		fixtab.refresh();
		Map mf = new HashMap();
		while(fixtab.next()){
		 	mf.put(fixtab.getString("fixtabid"),fixtab.getString("fixtab_type"));
		}
		DbInquiry amounttab = new InitialInquiry(screen.getUProfile()).getDbInquiry("mamounttab");	
		amounttab.setColumn("amounttabid,amnt_type");
		amounttab.refresh();
		Map ma = new HashMap();
		while(amounttab.next()){
		 	ma.put(amounttab.getString("amounttabid"),amounttab.getString("amnt_type"));
		}
		
		DbInquiry emp = new InitialInquiry(screen.getUProfile()).getDbInquiry("memployee");	
		emp.setColumn("employeeid,status,resigndate,companyid");
		
		int wrongFormat=0;
		int normal=0,resign=0;
		String s;
		Date thisDate = new Date();
		long resignDate=new CscCalendar("01-01-2100").getTime();//default คนยังไม่ลาออก 01-01-2100
		
		while((s=din.readLine())!=null){
			boolean invalidID=false;
			 if(!s.trim().equals("")){
				String[] r  = s.split(",");
				emp.setFilter("employeeid ='"+r[0].trim()+"'");
				emp.refresh();
				String status="";
				String classColor ="";
				if(emp.next()){
				   status =  emp.getString("status");
				   resignDate = new CscCalendar(emp.getString("resigndate")).getTime();
	
					if(xml.equalsIgnoreCase("memployee")&&r.length!=6){
					  //  out.print("1");
					    wrongFormat++;
					    classColor="red";
					}
					else if((xml.equalsIgnoreCase("mempl_fix")||xml.equalsIgnoreCase("mempl_amt"))&&r.length!=4){
				//	    out.print("2");
					    wrongFormat++;
					    classColor="red";
					}
					else if(r.length==4&&(r[0].trim().equals("")||r[1].trim().equals("")||r[2].trim().equals("")||r[3].trim().equals(""))){
			//			out.print("3");
					    wrongFormat++;
					    classColor="red";
					}
					else if(r.length==6&&(r[0].trim().equals("")||r[1].trim().equals("")||r[2].trim().equals("")||r[3].trim().equals("")||r[4].trim().equals("")||r[5].trim().equals(""))){
					   // out.print("4");
					    wrongFormat++;
					    classColor="red";
					}
					else if(!r[1].trim().equalsIgnoreCase(screen.getUProfile().get("companyid"))){
				//	    out.print("5");
					    wrongFormat++;
					    classColor="red";
					}
					else if(r.length!=4&&r.length!=6){
					   // out.print("6");
					    wrongFormat++;
					    classColor="red";
					}
					else if(xml.equalsIgnoreCase("mempl_fix")&&!mf.containsKey(r[2].trim())){
					//    out.print("7");
					    wrongFormat++;
					    classColor="red";
					}
					else if(xml.equalsIgnoreCase("mempl_amt")&&!ma.containsKey(r[2].trim())){
					//	out.print("8");
					    wrongFormat++;
					    classColor="red";
					}
					else if(ma.containsKey(r[2])&&ma.get(r[2]).toString().equalsIgnoreCase("1")){
					 	try{
					 	    double d = Double.parseDouble(r[3]);
					 	}
					 	catch(Exception e){
					 //		out.print("9");
						    wrongFormat++;
						    classColor="red";
					 	}
					}
					else if(ma.containsKey(r[2])&&ma.get(r[2]).toString().equalsIgnoreCase("3")){
					 	try{
					 	    int d = Integer.parseInt(r[3]);
					 	}
					 	catch(Exception e){
					 //		out.print("10");
						    wrongFormat++;
						    classColor="red";
					 	}
					}
					else if(ma.containsKey(r[2])&&ma.get(r[2]).toString().equalsIgnoreCase("2")){
					 	try{
					 	   // double d = Double.parseDouble(r[3]);
					 //		out.print("11");
							if(r[3].equals("")){
						    	classColor="red";
						    	wrongFormat++;
							}
							else
								classColor="";	
					 	}
					 	catch(Exception e){
						    classColor="";
					 	}
					}
					else if(ma.containsKey(r[2])&&ma.get(r[2]).toString().equalsIgnoreCase("5")){
						if(r[3].charAt(4)!='-'||r[3].charAt(7)!='-'){
					//	    out.print("12");
						    wrongFormat++;
						    classColor="red";
						}
					}
					else if(mf.containsKey(r[2])&&mf.get(r[2]).toString().equalsIgnoreCase("1")){
					 	try{
					 	    double d = Double.parseDouble(r[3]);
					 	}
					 	catch(Exception e){
					 	//	out.print("13");
						    wrongFormat++;
						    classColor="red";
					 	}
					}
					else if(mf.containsKey(r[2])&&mf.get(r[2]).toString().equalsIgnoreCase("3")){
					 	try{
					 	    int d = Integer.parseInt(r[3]);
					 	}
					 	catch(Exception e){
					 //		out.print("14");
						    wrongFormat++;
						    classColor="red";
					 	}
					}
					else if(mf.containsKey(r[2])&&mf.get(r[2]).toString().equalsIgnoreCase("2")){
					 	try{
					 	    //double d = Double.parseDouble(r[3]);
							if(r[3].equals("")){
						    	classColor="red";
						    	wrongFormat++;
							}
							else
								classColor="";							
				//	 	   out.print("15");
					 	}
					 	catch(Exception e){
							classColor="";
					 	}
					}
					else if(mf.containsKey(r[2])&&mf.get(r[2]).toString().equalsIgnoreCase("5")){
						if(r[3].charAt(4)!='-'||r[3].charAt(7)!='-'){
				//		    out.print("16");
						    wrongFormat++;
						    classColor="red";
						}
					}
					else if(r.length==6&&((r[4].charAt(4)!='-'||r[4].charAt(7)!='-')||r[5].charAt(4)!='-'||r[5].charAt(7)!='-')){
			//		    out.print("17");
					    wrongFormat++;
					    classColor="red";
					}
				}
				else{
	//			   out.print("18");
				    wrongFormat++;
				    classColor="red";
					invalidID=true;
				}
				
				if(status.equalsIgnoreCase("Y")&&thisDate.getTime()>resignDate&&wrongFormat==0){
				    resign++;
%>			
	            <tr>
	              <td colspan='2'><b><font class='black'><%=s %></font> * </b></td>
	            </tr>
<%
				}
				else if(!invalidID&&wrongFormat==0){
				    normal++;
%>
	            <tr>
	              <td colspan='2'><b><font class='<%=classColor %>'><%=s %></font></b></td>
	            </tr>
<%
				}
				else if(invalidID){
%>
	            <tr>
	              <td colspan='2'><b><font class='<%=classColor %>'><%=s %></font></b><<< invalid Employee ID</td>
	            </tr>
<%
				}
				else{
%>
	            <tr>
	              <td colspan='2'><b><font class='<%=classColor %>'><%=s %></font></b></td>
	            </tr>
<%
				}
			}
		}
			if(wrongFormat>0){
%>
	            <tr>
	              <td colspan='4' ><b><script language="javascript">swapLang('Wrong Format!! Please check your data file')</script></b> </td>

	            </tr>
	            <tr>
				<td align="center" colspan="2"><a href="javascript:history.go(-1);"><img src="../IMAGES/BUTTON/BACK.gif"  border=0></a></td>
	            </tr>
<%		    
			}
			else{
%>
	            <tr>
	              <td>&nbsp;</td>
	            </tr>
	            <tr>
	              <td><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<script language="javascript">swapLang('Resign Employee')</script></b>* &nbsp;&nbsp;&nbsp;</td><td><%=resign %></td>
	            </tr>
	            <tr>
	              <td><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<script language="javascript">swapLang('Normal Employee')</script></b>&nbsp;&nbsp;&nbsp;</td><td><%=normal %> </td>
	            </tr>   		
	            <tr>
	              <td><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<script language="javascript">swapLang('Total Employee')</script></b> &nbsp;&nbsp;&nbsp;</td><td width="376"><%=resign+normal %></td>
	            </tr>     
	        <tr>
				<td align="center" colspan="2"><a href="javascript:history.go(-1);"><img src="../IMAGES/BUTTON/BACK.gif"  border=0></a><a href="javascript:goProcess('<%=fileName %>','<%=xml %>');"><img src="../IMAGES/BUTTON/PROCESS.gif"  border=0></a></td>
	            </tr>
<%				    

	}
	fin.close();
	din.close();
	bin.close();
}
%>
  	    <tr> 
    	<td  colspan="3" class="bottom"></td>
   	</tr>	
        </table>
        </td>
      </tr> 
  </table>
  </div>
  <!----------BEGIN CSC ENDING ZONE ------------------------>
  <%=screen.endJSP()%>
  <!----------END CSC ENDING ZONE -------------------------->
</FORM>
</body>
</html>