package control;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

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

import model.CommentMod;

/**
 * Servlet implementation class Comment
 */
public class Comment extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Comment() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html; charset=UTF-8");
	    String rep = request.getParameter("rep");
	    HttpSession session = request.getSession();
	    if (rep.equals("post")){	
	   String idpd = request.getParameter("idpd");
	   String user = (String) session.getAttribute("user");
	   if(user==null){user="Khách";}
	   String content = request.getParameter("content");
	   content = content.replaceAll("[']", "");
	   Date date = new Date();
	    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy 'lúc' HH:mm");
	    String cmdate = formatter.format(date); 
	    CommentMod cm = new CommentMod();
	    cm.CreateComment(idpd, cmdate, user, content);
	    
	    response.sendRedirect("ProductId?id="+idpd);
	    	
	    	
	    }
	    else {
	    	response.sendRedirect("index.jsp");
	    }
	}

}
