package idc.line.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcIPDBDTO;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IdcIPModifyCmd implements IdcCmd
{
	private IdcDAO dao;
	
	public IdcIPModifyCmd()
	{
		dao = new IdcDAO();
	}
	
	//ip수정하기.
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		//checkToken 값이 다르면 true반환.
		if(dao.checkToken(request))
		{
			request.setAttribute("reError", dao.reError);
			return;
		}
		else
		{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
		
		
		IdcIPDBDTO dto = setdto(request);
		setDB(dto);
	}
	
	//db설정
	private void setDB(IdcIPDBDTO dto)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="UPDATE ipdb SET line_name=?, ip_case2=?, ip_user=?, ip_address=?, ip_able=?, ip_before_user=?, ";
			sql+="cancel_reason=?, block=?, block_endday=?, ip_note=? ";
			sql+="WHERE ip_address=?";
			pstmt =conn.prepareStatement(sql);
			
			int index=1;
			
			pstmt.setString(index++, dto.getLine_name());
			pstmt.setString(index++, dto.getIp_case2());
			pstmt.setString(index++, dto.getIp_user());
			pstmt.setString(index++, dto.getIp_address());
			pstmt.setInt(index++, dto.getIp_able());
			pstmt.setString(index++, dto.getIp_before_user());
			pstmt.setString(index++, dto.getCancel_reason());
			pstmt.setString(index++, dto.getBlock());
			pstmt.setDate(index++, dto.getBlock_endday());
			pstmt.setString(index++, dto.getIp_note());
			pstmt.setString(index++, dto.getIp_address());
			
			pstmt.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("IdcIPModifyCmd - setDB 에서의 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("IdcIPModifyCmd - setDB 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	private IdcIPDBDTO setdto(HttpServletRequest request)
	{
		IdcIPDBDTO dto = new IdcIPDBDTO();
		System.out.println("????");
		dto.setLine_name(request.getParameter("line_name"));
		System.out.println(request.getParameter("line_name"));
		dto.setIp_case2(request.getParameter("ip_case2"));
		dto.setIp_user(request.getParameter("ip_user"));
		dto.setIp_address(request.getParameter("ip_address"));
		if(request.getParameter("ip_able").equals("1"))
			dto.setIp_able(1);
		else
			dto.setIp_able(0);
		
		dto.setIp_before_user(request.getParameter("ip_before_user"));
		dto.setCancel_reason(request.getParameter("cancel_reason"));
		dto.setBlock(request.getParameter("block"));
		String d = 	request.getParameter("block_endday");
		if(d!=null && d!="")
			dto.setBlock_endday(Date.valueOf(d));
		dto.setIp_note(request.getParameter("ip_note"));
		
		return dto;
	}
}
