package idc.deposit.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcPaymentListDTO;

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 IdcPaymentListFormCmd implements IdcCmd
{
	private int WRITE_PER_PAGE=10;
	private IdcDAO dao;
	public IdcPaymentListFormCmd()
	{
		dao = new IdcDAO();
	}
	//결제내역
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String sc = request.getParameter("searchContent");
		if(sc==null)
			sc="";
		int pageCnt = getPageCnt(sc);
		int curPage = getCurPage(request.getParameter("curPage"));
		int firstPage = getFirstPage(pageCnt);
		ArrayList<IdcPaymentListDTO> list = new ArrayList<IdcPaymentListDTO>();

		getList(list, curPage, sc);
		
		request.setAttribute("paymentList", list);
		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
		request.setAttribute("searchContent", sc);
	}
	//결제내역 받아오기.
	private void getList(ArrayList<IdcPaymentListDTO> list, int curPage, String sc)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM paymentlist WHERE client_name like '%"+sc+"%' ";
			sql+="ORDER BY num DESC LIMIT ?,?";
			System.out.println(sql);
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, WRITE_PER_PAGE*(curPage-1));
			pstmt.setInt(2, WRITE_PER_PAGE);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				IdcPaymentListDTO dto = new IdcPaymentListDTO();
				dto.set_case(rs.getString("_case"));
				dto.setCode(rs.getString("service_code"));
				dto.setDate(rs.getDate("payment_date"));
				dto.setName(rs.getString("client_name"));
				dto.setPayment(rs.getString("payment"));
				list.add(dto);
			}
			
		}
		catch (Exception e) 
		{
			System.out.println("IDCLINEIPWRITECMD - getList 에서의 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("IDCLINEIPWRITECMD - getList 에서의 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 paymentlist 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("IdcPaymentListFormCmd - 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("IdcPaymentListFormCmd - pageCnt  에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return pageCnt;
	}
	private int getFirstPage(int pageCnt)
	{
		int firstPage = 1;
		if(pageCnt-10>1)
			firstPage=pageCnt-10;
		return firstPage;
	}

}
