package idc.service.command;

import idc.model.IdcBillPaperDTO;
import idc.model.IdcCmd;
import idc.model.IdcDAO;

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

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

import utility.IdcTxtGetter;

//청구서 양식 보여주기
public class IdcServiceBillWriteFormCmd implements IdcCmd
{
	private IdcDAO dao;
	private String PATH=this.getClass().getResource("").getPath()+"../../../../../ETCFolder/openInfo.txt";
	private IdcTxtGetter tg;
	public IdcServiceBillWriteFormCmd() 
	{
		dao = new IdcDAO();
		tg = new IdcTxtGetter();
	}
	
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		String targetNum = request.getParameter("service_code");
		String strOut;
		IdcBillPaperDTO dto;
		dto = idcBillWriteForm(targetNum);
		strOut = tg.getText(PATH);
		
		request.setAttribute("IdcBillPaperDTO", dto);
		request.setAttribute("StrOut", strOut);
	}
	private IdcBillPaperDTO idcBillWriteForm(String targetNum)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		IdcBillPaperDTO dto = new IdcBillPaperDTO();
		try	
		{
			conn=dao.getConn();
			String sql="SELECT cli.client_code, cli.client_name, cli.Client_Phone1, cli.Client_EMail1, sev.client_code, sev.service_code, sev.service_note, ";
			sql+="sev.service_line, DAYOFMONTH(sev.service_payment_date) as a, sev.service_amount, sev.service_ip,sev.service_cs, sev.service_os, sev.service_note, ";
			sql+="sev.service_admin, sev.service_pass ";
			sql+="FROM clientdb AS cli, servicedb AS sev ";
			sql+="WHERE sev.service_code=? AND cli.client_code = sev.client_code";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, targetNum);
			
			rs = pstmt.executeQuery();
			
			if(rs.next())
			{
				dto.setClient_code(rs.getString("cli.client_code"));
				dto.setClient_email1(rs.getString("cli.client_email1"));
				dto.setClient_name(rs.getString("cli.client_name"));
				dto.setClient_phone1(rs.getString("cli.client_phone1"));
				dto.setService_admin(rs.getString("sev.service_admin"));
				dto.setService_amount(rs.getInt("sev.service_amount"));
				dto.setService_code(rs.getString("sev.service_code"));
				dto.setService_line(rs.getString("sev.service_line"));
				dto.setService_mainip(rs.getString("sev.service_ip"));
				dto.setService_note(rs.getString("sev.service_note"));
				dto.setService_os(rs.getString("sev.service_os"));
				dto.setService_pass(rs.getString("sev.service_pass"));
				dto.setTemp(rs.getString("a"));
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcBillWriteFormCmd 에서의 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("IdcBillWriteFormCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return dto;
	}
}