package idc.service.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcDeleteServiceFormDTO;

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;

import utility.IdcGetClientNameCode;

public class IdcDeleteServiceListFormCmd implements IdcCmd
{
	private IdcDAO dao;
	private int WRITE_PER_PAGE=20;
	
	public IdcDeleteServiceListFormCmd()
	{
		dao = new IdcDAO();
	}
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String sc = getSC(request.getParameter("searchContent"));
		int pageCnt = getPageCnt(sc);
		int curPage = getCurPage(request.getParameter("curPage"));
		int firstPage = getFirstPage(pageCnt);
		ArrayList<IdcDeleteServiceFormDTO> list = new ArrayList<IdcDeleteServiceFormDTO>();
		
		getDeleteServiceList(list, sc, curPage);
		
		request.setAttribute("DeleteServiceList", list);
		request.setAttribute("searchContent", sc);
		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
	}
	
	//해지리스트 받아오기
	private void getDeleteServiceList(ArrayList<IdcDeleteServiceFormDTO> list, String sc, int curPage)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		String code = IdcGetClientNameCode.getClientCode(sc);
		if(code==null)
			code="";
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM deleteservicedb WHERE client_code like '%"+code+"%' ";
			sql+="ORDER BY service_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())
			{
				IdcDeleteServiceFormDTO dto = new IdcDeleteServiceFormDTO();
				dto.setAmount(Integer.parseInt(rs.getString("service_amount")));
				dto.setCancel_date(rs.getDate("service_cancel_date"));
				dto.setClient_name(IdcGetClientNameCode.getClientName(rs.getString("client_code")));
				dto.setNonpayment(Integer.parseInt(rs.getString("service_nonpayment")));
				dto.setOpen_date(rs.getDate("service_open_date"));
				dto.setPayment(dto.getAmount()-dto.getNonpayment());
				dto.setPayment_date(rs.getDate("service_payment_date"));
				dto.setRequest_date(rs.getDate("service_request_date"));
				dto.setServer_code(rs.getString("server_code"));
				dto.setService_code(rs.getString("service_code"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcDeleteServiceListForm - getDeleteServiceList 에서의 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("IdcDeleteServiceListForm - getDeleteServiceList 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
		
	private String getSC(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 deleteservicedb WHERE client_code 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("IdcDeleteServiceListForm - 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("IdcDeleteServiceListForm - pageCnt  에서의 sqlexception");
				e.printStackTrace();
			}
		}
		return pageCnt;
	}
	
	private int getFirstPage(int pageCnt){
		int firstPage = 1;
		if(pageCnt-10>1)
			firstPage=pageCnt-10;
		return firstPage;
	}
	
	
}