<%@page import="user.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import ="user.UserDAO" %>
<%@ page import ="java.io.PrintWriter" %>
<% request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>DoubleSS</title>
</head>
<body>
	<%
		String ID = null;
		if(session.getAttribute("userID") != null){
			ID = (String) session.getAttribute("userID");
		}
		if(ID != null){
			PrintWriter script = response.getWriter();
			script.println("<script>");
			script.println("alert('이미 로그인이 되어있습니다.')");
			script.println("location.href = 'main.jsp'");
			script.println("</script>");
		}
		
		String id = request.getParameter("ID");
		String password = request.getParameter("password");
		String repassword = request.getParameter("repassword");
		if(password.equals(repassword)){
			
		}else{
			PrintWriter script = response.getWriter();
			script.println("<script>");
			script.println("alert( '비밀번호가 다릅니다..')");
			script.println("history.back()");
			script.println("</script>");
		}
		String name = request.getParameter("name");
		int birth = Integer.parseInt(request.getParameter("birth"));
		String email = request.getParameter("email");
		int gender = Integer.parseInt(request.getParameter("gender"));
		String phone_number = request.getParameter("pnum1") + request.getParameter("pnum2") + request.getParameter("pnum3");
		int postnum = Integer.parseInt(request.getParameter("postnum"));
		String address = request.getParameter("address");
		String d_address =request.getParameter("d_address");
		
		System.out.println(id+password+name+birth+gender+phone_number+postnum+address+d_address);
		
		if(id == null || password == null || name == null || email == null || request.getParameter("birth") == null || request.getParameter("gender") == null
				|| phone_number == null || request.getParameter("postnum") == null || address == null || request.getParameter("d_address") == null){
			PrintWriter script = response.getWriter();
			script.println("<script>");
			script.println("alert( '입력이 안된 사항이 있습니다.')");
			script.println("history.back()");
			script.println("</script>");
		} else{
			UserDAO userDAO = new UserDAO();
			int result = userDAO.join(id, password, name, postnum, address, d_address, phone_number, birth, gender, email); //아이디ㄱ 패스워드 넘기기
			if(result == -1){	//이미 존재하는 아이디
				PrintWriter script = response.getWriter();
				script.println("<script>");
				script.println("alert('이미 존재하는 아이디입니다.')");
				script.println("history.back()");
				script.println("</script>");
			}else {
				session.setAttribute("userID", id);
				PrintWriter script = response.getWriter();
				script.println("<script>");
				script.println("location.href = 'main.jsp'");
				script.println("</script>");
			}
		}
	%>
</body>
</html>