package admin.control;

import java.io.IOException;
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 admin.model.ProductMods;
import model.SanPham;
import model.ThongTinSanPham;

/**
 * Servlet implementation class ProductController
 */
public class ProductController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductController() {
        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 {
		request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html; charset=UTF-8");	    
	String rep = request.getParameter("rep");
	if(rep==null){rep="showAll";}
	
	if(rep.equals("createpd")){	
		String name = request.getParameter("name").trim();
		name = name.replaceAll("[']", "");
		String image = request.getParameter("image").trim();
		image = image.replaceAll("[']", "");
		String status = request.getParameter("status").trim();
		String cate = request.getParameter("cate");
		if(cate!=null){cate = cate.replaceAll("[']", "").trim();}
		String newcate = request.getParameter("newcate");		
		if(newcate != null){
			newcate = newcate.replaceAll("[']", "");
			newcate = newcate.replaceAll("[%]", "");
			cate=newcate;		
		}
		
		double price = Double.parseDouble((String)request.getParameter("price"));
		String des = request.getParameter("des");
		ThongTinSanPham sp = new ThongTinSanPham(1, name, image, price, des, cate, status);
		
		ProductMods pdm = new ProductMods();
		pdm.CreateProduct(sp); 
		
		
		response.sendRedirect("ProductController?rep=showAll");
	}
	if(rep.equals("edit")){
		 try {
	        	String id = (String)request.getParameter("id");
	        	String page= request.getParameter("page");
	            SanPham c = new SanPham();
	            ThongTinSanPham sanpham = c.productId(Integer.parseInt(id));                 
	            request.setAttribute("sanpham", sanpham);
	            
	            RequestDispatcher rd = request.getRequestDispatcher("admin/index.jsp?main=editpd&?id="+id+"&page="+page);
	            rd.forward(request, response);
	        }
	        	
	        	
	        catch (SQLException ex) {
	            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
	        }
	}
	if(rep.equals("update")){
		String id = request.getParameter("id");
		String page= request.getParameter("page");
		if (id !=null){
		String name = request.getParameter("name").trim();
		name = name.replaceAll("[']", "");
		String image = request.getParameter("image");
		image = image.replaceAll("[']", "");
		String cate = request.getParameter("cate").trim();
		cate = cate.replaceAll("[']", "");
		double price = Double.parseDouble((String)request.getParameter("price"));
		String des = request.getParameter("des").trim();
		String status = request.getParameter("status").trim();
		ThongTinSanPham sp = new ThongTinSanPham(1,name,image,price,des,cate, status);		
		ProductMods pdm = new ProductMods();
		pdm.EditProduct(id, sp);
		
		response.sendRedirect("ProductController?rep=showAll&page="+page);
	}
	}
	
	if(rep.equals("showAll")){
		try {

        	int records = new SanPham().recordsnumber();
        	int pagenum = records / 10;
        	if((records % 10) != 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 - 10*pages ;
        	int limit = 10;
        	if (offset < 0 ){
        		offset = 0;
        		limit = records - 10*(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("/admin/index.jsp?main=allproduct&page="+Trang);
            rd.forward(request, response);
         }
           
           catch (SQLException ex) {
            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        } 
	}
	
	if(rep.equals("showpdso")){
		try {
        	int records = new SanPham().recordsnumberPDSO();
        	int pagenum = records / 10;
        	if((records % 10) != 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 - 10*pages ;
        	int limit = 10;
        	if (offset < 0 ){
        		offset = 0;
        		limit = records - 10*(pages-1);
        	}
        	// // Ket Thuc Phan Trang
        	List<ThongTinSanPham> sanpham = new SanPham().getAllPDSO(limit,offset);
        	
            request.setAttribute("sanpham", sanpham);
            request.setAttribute("TongSoTrang", pagenum);


            RequestDispatcher rd = request.getRequestDispatcher("/admin/index.jsp?main=allproductso&page="+Trang);
            rd.forward(request, response);
         }
           
           catch (SQLException ex) {
            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        } 
	}
	
	if(rep.equals("editpdso")){
		 try {
	        	String id = (String)request.getParameter("id");
	        	String page= request.getParameter("page");
	            SanPham c = new SanPham();
	            ThongTinSanPham sanpham = c.productId(Integer.parseInt(id));                 
	            request.setAttribute("sanpham", sanpham);
	            
	            RequestDispatcher rd = request.getRequestDispatcher("admin/index.jsp?main=editpdso&?id="+id+"&page="+page);
	            rd.forward(request, response);
	        }
	        	
	        	
	        catch (SQLException ex) {
	            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
	        }
	}
	
	if(rep.equals("updatepdso")){
		String id = request.getParameter("id");
		String page= request.getParameter("page");
		if (id !=null){
		String name = request.getParameter("name").trim();
		name = name.replaceAll("[']", "");
		String image = request.getParameter("image");
		image = image.replaceAll("[']", "");
		String cate = request.getParameter("cate").trim();
		cate = cate.replaceAll("[']", "");
		double price = Double.parseDouble((String)request.getParameter("price"));
		String des = request.getParameter("des").trim();
		String status = request.getParameter("status").trim();
		ThongTinSanPham sp = new ThongTinSanPham(1,name,image,price,des,cate, status);		
		ProductMods pdm = new ProductMods();
		pdm.EditProduct(id, sp);
		
		response.sendRedirect("ProductController?rep=showpdso&page="+page);
	}
	}
	if(rep.equals("delidso")){
		
		String id = request.getParameter("id");
		String page= request.getParameter("page");
		if (id != null){
		ProductMods pdm = new ProductMods();
		pdm.DeleteProduct(id);		
		response.sendRedirect("ProductController?rep=showpdso&page="+page);
        }
				
	}
	if(rep.equals("delallpdso")){
		ProductMods pdm = new ProductMods();
		pdm.DeleteAllProductSO();
		response.sendRedirect("ProductController?rep=showpdso");
        }
	
	if(rep.equals("delid")){
		
		String id = request.getParameter("id");
		String page= request.getParameter("page");
		if (id != null){
		ProductMods pdm = new ProductMods();
		pdm.DeleteProduct(id);		
		response.sendRedirect("ProductController?rep=showAll&page="+page);
        }
				
	}
	
	if (rep.equalsIgnoreCase("delchoisepdso")){
		String idchoiced;
		String Trang = (String)request.getParameter("page");
		for (int i =1; i < 11; i++){
			idchoiced = (String)request.getParameter("idchoiced"+i);
			if (idchoiced != null){
				ProductMods pdm = new ProductMods();
				pdm.DeleteProduct(idchoiced);						
			}
		}
		response.sendRedirect("ProductController?rep=showpdso&page="+Trang);
	}
	
	if (rep.equalsIgnoreCase("delchoise")){
		String idchoiced;
		String Trang = (String)request.getParameter("page");
		for (int i =1; i < 11; i++){
			idchoiced = (String)request.getParameter("idchoiced"+i);
			if (idchoiced != null){
				ProductMods pdm = new ProductMods();
				pdm.DeleteProduct(idchoiced);	
			}
		}
		response.sendRedirect("ProductController?rep=showAll&page="+Trang); 
	}
	
	if(rep.equals("deleteall")){
		ProductMods pdm = new ProductMods();
		pdm.DeleteAllProduct();
		response.sendRedirect("ProductController?rep=showAll");
        }

	if(rep.equals("viewID")){
		String id = (String)request.getParameter("id");
        SanPham c = new SanPham();
        ThongTinSanPham sanpham;
		try {
			sanpham = c.productId(Integer.parseInt(id));
	        request.setAttribute("sanpham", sanpham);
	        RequestDispatcher rd = request.getRequestDispatcher("admin/index.jsp?main=viewpd-ID");
            rd.forward(request, response);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}                 		
	}
	
	
	}

}
