<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import ="bbs.BbsDAO" %>
<%@page import="user.UserDAO"%>
<%@ page import="java.io.File" %>
<%@ page import ="java.io.PrintWriter" %>
<%@ page import="java.io.IOException" %>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<% request.setCharacterEncoding("UTF-8"); %>
<jsp:useBean id="bbs" class="bbs.Bbs" scope="page"/>
<jsp:setProperty name="bbs" property="*"/>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TKmall</title>
</head>
<body>
			<% //상대경로를 이용한 파일 업로드
			String fileSave = "C:/Users/remil/바탕 화면/새 폴더/SW/src/main/webapp/produc";
			String real = application.getRealPath(fileSave);
			
			int max = 10*1024*1024;
			
			MultipartRequest mr = new MultipartRequest(request, fileSave, max, "utf-8", new DefaultFileRenamePolicy());
			
			
			//파일 데이터 저장			
			String filename = mr.getFilesystemName("bbpic");
			File file = new File(real + "/" + filename);
			
			String filename1 = mr.getFilesystemName("bbpic1");
			File file1 = new File(real + "/" + filename1);
			 %>

	<%
		String ID = null;
		if (session.getAttribute("ID") != null) {
			ID = (String) session.getAttribute("ID");
		}
		
		int USER_TYPE = 1;
		UserDAO u = new UserDAO();
		USER_TYPE = u.fmanager(ID);
		if(USER_TYPE != 0){
			PrintWriter script = response.getWriter();
			script.println("<script>");
			script.println("alert('권한 없음.')");
			script.println("location.href = 'login.jsp'");
			script.println("</script>");
		}else{
			String proname = mr.getParameter("proname");
	        String kind = mr.getParameter("kind");
	        String brand = mr.getParameter("brand");
	        int price = Integer.parseInt(mr.getParameter("price"));
	        int rprice = Integer.parseInt(mr.getParameter("rprice"));
	        String stype = new String();
	        stype = mr.getParameter("type");
	        String ex = mr.getParameter("ex");
	        
	        
	        
			if(kind == null || brand == null || proname == null || mr.getParameter("price") == null || mr.getParameter("rprice") == null || filename == null || filename1 == null){
					PrintWriter script = response.getWriter();
					script.println("<script>");
					script.println("alert( '입력이 안된 사항이 있습니다.')");
					script.println("history.back()");
					script.println("</script>");
				} else{
					BbsDAO b = new BbsDAO();
					int result = b.write(kind, brand, proname, price, rprice, ex, stype, filename, filename1); // 실제로 게시글 작성
					if(result == -1){	//이미 존재하는 아이디
						PrintWriter script = response.getWriter();
						script.println("<script>");
						script.println("alert('글쓰기에 실패했습니다.')");
						script.println("history.back()");
						script.println("</script>");
					}else {
						PrintWriter script = response.getWriter();
						script.println("<script>");
						script.println("location.href = 'main.jsp'");
						script.println("</script>");
					}
				}
		}
	%>
</body>
</html>