package idc.client.command;

import idc.model.IdcClientDBDTO;
import idc.model.IdcCmd;
import idc.model.IdcDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IdcClientListFormExcelCmd implements IdcCmd
{
	private IdcDAO dao;
	private int WRITE_PER_PAGE = 15;
	public IdcClientListFormExcelCmd()
	{
		dao = new IdcDAO();
	}
	//엑셀용 페이지
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String searchOption  = request.getParameter("searchOption");
		String searchContent = request.getParameter("searchContent");

		String so = getOption(searchOption, searchContent);
		if(searchContent==null || searchContent=="")
			searchContent="";
		
		int[] temp = getPageCnt(so);
		int pageCnt = temp[0];
		int numbering_end = temp[1];
		int curPage = getCurPage(request.getParameter("curPage"));
		int firstPage = getFirstPage(pageCnt, curPage);
		int endPage = getEndPage(firstPage, pageCnt);

		ArrayList<IdcClientDBDTO> list = new ArrayList<IdcClientDBDTO>();
		getList(list, so, curPage, numbering_end);
		
		
		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
		request.setAttribute("endPage", endPage);
		request.setAttribute("searchOption", searchOption);
		request.setAttribute("searchContent", searchContent);
		request.setAttribute("isClientDB", true);
		request.setAttribute("IdcClientDBList", list);
	}
		
	
	private String getOption(String searchOption, String sc)
	{
		String so="";
		if(searchOption!=null)
		{
			switch(searchOption)
			{
			case "client_code" :
				so="WHERE client_code like '%"+sc+"%' ";
				break;
			case "client_name" :
				so="WHERE client_name like '%"+sc+"%' ";
				break;
			case "client_reseller" :
				so="WHERE client_reseller like '%"+sc+"%' ";
				break;
			case "client_phone" :
				so="WHERE client_phone1 like '%"+sc+"%' OR client_phone2 like '%"+sc+"%'";
				break;
			case "client_mail" :
				so="WHERE client_email1 like '%"+sc+"%' OR client_email2 like '%"+sc+"%'";
				break;
			case "client_note" :
				so="WHERE client_note like '%"+sc+"%' ";
				break;
			}
		}
		return so;
	}
	private void getList(ArrayList<IdcClientDBDTO> list, String so, int curPage, int numbering_end)
	{
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null;
		ResultSet rs=null, rs2=null;
		int numbering = numbering_end - WRITE_PER_PAGE*(curPage-1);
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM clientdb "+so+" ORDER BY client_num DESC ";
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			sql="SELECT COUNT(*) as a FROM servicedb WHERE client_code=?";
			pstmt2=conn.prepareStatement(sql);
			while(rs.next())
			{
				IdcClientDBDTO dto = new IdcClientDBDTO();
				dto.setClient_num(numbering--);
				dto.setClient_code(rs.getString("client_code"));
				dto.setClient_email1(rs.getString("client_email1"));
				dto.setClient_email2(rs.getString("client_email2"));
				dto.setClient_name(rs.getString("client_name"));
				dto.setClient_note(rs.getString("client_note"));
				dto.setClient_phone1(rs.getString("client_phone1"));
				dto.setClient_phone2(rs.getString("client_phone2"));
				dto.setClient_receive_date(rs.getDate("client_receive_date"));
				dto.setClient_renew_date(rs.getDate("client_renew_date"));
				dto.setClient_reseller(rs.getString("client_reseller"));
				pstmt2.setString(1, dto.getClient_code());
				rs2=pstmt2.executeQuery();
				if(rs2.next())
				{
					if(rs2.getInt("a")>0)
					{
						dto.setClient_check("n");
					}
					else
						dto.setClient_check("y");
				}
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcClientListFormCmd 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
				if(rs2!=null)rs2.close();
				if(pstmt2!=null)pstmt2.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcClientListFormCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	private int getCurPage(String strPage)
	{
		int curPage=1;
		
		if(strPage !=null && strPage!="")
			curPage=Integer.parseInt(strPage);
		
		return curPage;
	}
	private int[] getPageCnt(String so)
	{
		int pageCnt=1;
		int[] result = new int[2];
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT COUNT(*) as a FROM clientdb "+so;
			pstmt =conn.prepareStatement(sql);
			System.out.println(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				pageCnt = rs.getInt("a")/WRITE_PER_PAGE+1;
				result[0] = pageCnt;
				result[1] = rs.getInt("a");
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcClientListFormCmd - pageCnt 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcClientListFormCmd - pageCnt  에서의 sqlexception");
				e.printStackTrace();
			}
		}
		return result;
	}
	
	private int getFirstPage(int pageCnt, int curPage)
	{
		int firstPage = 1;
		if(pageCnt-10>1)
			firstPage=pageCnt-10;
		
		int div =0;
		if(firstPage>=curPage)
			div = firstPage-curPage;
		firstPage-=div;
		
		return firstPage;
	}
	private int getEndPage(int firstPage, int pageCnt)
	{
		int endPage = pageCnt;
		
		int div = pageCnt-firstPage;
		if(div>10)
			endPage-=div-10;
		return endPage;
	}

}
