package idc.business.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcWorkDTO;

import java.io.UnsupportedEncodingException;
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 IdcBusinessGEModifyCmd implements IdcCmd
{
	private IdcDAO dao;
	
	public IdcBusinessGEModifyCmd(){
		dao = new IdcDAO();
	}
	
	@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");
		}
		
		int num = Integer.parseInt(request.getParameter("modiNum"));
		String status = request.getParameter("status");
		String[] d = request.getParameterValues("data");
		
		//try{
		//	for(int i=0;i<d.length;i++){	
		//			d[i] = new String(d[i].getBytes("8859_1"),"utf-8");
		//	}
		//}catch (UnsupportedEncodingException e){
		//	e.printStackTrace();
		//}
		
		//데이터 업데이트
		setUpdate(num, status, d);
	}
	
	private void setUpdate(int num, String status, String[] d){
		
		IdcWorkDTO dto = new IdcWorkDTO();
		
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null, pstmt3=null;
		ResultSet rs=null;
		try{
			boolean t = false;
			conn=dao.getConn();
			String sql="SELECT * FROM workdb WHERE work_num=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, num);
			rs = pstmt.executeQuery();
			if(rs.next()){
				t=true;
			
				dto.setWork_sort(d[0]);
				dto.setTemp1(d[0]);
				dto.setClient_name(d[1]);
				dto.setWork_content(d[2]);
				
				if(d[3]!=null){
					dto.setWork_check_date(Date.valueOf(d[3]));
				}else{
					dto.setWork_check_date(null);
				}
					
				if(d[4]!=null){
					dto.setWork_end_date(Date.valueOf(d[4]));
				}else{
					dto.setWork_end_date(null);
				}
				
				dto.setWork_status(d[5]);
				dto.setWork_manager(d[6]);
			}
			
			//데이터가 있을 경우 삭제하고 새입력
			if(t){
				sql = "DELETE FROM Workdb WHERE Work_num=?";
				pstmt2 = conn.prepareStatement(sql);
				pstmt2.setInt(1, num);
				pstmt2.executeUpdate();
				
				sql = "INSERT INTO Workdb (Work_sort, client_name, Work_content, Work_check_date, ";
				sql+="Work_end_date, Work_status, Work_manager, Work_note, Work_finish_date) ";
				if(d[5].equals("end")){	
					sql+="values(?,?,?,?,?, ?,?,?,curdate())";
				}else{
					sql+="values(?,?,?,?,?, ?,?,?,?)";
				}
				pstmt3=conn.prepareStatement(sql);
				
				pstmt3.setString(1, dto.getWork_sort());
				pstmt3.setString(2, dto.getClient_name());
				pstmt3.setString(3, dto.getWork_content());
				pstmt3.setDate(4, dto.getWork_check_date());
				pstmt3.setDate(5, dto.getWork_end_date());
				pstmt3.setString(6, dto.getWork_status());
				pstmt3.setString(7, dto.getWork_manager());
				pstmt3.setString(8, dto.getTemp1());
				if(!d[5].equals("end")){
					pstmt3.setDate(9, null);
				}
				pstmt3.executeUpdate();
			}
		}catch (Exception e){
			System.out.println("IdcBusinessModifyCmd - setUpdate 에서의 exception");
			e.printStackTrace();
		}finally{
			try{
				if(conn!=null) conn.close();
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
				if(pstmt2!=null)pstmt2.close();
				if(pstmt3!=null)pstmt3.close();
			}
			catch (SQLException e){
				System.out.println("IdcBusinessModifyCmd - setUpdate 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
