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.SupportInfo;
import admin.model.SupportMod;


/**
 * Servlet implementation class Support
 */
public class Support extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Support() {
        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="viewall";}
	    
	    if (rep.equals("viewall")){
	    	try {
				List<SupportInfo> spi = new SupportMod().getAllSupport();
				request.setAttribute("spi", spi);
				RequestDispatcher rd = request.getRequestDispatcher("admin/index.jsp?main=viewallsupport");
	            rd.forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    
	    if (rep.equals("edit")){
	    	String id = request.getParameter("id").trim();
	    	try {
				SupportInfo spi = new SupportMod().getOneSupport(id);
				request.setAttribute("spi", spi);
				RequestDispatcher rd = request.getRequestDispatcher("admin/index.jsp?main=editsupport");
	            rd.forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	
	    }
	    
	    if (rep.equals("update")){
	    	String id = request.getParameter("id").trim();
	    	String yahoo = request.getParameter("yahoo").trim();
	    	String name = request.getParameter("name").trim();
	    	String phone = request.getParameter("phone").trim();
	    	SupportInfo spi = new SupportInfo(id,yahoo,name,phone);
	    	new SupportMod().UpdateSupport(id, spi);
	    	response.sendRedirect("Support?rep=viewall");
	    }
	    
	    if (rep.equals("create")){
	    	try {
				if (new SupportMod().recordsnumber() >=5 ) {
					response.sendRedirect("admin/index.jsp?main=createsupport&sp=max");
				} else {
				String yahoo = request.getParameter("yahoo").trim();
				yahoo = yahoo.replaceAll("[']", "");
				String name = request.getParameter("name").trim();
				name = name.replaceAll("[']", "");
				String phone = request.getParameter("phone").trim();
				SupportInfo spi = new SupportInfo("1",yahoo,name,phone);
				new SupportMod().CreateSupport(spi);
				response.sendRedirect("Support?rep=viewall");
   }
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    
	    if (rep.equals("delid")){
	    	String id = request.getParameter("id").trim();
	    	new SupportMod().DeleteSupport(id);
	    	response.sendRedirect("Support?rep=viewall");
	    	
	    }
	    
		if (rep.equalsIgnoreCase("delchoise")){
			String idchoiced;
			for (int i =1; i < 11; i++){
				idchoiced = (String)request.getParameter("idchoiced"+i);
				if (idchoiced != null){
					new SupportMod().DeleteSupport(idchoiced);
				}
			}
			response.sendRedirect("Support?rep=viewall"); 
		}
	    
	    if (rep.equals("delall")){
	    	new SupportMod().DeleteAllSupport();
	    	response.sendRedirect("Support?rep=viewall");
	    }
	    
	}

}
