<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="java.util.*,java.io.*"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="com.csc.library.entry.*,com.csc.library.utilities.*"%>
<%@page import="com.csc.library.session.InitialEnvironment,com.csc.library.util.download.Downloadfile"%>
<%@page import="com.csc.library.util.download.*"%>

<%request.setCharacterEncoding("UTF-8");%>

<jsp:useBean id="USER_PROFILE" class="com.csc.library.system.Task" scope="page"/>

<%
USER_PROFILE.setChannel(request,response);
USER_PROFILE.checkProfile();
if( USER_PROFILE.getUProfile() != null ){
	CheckNull chkNull = new CheckNull();
	long attach_time = Long.parseLong(chkNull.chkNullString(request.getParameter("attach_time"),"0"));
	
	if( request.getParameter("downfile") != null ){
		ThaiUtilities tu = new ThaiUtilities();
		String filePath = request.getParameter("downfile");
		File f = new File(tu.ASCII2Unicode(filePath));
		String fileName = f.getName();
		String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());
		if ( f.exists() && f.canRead() ) {			
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment; filename="+tu.Unicode2ASCII(f.getName()));
			response.setHeader("cache-control", "no-cache");
			response.setContentLength((int) f.length());
			BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(f));
			byte buffer[] = new byte[8 * 1024];
			out.clearBuffer();
			OutputStream out_s = response.getOutputStream();
			CopyStreams.copyStreamsWithoutClose(fileInput, out_s, buffer);
			out_s.flush();
			out_s.close();
			fileInput.close();
		}
	}
%>

<HTML>
	<HEAD>
		<TITLE>myNOTE : Business Process Management Service</TITLE>
		<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/MYNOTE_SCREEN.js"></SCRIPT>
		<SCRIPT TYPE="text/JavaScript" SRC="../JS/SCREEN.js"></SCRIPT>
		<SCRIPT LANGUAGE="JavaScript">setLang('<%=USER_PROFILE.getUProfile().get("lang")%>');</SCRIPT>
		<SCRIPT LANGUAGE="JavaScript" TYPE="text/JavaScript">
		<!--
			function GoUpload(){
				with( document.cscform ){
					if( nullCheck() ){
						aCon=confirm(MyCode[1]);
						if( aCon == true ){
							enctype='multipart/form-data';
							encoding='multipart/form-data';
							action='UPLOAD_ATTACHFILE.jsp';
							submit();
						}
					}
				}
			}
			
			function GoDelete(){
				with( document.cscform ){
					aCon=confirm(MyCode[4]);
					if( aCon == true ){
						var checkbox = document.getElementsByName("attach_file");
						for( i = 0 ; i < checkbox.length ; i++ ){
							if( checkbox[i].checked ){
								delete_file.value += checkbox[i].value+",";
							}
						}
						submit();
					}
				}
			}
			
			function GoBack(){
				with( document.cscform ){
					//opener.getParam();
					opener.document.cscform.submit();
					window.close();
				}
			}
		//-->
		</SCRIPT>
	</HEAD>
	<BODY BGCOLOR=#FFFFFF>
		<FORM NAME="cscform" METHOD="post" ACTION="ATTACH_FILE.jsp">
			<INPUT TYPE="hidden" NAME="__cmd" VALUE="">
			<INPUT TYPE="hidden" NAME="__screen" VALUE="ATTACH_FILE">
			<INPUT TYPE="hidden" NAME="__fixCon" VALUE="">
			<INPUT TYPE="hidden" NAME="__notNull" VALUE="file_name,">					
			
			<INPUT TYPE="hidden" NAME="attach_time" VALUE="<%=attach_time%>">
			<INPUT TYPE="hidden" NAME="delete_file" VALUE="">
			
			<br>
			<table border="0" align="center" cellpadding="0" cellspacing="0" class="inner">
				<tr > 
					<td  class="header"><script language="javascript">swapLang("Upload File")</script>&nbsp;</td>
				</tr>
  				<tr>
					<td>
      					<table border="0" cellpadding="0" cellspacing="0" class="maxsize">
							<tr>
								<td  class="blankspace" colspan="2">&nbsp;</td>
							</tr>
							<tr>
								<td width="20%" align="right">เพิ่มไฟล์ :</td>
								<td width="80%">&nbsp;<input type="file" name="__file_name" value=""></td>
							</tr>
							<tr>
								<td  class="blankspace" colspan="2">&nbsp;</td>
							</tr>
      					</table>
      				</td>
				</tr>
				<tr>
					<td>
						<table border="1" cellpadding="0" cellspacing="1" class="maxsize">
      						<tr >
								<td width="10%" align="center" class="title1"><input type="checkbox" name="checkall" onclick="checkBox('attach_file');"></td>
      							<td width="70%" align="center" class="title1"><script language="javascript">swapLang("File Name")</script></td>
     							<td width="20%" align="center" class="title1"><script language="javascript">swapLang("Size")</script> (KB)</td>
      						</tr>
      					<%
      					CscCalendar calendar = new CscCalendar(attach_time);
      					InitialEnvironment environment = new InitialEnvironment("GLOBAL");
      					Downloadfile download = new Downloadfile(environment.getValue("DIRUPLOAD-dir")+attach_time);
      					if( !chkNull.chkNullString(request.getParameter("delete_file")).equals("") ){
      						String[] file_name = request.getParameter("delete_file").split(",");
      						for( int i = 0 ; i < file_name.length ; i++ ){
      							download.delFile(file_name[i]);
      						}
      					}      					
      					HashMap hm = download.listFile();
      					ArrayList file_name = (ArrayList)hm.get("file_name");
      					ArrayList file_size = (ArrayList)hm.get("file_size");
      					
      					for( int i = 0 ; i < file_name.size() ; i++ ){
      					%>
      						<tr >
								<td align="center"><input type="checkbox" name="attach_file" value="<%=file_name.get(i)%>" onclick="deCheck();"></td>
      							<td align="left"><a href="<%=request.getRequestURI()+"?"+download.getDownloadLink((String)file_name.get(i))%>"><%=file_name.get(i)%></a></td>
     							<td align="center"><%=file_size.get(i)%></td>
      						</tr>
      					<%
      					}
      					%>
      					</table>
					<td>
				</tr>
				<tr>
						<td  class="blankspace" >&nbsp;</td>
				</tr>
				<tr>
					<td align="center">
						<a href="JavaScript:GoUpload();"><IMG SRC="../IMAGES/BUTTON/SAVE.gif" border="0"></a>
						<a href="JavaScript:GoDelete();"><IMG SRC="../IMAGES/BUTTON/DELETE.gif" border="0"></a>
						<a href="JavaScript:GoBack();"><IMG SRC="../IMAGES/BUTTON/BACK.gif" border="0"></a>
					</td>
				</tr>
				<tr>
					<td  class="blankspace" >&nbsp;</td>
				</tr>
				<tr> 
					<td  class="bottom"></td>
				</tr>
			</table>
		</form>
	</body>
</html>
<%
}
%>