package web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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

import service.UserOperatorManager;
import service.impl.UserOperatorManagerImpl;
import domain.Users;

public class UpdateUser extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Users user = createUser(request);
		UserOperatorManager uom = new UserOperatorManagerImpl();
		uom.modifyUser(user);
		List<Users> list= uom.findUserByProperty(user);
		Users u = list.get(0);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		StringBuffer sb = new StringBuffer();
		sb.append("<tr>");
		sb.append("<td>").append(u.getUserName()).append("</td>");
		sb.append("<td>").append(u.getUserPwd()).append("</td>");
		sb.append("<td>").append(u.getUserSex()).append("</td>");
		sb.append("<td>").append(u.getUserAge()).append("</td>");
		sb.append("<td>");
			sb.append("<a class='btn btn-info btn-xs' role='button' onclick=preUpdate('").append(u.getUserName()).append("')>更 新</a>");
			sb.append("<a class='btn btn-danger btn-xs' role='button' onclick=delUser('").append(u.getUserName()).append("')>删 除</a>");
		sb.append("</td>");
		sb.append("</tr>");
		out.print(sb.toString());
		out.flush();
		out.close();
	}
	
	private Users createUser(HttpServletRequest request){
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");
		String userSex = request.getParameter("userSex");
		String userAge = request.getParameter("userAge");
		Users user = new Users();
		user.setUserName(userName);
		user.setUserPwd(userPwd);
		user.setUserSex(userSex);
		user.setUserAge(userAge);
		return user;
	}

}
