package idc.main.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcMemberDBDTO;

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 IdcMemberListFormCmd implements IdcCmd 
{
	IdcDAO dao;
	private int WRITE_PER_PAGE = 20;
	public IdcMemberListFormCmd() 
	{
		dao = new IdcDAO();
	}
	
	//회원보기 (운영자 제한)
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		ArrayList<IdcMemberDBDTO> list = new ArrayList<IdcMemberDBDTO>();
		
		int pageCnt = getPageCnt();
		int curPage = getCurPage(request.getParameter("curPage"));
		int firstPage = getFirstPage(pageCnt);

		idcMemberListFormCmd(list,curPage);
		request.setAttribute("memberList", list);
		
		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
	}
	private int getCurPage(String strPage)
	{
		int curPage=1;
		
		if(strPage !=null && strPage!="")
			curPage=Integer.parseInt(strPage);
		
		return curPage;
	}
	private int getPageCnt()
	{
		int pageCnt=1;
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT COUNT(*) as a FROM memberdb";
			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 firstPage = 1;
		if(pageCnt-10>1)
			firstPage=pageCnt-10;
		return firstPage;
	}
	private void idcMemberListFormCmd(ArrayList<IdcMemberDBDTO> list, int curPage)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM memberdb ORDER BY join_date DESC LIMIT ?,?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, (curPage-1)*WRITE_PER_PAGE);
			pstmt.setInt(2, WRITE_PER_PAGE);
			
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				IdcMemberDBDTO dto = new IdcMemberDBDTO();
				dto.setId(rs.getString("id"));
				dto.setName(rs.getString("name"));
				dto.setNum(rs.getString("num"));
				dto.setRank(rs.getString("rank"));
				dto.setPhone(rs.getString("phone"));
				dto.setLocation(rs.getString("location"));
				dto.setPower(rs.getInt("power"));
				dto.setJoin_date(rs.getDate("join_date"));
				dto.setNote(rs.getString("note"));
				dto.setPower1(rs.getString("power1"));
				dto.setPower2(rs.getString("power2"));
				dto.setPower3(rs.getString("power3"));
				dto.setPower4(rs.getString("power4"));
				dto.setPower5(rs.getString("power5"));
				dto.setPower6(rs.getString("power6"));
				dto.setPower7(rs.getString("power7"));
				dto.setPower8(rs.getString("power8"));
				dto.setPower9(rs.getString("power9"));
				dto.setPower10(rs.getString("power10"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("idcMemberListFormCmd 에서의 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 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
