package admin.control;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

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

import admin.model.BillMods;

import model.CommentInfo;
import model.CommentMod;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8"); 
	    String rep = request.getParameter("rep");
	    if (rep.equals("view")){
		//Comment
		String id = (String)request.getParameter("id");
    	String cmpagex = (String)request.getParameter("cmpage");
    	if (cmpagex==null){cmpagex ="1";}
    	int cmpage = Integer.parseInt(cmpagex);
        CommentMod cm = new CommentMod();   
        int TongRecords;
		try {
			TongRecords = cm.recordsComment(id);
	        int pagen = TongRecords / 9;
	        if((TongRecords % 9) != 0) {
	            pagen++;
	        }
	        if (cmpage < 1 || cmpage > pagen ){cmpage = 1;}
	        int limit = 10;
	        int offset = TongRecords - cmpage*10;
	        if (offset < 0){ offset = 0; limit = TongRecords - (cmpage-1)*10;} 
	               
	        List<CommentInfo> cminfo =  cm.getComment(id,limit,offset);
	        request.setAttribute("cmrecord", pagen);
	        request.setAttribute("cminfo", cminfo);
	        // Ket Thuc Comment
	        RequestDispatcher rd = request.getRequestDispatcher("admin/index.jsp?main=viewCM&id="+id+"&cmpage="+cmpage);
	        rd.forward(request, response);
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    }
	    
	    if (rep.equals("DelCm")){
	    	String idpd = request.getParameter("idpd");
			String id = (String)request.getParameter("id");
	    	String cmpagex = (String)request.getParameter("cmpage");
	    	new CommentMod().DeleteCM(id, idpd);
	    	response.sendRedirect("AComment?rep=view&id="+idpd+"&cmpage="+cmpagex);
	    }
	    
		if (rep.equalsIgnoreCase("delchoise")){
			String idchoiced;
			String idpd = request.getParameter("idpd");
			String Trang = (String)request.getParameter("page");
			for (int i =1; i < 11; i++){
				idchoiced = (String)request.getParameter("idchoiced"+i);
				if (idchoiced != null){
					new CommentMod().DeleteCM(idchoiced, idpd);
				}
			}
			response.sendRedirect("AComment?rep=view&id="+idpd+"&cmpage="+Trang); 
		}
	    
	    if (rep.equals("DelAllCm")){
	    	String idpd = request.getParameter("idpd");
	    	new CommentMod().DeleteAllCM(idpd);
	    	response.sendRedirect("admin/index.jsp?main=viewCM");
	}
	}

}
