<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="java.sql.*" %>
<%!
	public String toParamStr(String oldStr, String newStr) {
		try {
			if (oldStr == null || oldStr.equals("")) {
				return newStr;
			} else {
				return oldStr.trim();
			}
		} catch (Exception e) {
			return oldStr;
		}
	}

	public String toEucKr(String s) {
		try {
			return new String(s.getBytes("iso-8859-1"), "EUC-KR");
		} catch (Exception e) {
			return s;
		}
	}

	public String convertDBtoWEB (String str) {
		try {
			str = replaceStr(str, "&amp;", "&");
			str = replaceStr(str, "&lt;", "<");
			str = replaceStr(str, "&gt;", ">");
			str = replaceStr(str, "&quot;", "\"");
			str = replaceStr(str, "&#039;", "\'");
			str = replaceStr(str, "&#092;", "\\");
			str = replaceStr(str, " ", "&nbsp;");
		} catch(Exception e){
			System.out.println("<< convertWEBtoDB (String str) >> Error : " + e.getMessage()); 
		}
		
		return str;	
	}

	public String replaceStr(String stro, String s1, String s2) {
		int i = 0;
		String rStr = "";
		while( stro.indexOf(s1) > -1 ) {
		      i = stro.indexOf(s1);
		      rStr += stro.substring(0,i)+s2;
		      stro = stro.substring(i+s1.length());
		}
		return rStr+stro;
	}
%>

<%
	String spage	= request.getParameter("spage");
	int    no		= Integer.parseInt(request.getParameter("no"));
	System.out.println("no"+no);	
	String galtype = toParamStr(request.getParameter("galtype"), "2");
	String galseason = toParamStr(request.getParameter("galseason"), "1");
	String menuId = request.getParameter("menuId");
	if(menuId == null || menuId.equals("")) menuId = "11";	
	if (spage == null)
		spage = "1";
%>

<%
	Connection conn = null;

		try { // 클래스 로드
			Class.forName("org.postgresql.Driver");
			conn = DriverManager.getConnection("jdbc:postgresql:hanmaum", "postgres", "");
		} catch(Exception e) {
	}

	PreparedStatement ps = null;
	ResultSet rs = null;
	ResultSet rsList = null;

	String searchPage = toParamStr(request.getParameter("searchPage"), "1");
	String searchType = toParamStr(request.getParameter("searchType"), "01");
	String searchWord = toEucKr(toParamStr(request.getParameter("searchWord"), ""));	
	String searchLine = toParamStr(request.getParameter("searchLine"), "10");	//한페이지에 보여줄 글의 최대 갯수
	String todate = "";

	int totalCnt = 0;								//글의 총 갯수
	int totalPage;									//총 페이지 갯수
	int blockSize = 10;								//한 화면에 보여줄 최대 페이지 갯수
	int blockPage;									//한 화면에 보여지는 페이지 중 첫번째 페이지 번호
	
	int start_no	= (Integer.parseInt(searchPage)-1)*Integer.parseInt(searchLine) + 1;	//해당 페이지의 첫번째 row number
	int end_no		= Integer.parseInt(searchPage)*Integer.parseInt(searchLine);			//해당 페이지의 마지막 row number(최대값)
	String query = "select now() as sysdate ";

	ps = conn.prepareStatement(query);
	rs = ps.executeQuery ();

	while ( rs.next() ) {
		todate = rs.getString("sysdate");
	}

	rs.close();

	String 	sql = "";
	
	sql = " select count(*) as cnt from gallery where type = ? ";
	
	try {
	
		ps = conn.prepareStatement(sql);
		ps.setString(1, galtype);
		rs = ps.executeQuery ();
		
			
		if ( rs.next() ) {
			totalCnt = rs.getInt ("cnt");
		}
	} catch (Exception e) {
		System.out.println("tqna_List Error : " + e.toString());
	}  finally {
		try {
			if (rs != null) rs.close();
			if (ps != null) ps.close();
		} catch (Exception e) {
			System.out.println("Close Error : " + e.toString());
		}
	}
	
	totalPage = ( (int) (totalCnt - 1)/Integer.parseInt(searchLine) ) + 1;
	blockPage = ( (int) (Integer.parseInt(searchPage) -1)/blockSize ) * blockSize + 1;

	try {

		sql = " select  no, type, title, photo1, photo2, photo3, article, writer, wdate, ip, passwd, hitcnt, state, rpl_no, season ";
		sql += " from gallery where type = ? and state = '1' and no = ? ";
System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww  2");

		ps = conn.prepareStatement(sql);
		ps.setString(1, galtype);
		ps.setInt(2, no);
		rsList = ps.executeQuery ();
		if(rsList.next()) {
			String	content = convertDBtoWEB(rsList.getString(7));
			String	view_content = replaceStr(content, " ", "&nbsp;");
System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww  2");

			view_content = replaceStr(view_content, "\n", "<br>");
	System.out.println("no"+no);

%>
<jsp:include page="/hanmaum/top.jsp" flush="false"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ko" xml:lang="ko"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr" />
<link rel="stylesheet" href="/hanmaum/Common/hstyle.css" type="text/css">
<title>무제 문서</title>
<script type="text/javascript">
<!--

	function onList() {
		location.href = "./sub_43.jsp?galtype=<%= galtype %>&galseason=<%= galseason %>";
	}

	function onSave() {
		var frm = document.form1;
		var title = frm.title.value;
		var writer = frm.writer.value;
		var article = frm.article.value;
		var passwd = frm.passwd.value;
		var file_nm_new01 = frm.file_nm_new01.value;
		var file_nm_new02 = frm.file_nm_new02.value;
		var file_nm_new03 = frm.file_nm_new03.value;

		title = title.replace(/\s| /gi, '');
		writer = writer.replace(/\s| /gi, '');
		passwd = passwd.replace(/\s| /gi, '');

		if(title == "") {
			alert ("제목을 입력하세요.");
			frm.title.focus();
			return;
		}

		if(writer == "") {
			alert ("작성자를 입력하세요.");
			frm.writer.focus();
			return;
		}

		if(passwd == "") {
			alert ("비밀번호를 입력하세요.");
			frm.passwd.focus();
			return;
		}

		if(article == "") {
			alert ("내용을 입력하세요.");
			frm.article.focus();
			return;
		}

		if(confirm ("게시물을 수정하시겠습니까?")){
		    frm.submit ();
		}
	}

	function onDelete() {
		var frm = document.form1;

		if(confirm ("게시물을 삭제하시겠습니까?")){
			frm.update_yn.value = "D";
			frm.target = "_self";
		    frm.submit ();
		}
	}
//-->
</script>
</head>
<%@ include file="/hanmaum/Intro/01_subimg.jsp" %>
<form name="form1" method="post" action="gallery_write_ok.jsp" enctype="multipart/form-data">
	<input type="hidden" name="galseason" value="<%= galseason %>" />
	<input type="hidden" name="type" value="<%= galtype %>" />
	<input type="hidden" name="galtype" value="<%= galtype %>" />
	<input type="hidden" name="update_yn" value="Y" />
	<input type="hidden" name="todate" value="<%= todate %>" />
	<input type="hidden" name="no_str" value="<%=no%>" />
<table width="940" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td width="239" valign="top" id="left">
<!--좌측메뉴-->
			<%@ include file="/hanmaum/gallery/left_menu.jsp" %>
<!--좌측메뉴끝-->
		</td>
		<td width="701" align="center" valign="top">
<!--컨텐츠 시작-->
<table width="684" border="0" cellspacing="0" cellpadding="0" class="fqtable">
				<tr>
					<td height="25" align="right" valign="bottom" id="ma1">
						<img src="/hanmaum/Image/location.gif" width="51" height="13" align="absmiddle"> 홈 &gt; 갤러리 &gt; <% if(galtype.equals("1")) { %>주변풍경<% } else if(galtype.equals("2")) { %>한마음연수원 전경<% } else if(galtype.equals("3")) { %>실내인테리어<% } else if(galtype.equals("4")) { %>사용자갤러리<% } %><% if(galtype.equals("1") || galtype.equals("2")) { %> &gt; <% if(galseason.equals("1")) { %>봄<% } else if(galseason.equals("2")) { %>여름<% } else if(galseason.equals("3")) { %>가을<% } else if(galseason.equals("4")) { %>겨울<% } %><% } %>
					</td>
				</tr>
				<tr>
					<td>
						<img src="/hanmaum/Image/06_centop01.gif" alt="" width="701" height="56" />
					</td>
				</tr>	
				<tr>
					<td>
					<% if(galtype.equals("1")) { %>
						<img src="/hanmaum/Image/gallery_tbanner01.gif" alt="" width="701" height="110" />
					<% } else if(galtype.equals("2")) { %>
						<img src="/hanmaum/Image/gallery_tbanner02.gif" alt="" width="701" height="110" />
					<% } else if(galtype.equals("3")) { %>
						<img src="/hanmaum/Image/gallery_tbanner03.gif" alt="" width="701" height="110" />
					<% } else if(galtype.equals("4")) { %>
						<img src="/hanmaum/Image/gallery_tbanner04.gif" alt="" width="701" height="110" />
					<% } %>
						
					</td>
				</tr>					
				<tr>
					<td>&nbsp;</td>
				</tr>	
		<% if(galtype.equals("1") || galtype.equals("2")) {  %>
		<tr>
			<td height="36" background="/hanmaum/Image/co_4nobar01.gif">
				<table>
					<tr>
						<td width="50" align="center" ><img src="/hanmaum/Image/co_4nobar03.gif" alt="" width="21" height="12" /></td>
						<td width="154" align="left" ><input type="text" name="title" id="title" value="<%=rsList.getString(3)%>" /></td>
						<td width="80" align="center" ><img src="/hanmaum/Image/co_4nobar05.gif" alt="" width="31" height="12" /></td>
						<td width="90" align="left" ><input name="writer" type="text" id="writer" size="12" value="<%=rsList.getString(8)%>" /></td>
						<td width="80" height="36" align="center">
						<!--<img src="/hanmaum/Image/co_4nobar07.gif" alt="" width="21" height="12" />-->
						</td>
						 <td width="100" align="center" >
						<select name="season" id="season" size="1" >
						<option value="1" <% if(rsList.getString(15).equals("1")) { %>selected<% } %> >봄</option>
						<option value="2" <% if(rsList.getString(15).equals("2")) { %>selected<% } %> >여름</option>
						<option value="3" <% if(rsList.getString(15).equals("3")) { %>selected<% } %> >가을</option>
						<option value="4" <% if(rsList.getString(15).equals("4")) { %>selected<% } %> >겨울</option>
						</select>
						</td>
						<td width="50" align="center"><img src="/hanmaum/Image/co_4nobar07.gif" width="40" height="12" /></td>
						<td width="100" align="center"><input name="passwd" type="text" id="passwd" size="12" value="<%=rsList.getString(11)%>" /></td>
					</tr>
				</table>
			</td>
		</tr>
              <tr>
                <td bgcolor="#434343" height="2" colspan="8"></td>
            </tr>   
		<% } else { %>
            <tr>
				<td height="36" background="/hanmaum/Image/co_4nobar01.gif">
						<table>
						  <tr>
				              <td width="50" align="center"><img src="/hanmaum/Image/co_4nobar03.gif" alt="" width="21" height="12" /></td>
				              <td width="174" align="left"><input type="text" name="title" id="title" value="<%=rsList.getString(3)%>" /></td>
							  <td width="80" align="center"><img src="/hanmaum/Image/co_4nobar05.gif" alt="" width="31" height="12" /></td>
							  <td width="90" align="left"><input name="writer" type="text" id="writer" size="12" value="<%=rsList.getString(8)%>" /></td>
				              <td width="90" align="center"><img src="/hanmaum/Image/co_4nobar07.gif" width="40" height="12" /></td>
				              <td width="100" align="center"><input name="passwd" type="password" id="passwd" size="12" value="<%=rsList.getString(11)%>" /></td>
			            </tr>
		            </table>
	            </td>
            </tr>

              <tr>
                <td bgcolor="#434343" height="2" colspan="6"><input type="hidden" name="season" value="0" value="<%=rsList.getString(15)%>"  /></td>
            </tr>  
                  
		<% } %>           
          </table>
            <table width="684" border="0" cellspacing="0" cellpadding="0" class="fqtable">
              <tr>
                <td height="26" align="left">사진 업로드 1
                <input type="file" name="file_nm_new01" size="50" />
<%	if(rsList.getString(4).length()>2){
%>
<img src="./photo/<%= rsList.getString(4) %>" width="50" height="50" /><br /><br />
<%}
%></td>
              </tr>
              <tr>
                <td height="26" align="left">사진 업로드 2
                <input type="file" name="file_nm_new02" size="50" />
<%	if(rsList.getString(5).length()>2){
%>
<img src="./photo/<%= rsList.getString(5) %>" width="50" height="50" /><br /><br />
<%}
%></td>
              </tr>
              <tr>
                <td height="26" align="left">사진 업로드 3
                <input type="file" name="file_nm_new03" size="50" />
<%	if(rsList.getString(6).length()>2){
%>
<img src="./photo/<%= rsList.getString(6) %>" width="50" height="50" /><br /><br />
<%}
%></td>
              </tr>
              <tr>
                <td height="26" align="left"><font style="color:#F90;">이미지는 3장까지 가능하며 장당 5120KB 이하로 등록하실 수 있습니다.
                이미지 등록은 JPG, GIF, BMP만 가능합니다. </font></td>
              </tr>
              <tr>
                <td height="26" align="left" id="wr"><textarea name="article" id="article" cols="106" rows="10" style="background-color: #ffffff; border: #e2e2e2 1px solid ; overflow:-x; padding:10 10 10 10; font-size:8pt; color:#666666"><%=view_content%></textarea></td>
              </tr>
              <tr>
                <td bgcolor="#434343" height="2"></td>
              </tr>
          </table>
            <table width="684" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td height="50" align="right" valign="bottom">
						<a href="javascript:onSave();">
							<img src="/hanmaum/Image/co_but03.gif" alt="수정" width="68" height="31" border="0" />	
						</a>
						<a href="javascript:onDelete();">
							<img src="/hanmaum/Image/co_but05.gif" alt="삭제" width="68" height="31" border="0" />	
						</a>
						<a href="javascript:history.back();">
							<img src="/hanmaum/Image/co_but01.gif" alt="" width="68" height="31" border="0" />
						</a>
				</td>
              </tr>
          </table></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
</td>
</tr>
</table>
</form>





</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
<%
		}
		rs.close();
		ps.close();
		conn.close();

	} catch (Exception e) {
		
		if ( rs != null ) rs.close();
		if ( ps != null ) ps.close();
		if ( conn != null ) conn.close();
	}
%>