package idc.deposit.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.ReSendMailDBDTO;

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 IdcReSendMailListFormCmd implements IdcCmd
{
	IdcDAO dao  = new IdcDAO();
	int WRITE_PER_PAGE = 15;
	
	//재전송 리스트 폼
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String sc = null;
		sc = request.getParameter("searchContent")==null?"":request.getParameter("searchContent");
		int pageCnt = getPageCnt(sc);
		int curPage = getCurPage(request.getParameter("curPage"));
		int firstPage = getFirstPage(pageCnt, curPage);
		int endPage = getEndPage(firstPage, pageCnt);
		
		ArrayList<ReSendMailDBDTO> list = new ArrayList<ReSendMailDBDTO>();
		getList(list, sc, curPage);
		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
		request.setAttribute("endPage", endPage);
		request.setAttribute("searchContent", sc);
		request.setAttribute("list", list);
	}
	//리스트 얻어오기.
	private void getList(ArrayList<ReSendMailDBDTO> list, String sc, int curPage)
	{
		Connection conn=null;
		PreparedStatement pstmt[] = new PreparedStatement[2];
		ResultSet rs[]= new ResultSet[2];
		
		int numbering_end =  WRITE_PER_PAGE * (curPage -1);
		int numbering = 0;
		
		try	
		{
			conn=dao.getConn();
			String sql2="SELECT COUNT(*) as a FROM mailsave WHERE client_name like '%"+sc+"%' OR date like '%"+ sc +"%' LIMIT "+
							   numbering_end+","+WRITE_PER_PAGE;
			String sql="SELECT * FROM mailsave WHERE client_name like '%"+sc+"%' OR date like '%"+ sc +"%' ";
			sql+="ORDER BY inTime DESC LIMIT " + numbering_end + "," + WRITE_PER_PAGE;
			pstmt[1] = conn.prepareStatement(sql2);
			rs[1] = pstmt[1].executeQuery();
			if(rs[1].next())
			{
				numbering = rs[1].getInt("a");
			}
			
			pstmt[0] =conn.prepareStatement(sql);
			rs[0] = pstmt[0].executeQuery();
			while(rs[0].next())
			{
				ReSendMailDBDTO dto  = new ReSendMailDBDTO();
				
				dto.setNum(rs[0].getInt("num"));
				dto.setClient_name(rs[0].getString("client_name"));
				dto.setService_code(rs[0].getString("service_code"));
				dto.setDate(rs[0].getDate("date"));
				dto.setWebNum(numbering_end+numbering--);
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcReSendMailListFormCmd 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				for(int i=0;i<rs.length;i++)
				{
						if(rs[i]!=null)rs[i].close();
						if(pstmt[i]!=null)pstmt[i].close();
				}
			}
			catch (SQLException e) 
			{
				System.out.println("IdcReSendMailListFormCmd 에서의 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 sc)
	{
		int pageCnt=1;
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT COUNT(*) as a FROM mailsave WHERE client_name like '%"+sc+"%' OR date like '%"+ sc +"%'";
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
				pageCnt = rs.getInt("a")/WRITE_PER_PAGE+1;
		}
		catch (Exception e) 
		{
			System.out.println("IdcMemberListFormCmd - 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("IdcMemberListFormCmd - pageCnt  에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return pageCnt;
	}
	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;
	}
}
