package idc.service.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcDeleteSubDBDTO;

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 IdcDeleteSubFormCmd implements IdcCmd
{
	IdcDAO dao = new IdcDAO();
	private int WRITE_PER_PAGE=20;
	
	
	public IdcDeleteSubFormCmd()
	{
		dao = new IdcDAO();
	}
	
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String sc = getSearchContent(request.getParameter("searchContent"));
		ArrayList<IdcDeleteSubDBDTO> list = new ArrayList<IdcDeleteSubDBDTO>();
		
		int pageCnt = getPageCnt(sc);
		int curPage = getCurPage(request.getParameter("curPage"));
		int firstPage = getFirstPage(pageCnt);
		
		getDeleteSubList(list, sc, curPage);
		
		request.setAttribute("DeleteSubList", list);
		request.setAttribute("searchContent", sc);
		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
	}
	
	//삭제한 서비스 항목 불러오기
	private void getDeleteSubList(ArrayList<IdcDeleteSubDBDTO> list, String sc, int curPage)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM deletesub WHERE client_name like '%"+sc+"%' ";
			sql+="ORDER BY num DESC LIMIT ?,?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, WRITE_PER_PAGE*(curPage-1));
			pstmt.setInt(2, WRITE_PER_PAGE);
			
			rs=pstmt.executeQuery();
			
			while(rs.next())
			{
				IdcDeleteSubDBDTO dto = new IdcDeleteSubDBDTO();
				
				dto.setBehavior(rs.getString("behavior"));
				dto.setServer_code(rs.getString("server_code"));
				dto.setCancel_date(rs.getDate("cancel_date"));
				dto.setClient_name(rs.getString("client_name"));
				dto.setNote(rs.getString("note"));
				dto.setPayment_Status(rs.getString("payment_Status"));
				dto.setRequest_date(rs.getDate("request_date"));
				dto.setService_code(rs.getString("service_code"));
				dto.setAmount(rs.getInt("amount"));
				dto.setPayment(rs.getInt("payment"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcDeleteSubFormCmd - getDeleteSub 에서의 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("IdcDeleteSubFormCmd - getDeleteSub 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	private String getSearchContent(String sc)
	{
		if(sc=="null" || sc==null)
			sc="";
		return sc;
	}
	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 deletesub WHERE client_name 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("IdcDeleteSubFormCmd - 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("IdcDeleteSubFormCmd - pageCnt  에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return pageCnt;
	}
	private int getFirstPage(int pageCnt)
	{
		int firstPage = 1;
		if(pageCnt-10>1)
			firstPage=pageCnt-10;
		return firstPage;
	}
}