package idc.client.command;

import javax.servlet.http.*;

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

import idc.model.*;

public class IdcClientDBListCmd implements IdcCmd
{
	private boolean isClientDB;
	private IdcDAO dao;
	public IdcClientDBListCmd(boolean b)
	{
		isClientDB = b;
		dao = new IdcDAO();
	}
	
	//고객정보 가져오기.
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		ArrayList<IdcClientDBDTO> list;
		String searchOption  = request.getParameter("searchOption");
		String searchContent = request.getParameter("searchContent");
		
		int curPage = getCurPage(request.getParameter("curPage"));
		int pageCnt = getPageCnt(searchOption, searchContent);

		if(searchContent!=null && searchContent!="")
		{
			list = idcClientDBListSearch(searchOption,searchContent);
		}
		else
		{
			list = dao.idcClientDBList(Integer.toString(curPage), isClientDB);
			pageCnt = dao.idcClientDBPageCnt(isClientDB);
			searchOption="client_code";
		}
		
		int firstPage = getFirstPage(pageCnt);
		
		request.setAttribute("IdcClientDBList", list);
		request.setAttribute("isClientDB",isClientDB);
		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
		request.setAttribute("searchOption",searchOption);
		request.setAttribute("searchContent", searchContent);

		String searchOptionName="고객코드";
		switch(searchOption)
		{
			case "client_code":searchOptionName="고객코드";break;
			case "client_name":searchOptionName="고객명";break;
			case "client_reseller":searchOptionName="리셀러";break;
		}
		request.setAttribute("searchOptionName",searchOptionName);
	}
	
	
	
	private int getCurPage(String strPage)
	{
		int curPage=1;
		
		if(strPage !=null && strPage!="")
			curPage=Integer.parseInt(strPage);
		
		return curPage;
	}
	private int getPageCnt(String op, String co)
	{
		int pageCnt=1;
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT COUNT(*) as a FROM clientdb WHERE "+op+" like '%"+co+"%'";
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
				pageCnt = rs.getInt("a")/dao.WRITING_PER_PAGE+1;
		}
		catch (Exception e) 
		{
			System.out.println("IdcClientDBListCmd - 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("IdcClientDBListCmd - pageCnt  에서의 sqlexception");
				e.printStackTrace();
			}
		}
		return pageCnt;
	}
	private int getFirstPage(int pageCnt)
	{
		int firstPage = 1;
		if(pageCnt-10>1)
			firstPage=pageCnt-10;
		return firstPage;
	}
	private ArrayList<IdcClientDBDTO> idcClientDBListSearch(String op, String co)
	{
		ArrayList<IdcClientDBDTO> list = new ArrayList<IdcClientDBDTO>();
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM clientdb WHERE "+op+" like '%"+co+"%'";
//			System.out.println(sql);
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				IdcClientDBDTO dto = new IdcClientDBDTO();
				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"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("idcClientDBListSearch 에서의 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("idcClientDBListSearch 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return list;
	}
}
