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.BillInfo;
import model.User;
import model.UserMod;


/**
 * Servlet implementation class Bill
 */
public class Bill extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @throws SQLException 
     * @see HttpServlet#HttpServlet()
     */
    protected void BillProcess(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8");    	    
	    String rep = (String)request.getParameter("rep");
	    if(rep==null){rep="view";}
	    
	    if (rep.equals("view")){	    
	    int records = new BillMods().BillRecords();
	    int pagenum = records / 10;
    	if((records % 10) != 0) {
    		pagenum++;
    	}	    
      String Trang = (String)request.getParameter("page");
      String userinfo = (String)request.getParameter("userinfo");
      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<BillInfo> bills = new BillMods().getBill(limit,offset); 
	request.setAttribute("bills", bills);
    request.setAttribute("TongSoTrang", pagenum);
String url = "admin/index.jsp?main=billlist&page="+Trang;


    if (userinfo !=null){
        UserMod usermod = new UserMod();
        User us = usermod.getUserInfo(userinfo);         
        request.setAttribute("userinfo", us);
        url = "admin/index.jsp?main=billlist&page="+Trang+"&userinfo="+userinfo;
    }
    

    RequestDispatcher rd = request.getRequestDispatcher(url);
    rd.forward(request, response);
}

	if (rep.equals("status")) {
String id = (String)request.getParameter("id");
String stt = (String)request.getParameter("stt");
String Trang = (String)request.getParameter("page");
new BillMods().ChangeStatus(id, stt); 
response.sendRedirect("Bill?rep=view&page="+Trang);
}
	if (rep.equals("delid")) {
	String id = (String)request.getParameter("id");
	String Trang = (String)request.getParameter("page");
	if (id !=null){
	new BillMods().DeleteBill(id);
	
	response.sendRedirect("Bill?rep=view&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){
				new BillMods().DeleteBill(idchoiced);
			}
		}
		response.sendRedirect("Bill?rep=view&page="+Trang); 
	}
	
	if (rep.equals("delall")){		
		new BillMods().DeleteAllBill();
		response.sendRedirect("Bill?rep=view");
	}
	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
			try {
				BillProcess(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			BillProcess(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
