package com.wxpn.tutorial.ec.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wxpn.tutorial.db.ConnectionPool;
import com.wxpn.tutorial.db.DB;
import com.wxpn.tutorial.ec.bean.ECMember;
import com.wxpn.tutorial.ec.bean.ECMemberMgr;

/**
 * Servlet implementation class for Servlet: RegisterServlet
 */
public class RegisterServlet extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public RegisterServlet() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=GB2312");
		String error = "";

		// 必须填写的项目
		String username = request.getParameter("logname");
		String realname = request.getParameter("realname");
		String passwd1 = request.getParameter("passwd1");
		String passwd2 = request.getParameter("passwd2");
		String email = request.getParameter("email");
		String gender = request.getParameter("Gender");
		String random = request.getParameter("random");
		String random2 = request.getSession().getAttribute("random").toString();
		if (!random.equals(random2)) {
			error = "输入的随机码不正确！";
			request.setAttribute("error", error);
			try {
				request.getRequestDispatcher("/member/reg_failure.jsp").forward(
						request, response);
			} catch (Throwable t) {
				getServletContext().log(t.getMessage());
			}
		}
		// 非必须填写的项目
		String phone = request.getParameter("phone");
		String problem = request.getParameter("problem");
		String answer = request.getParameter("answer");
		String province = request.getParameter("Province");
		String education = request.getParameter("education");
		String[] hobbies = request.getParameterValues("hobbies");
		String hobby = "";
		if (hobbies != null) {
			for (int i = 0; i < hobbies.length; i++) {
				hobby = hobby + hobbies[i];
			}
		}
		String selfintro = request.getParameter("selfintro");

		try {

			ECMemberMgr mgr = new ECMemberMgr();
			ECMember member = mgr.get(username);

			if (member != null) {
				// 如果记录集为非空，表明有相匹配的用户名，注册失败：
				error = "已经存在相同的用户名，请更换一个新用户名";

				request.setAttribute("error", error);

				try {
					request.getRequestDispatcher("/member/reg_failure.jsp").forward(
							request, response);
				} catch (Throwable t) {
					getServletContext().log(t.getMessage());
				}
			} else {
				member = new ECMember();
				member.setEducation(education);
				member.setEmail(email);
				member.setGender(Integer.parseInt(gender));
				member.setHobbies(hobby);
				member.setPassword(passwd1);
				member.setPhone(phone);
				member.setProvince(province);
				member.setPwd_answer(answer);
				member.setPwd_problem(problem);
				member.setRealname(realname);
				member.setSelfintro(selfintro);
				member.setUsername(username);
				int i = mgr.add(member);
				// 注册成功
				request.setAttribute("user", member);
				try {
					request.getSession(true).setAttribute("username", username);
					request.getRequestDispatcher("/member/reg_success.jsp")
							.forward(request, response);
				} catch (Throwable t) {
					getServletContext().log(t.getMessage());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}