package idc;

import idc.business.command.IdcBusinessDeliveryHandleCmd;
import idc.business.command.IdcBusinessGEModifyCmd;
import idc.business.command.IdcBusinessHandlerCmd;
import idc.business.command.idcworksheetMKCmd;
import idc.business.command.IdcBusinessHandoverUpdateCmd;
import idc.business.command.IdcBusinessHandoverWriteCmd;
import idc.business.command.IdcBusinessHandoverWriteHandleCmd;
import idc.business.command.IdcBusinessListCmd;
import idc.business.command.IdcBusinessModifyCmd;
import idc.business.command.IdcBusinesshandoverListCmd;
import idc.client.command.IdcClientDBListCmd;
import idc.model.IdcCmd;

import java.io.IOException;

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

/**
 * Servlet implementation class IdcBusinessFrontController
 */
@WebServlet("*.bus")
public class IdcBusinessFrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String BUSINESS_PATH="/jsp/business/";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdcBusinessFrontController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control","no-Cache");
		response.setHeader("Cache-Control","no-store");
		response.setContentType("charset='utf-8'");
		
		request.setCharacterEncoding("UTF-8");
		
		String requestURI = request.getRequestURI();
		String contextPath = request.getContextPath();
		String cmdURI = requestURI.substring(contextPath.length());
		
		IdcCmd cmd = null;
		String viewPage = null;
		
		Object obj=request.getSession().getAttribute("member");
		if(obj!=null)
		{
			//비즈니스 리스트.
			if(cmdURI.equals("/businessList.bus")){
				cmd = new IdcBusinessListCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",BUSINESS_PATH+"businessList.jsp");
				viewPage="DefaultPage.jsp";
			}
			//업무작성
			if(cmdURI.equals("/businessWrite.bus")){
				cmd = new IdcBusinessHandlerCmd();
				cmd.execute(request, response);
				viewPage="businessList.bus";
			}
			
			//전달사항 쓰기
			if(cmdURI.equals("/businessDeliveryWrite.bus")){
				cmd = new IdcBusinessDeliveryHandleCmd();
				cmd.execute(request, response);
				viewPage = "businessList.bus";			
			}
			
	//		진행상황 수정
			if(cmdURI.equals("/businessModify.bus")){
				cmd = new IdcBusinessModifyCmd();
				cmd.execute(request, response);
				viewPage = "businessList.bus";			
			}
	//		금일상황 수정
			if(cmdURI.equals("/businessGEModify.bus")){
				cmd = new IdcBusinessGEModifyCmd();
				cmd.execute(request, response);
				viewPage = "businessList.bus";			
			}
			//인수인계 작성폼으로가기
			if (cmdURI.equals("/handoverWrite.bus"))
			{
				cmd = new IdcBusinessHandoverWriteCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile", BUSINESS_PATH + "handoverWrite.jsp");
				viewPage = "DefaultPage.jsp";
			}
	
			// 인수인계 작성처리
			if (cmdURI.equals("/handoverWriteHandle.bus"))
			{
				cmd = new IdcBusinessHandoverWriteHandleCmd();
				cmd.execute(request, response);
				viewPage = "handoverList.bus";
			}
	
			// 업무 인수 사항 리스트
			if (cmdURI.equals("/handoverList.bus"))
			{
				cmd = new IdcBusinesshandoverListCmd();
				cmd.execute(request, response);
				// viewPage = "/businessList.jsp";
				request.setAttribute("TargetFile", BUSINESS_PATH + "handoverList.jsp");
				viewPage = "DefaultPage.jsp";
			}
	
			//업무 인수 리스트 수정 처리
			if (cmdURI.equals("/handoverUpdate.bus")){
				cmd = new IdcBusinessHandoverUpdateCmd();
				cmd.execute(request, response);
				viewPage = "handoverList.bus";
			}
			
	
			//테스트용 기본페이지.
			if(cmdURI.equals("/DefaultPage.cli"))
			{
				cmd = new IdcClientDBListCmd(true);
				cmd.execute(request,response);
				request.setAttribute("TargetFile",BUSINESS_PATH+"IdcCuponList.jsp");
				viewPage="DefaultPage.jsp";
			}		
			
			//MK
			if(cmdURI.equals("/worksheetMK.bus")){
				cmd = new idcworksheetMKCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",BUSINESS_PATH+"worksheetMK.jsp");
				viewPage="DefaultPage.jsp";
			}			
			
		}
		else
		{
			request.setAttribute("TargetFile", "jsp/main/IdcLoginForm.jsp");
			viewPage="DefaultPage.jsp";
		}
		RequestDispatcher dis = request.getRequestDispatcher(viewPage);
		dis.forward(request, response);
	}

}
