
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.SanPham;
import model.ThongTinSanPham;


public class getAllProduct extends HttpServlet {
	private static final long serialVersionUID = 2L;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String rep = request.getParameter("rep");
        if (rep == null){ rep = "show";}
        
        if(rep.equals("show")){
        try {

        	int records = new SanPham().recordsnumber();
        	int pagenum = records / 9;
        	if((records % 9) != 0) {
        		pagenum++;
        	}
        	String Trang= (String)request.getParameter("page");
        	if (Trang==null){Trang="1";}
        	int pages = Integer.parseInt(Trang);
        	if (pages < 1 || pages > pagenum){
        		pages = 1;
        	}

        	// Phan Trang
        	int offset = records - 9*pages ;
        	int limit = 9;
        	if (offset < 0 ){
        		offset = 0;
        		limit = records - 9*(pages-1);
        	}
        	// // Ket Thuc Phan Trang
        	List<ThongTinSanPham> sanpham = new SanPham().getAll(limit,offset);
        	
            request.setAttribute("sanpham", sanpham);
            request.setAttribute("TongSoTrang", pagenum);


            RequestDispatcher rd = request.getRequestDispatcher("index.jsp?main=allproduct&page="+Trang);
            rd.forward(request, response);
         }
           
           catch (SQLException ex) {
            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        }   
           finally {
            out.close();
        }
        }
        
        if (rep.equals("byprice")){
        	try {

            	int records = new SanPham().recordsnumber();
            	int pagenum = records / 9;
            	if((records % 9) != 0) {
            		pagenum++;
            	}
            	String Trang= (String)request.getParameter("page");
            	if (Trang==null){Trang="1";}
            	int pages = Integer.parseInt(Trang);
            	if (pages < 1 || pages > pagenum){
            		pages = 1;
            	}

            	// Phan Trang
            	int offset = records - 9*pages ;
            	int limit = 9;
            	if (offset < 0 ){
            		offset = 0;
            		limit = records - 9*(pages-1);
            	}
            	// // Ket Thuc Phan Trang
            	List<ThongTinSanPham> sanpham = new SanPham().getAllbyPrice(limit, offset);
            	
                request.setAttribute("sanpham", sanpham);
                request.setAttribute("TongSoTrang", pagenum);


                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?main=allproductbyprice&page="+Trang);
                rd.forward(request, response);
             }
               
               catch (SQLException ex) {
                Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
            }   	
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
