package jp.yoyoyoyo.webform.action;

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

import jp.yoyoyoyo.webform.dto.RequestDAO;
import jp.yoyoyoyo.webform.form.ConfirmForm;
import jp.yoyoyoyo.webform.form.EntryForm;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ConfirmAction extends Action  {

	public ActionForward execute(ActionMapping mapping,
								ActionForm form,
								HttpServletRequest request,
								HttpServletResponse response){
	
		ConfirmForm confirmForm = (ConfirmForm)form;
		HttpSession session = request.getSession();
		EntryForm entryForm = (EntryForm)session.getAttribute("entryData");
		
		// セッションが見つからないときはメインテナンス画面へ
		if(entryForm == null){
			return mapping.findForward("maintenance");
		}
		
		// 戻るボタンを押したとき
		if(confirmForm.getBack() != null && !confirmForm.getBack().equals("")){
			// フォームに渡すデータをセット
			request.setAttribute("seiKanji", entryForm.getSeiKanji());
			request.setAttribute("meiKanji", entryForm.getMeiKanji());
			request.setAttribute("seiKana", entryForm.getSeiKana());
			request.setAttribute("meiKana", entryForm.getMeiKana());
			request.setAttribute("birthDate", entryForm.getBirthDate());
			request.setAttribute("gender", entryForm.getGender());
			request.setAttribute("zip1", entryForm.getZip1());	
			request.setAttribute("zip2", entryForm.getZip2());	
			request.setAttribute("address1", entryForm.getAddress1());	
			request.setAttribute("address2", entryForm.getAddress2());	
			request.setAttribute("address3", entryForm.getAddress3());	
			request.setAttribute("address4", entryForm.getAddress4());	
			request.setAttribute("tel1", entryForm.getTel1());	
			request.setAttribute("tel2", entryForm.getTel2());	
			request.setAttribute("tel3", entryForm.getTel3());
			request.setAttribute("mTel1", entryForm.getmTel1());
			request.setAttribute("mTel2", entryForm.getmTel2());
			request.setAttribute("mTel3", entryForm.getmTel3());
			request.setAttribute("email1", entryForm.getEmail1());
			request.setAttribute("email2", entryForm.getEmail2());
			request.setAttribute("occupation", entryForm.getOccupation());
			// 入力フォームを表示
			return mapping.findForward("entry");
		
			// 保存ボタンを押したとき
		}else if (confirmForm.getSave() != null && !confirmForm.getSave().equals("")){
			// データベース保存用オブジェクトの生成
			RequestDAO requestDAO = new RequestDAO();
			int ret = 0;
			try {
				// データベースに保存
				ret = requestDAO.executeSP(entryForm);
			} catch (Exception e){
				// エラーが発生したときはメインテナンス画面へ
				return mapping.findForward("maintenance");
			}
			// 登録番号の表示
			request.setAttribute("seqId", ret);
			
			// セッションのクリア
			session.invalidate();
			
			// 完了画面に遷移
			return mapping.findForward("thankyou");
		} else {
			// 初期表示オブジェクトの設定			
			request.setAttribute("entryData", entryForm);
			// 確認画面へ
			return mapping.findForward("confirm");
		}	
	}
}
