/**
 *
 */
package jp.agentec.sinaburocast.action.admin.enquete;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.log4j.Logger;
import org.apache.velocity.tools.generic.DateTool;
import org.seasar.framework.util.StringUtil;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ResponseUtil;

import jp.agentec.sinaburocast.action.AbstractAction;
import jp.agentec.sinaburocast.common.util.SinaburoUtil;
import jp.agentec.sinaburocast.form.admin.enquete.MemberCardReceiptSearchForm;
import jp.agentec.sinaburocast.service.MemberCardReceiptService;

/**
 * (磁気カードの)郵送申請検索
 * @author nakamura-k
 *
 */
public class MemberCardReceiptSearchAction extends AbstractAction {
	private final Logger logger = Logger.getLogger(getClass());

	@ActionForm
	public MemberCardReceiptSearchForm memberCardReceiptSearchForm;

	public MemberCardReceiptService memberCardReceiptService;

	/**
	 * 初期表示
	 */
	@Execute(validator = false,reset="resetIndex")
	public String index() {

		return "/admin/enquete/memberCardReceiptSearch.html";
	}

	/**
	 * カード郵送申請を検索する
	 */
	@Execute(validator = false,urlPattern="memberCardReceiptSearch/{pageNo}")
	public String memberCardReceiptSearch(){
		
		if(StringUtil.isNotBlank(memberCardReceiptSearchForm.fromDay) && !SinaburoUtil.checkDate(memberCardReceiptSearchForm.fromDay)){
			addErrorMessage("dateError", "errors.E033");
			addErrorMessage("dateError_1", "errors.E033_1");
			addErrorMessage("dateError_2", "errors.E033_2");
			return "/admin/enquete/memberCardReceiptSearch.html";
		}else if(StringUtil.isNotBlank(memberCardReceiptSearchForm.toDay) && !SinaburoUtil.checkDate(memberCardReceiptSearchForm.toDay)){
			addErrorMessage("dateError", "errors.E033");
			addErrorMessage("dateError_1", "errors.E033_1");
			addErrorMessage("dateError_2", "errors.E033_2");
			return "/admin/enquete/memberCardReceiptSearch.html";
		}else if(StringUtil.isNotBlank(memberCardReceiptSearchForm.fromDay) && StringUtil.isNotBlank(memberCardReceiptSearchForm.toDay)
				&& memberCardReceiptSearchForm.toDay.compareTo(memberCardReceiptSearchForm.fromDay) < 0
				){
			addErrorMessage("dateError", "errors.E020");
			return "/admin/enquete/memberCardReceiptSearch.html";
		}

		search();
		return "/admin/enquete/memberCardReceiptSearch.html";
	}
	
	/**
	 * Formの日付のチェックを行う。
	 */
	private void formCheck(){
		if(StringUtil.isNotBlank(memberCardReceiptSearchForm.fromDay) && !SinaburoUtil.checkDate(memberCardReceiptSearchForm.fromDay)){
			addErrorMessage("dateError", "errors.E033");
			addErrorMessage("dateError_1", "errors.E033_1");
			addErrorMessage("dateError_2", "errors.E033_2");
		}else if(StringUtil.isNotBlank(memberCardReceiptSearchForm.toDay) && !SinaburoUtil.checkDate(memberCardReceiptSearchForm.toDay)){
			addErrorMessage("dateError", "errors.E033");
			addErrorMessage("dateError_1", "errors.E033_1");
			addErrorMessage("dateError_2", "errors.E033_2");
		}else if(StringUtil.isNotBlank(memberCardReceiptSearchForm.fromDay) && StringUtil.isNotBlank(memberCardReceiptSearchForm.toDay)
				&& memberCardReceiptSearchForm.toDay.compareTo(memberCardReceiptSearchForm.fromDay) < 0
				){
			addErrorMessage("dateError", "errors.E020");
		}
	}

	private void search(){
		formCheck();
		if(hasErrors()){
			return ;
		}
		memberCardReceiptSearchForm.searchResultList = memberCardReceiptService.findAllByPaging(memberCardReceiptSearchForm);

	}

	/**
	 * カード郵送申請結果CSV出力
	 */
	@Execute(validator=false)
	public String reportDownload() throws UnsupportedEncodingException, Exception {

		String result = SinaburoUtil.toSJIS(memberCardReceiptService.getMemberCardReceiptForCsv(memberCardReceiptSearchForm));
		
		String dateStr = new DateTool().format("yyyyMMdd_HHmmss",DateTool.getSystemDate());
		ResponseUtil.download(URLEncoder.encode("カード郵送申請結果_"+dateStr+".csv", "UTF-8"),result.getBytes("Windows-31J") );

		return null;
	}
	
}