package cokr.xit.fims.crdn.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.servlet.ModelAndView;

import cokr.xit.base.code.CommonCode;
import cokr.xit.base.web.ApplicationController;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.crdn.dao.CrdnInfoMapper;
import cokr.xit.fims.crdn.service.CrdnService;
import cokr.xit.fims.crdn.service.CrdnStngService;
import cokr.xit.foundation.data.DataObject;

/**단속자료검사 서비스 웹 컨트롤러.<br />
 * {웹 컨텍스트}/crdn/crdn02 로 접근할 수 있다.
 * @author leebj
 */
public class Crdn02Controller extends ApplicationController {

	public static final String CLASS_URL = "/crdn/crdn02";

	public class METHOD_URL {
		public static final String
		crackdownDataInspectionMain = "/010/main.do",
		getInspectionDataList = "/020/list.do",
		getOverTimeInfo = "/050/info.do",
		updateOverTimeInfo = "/050/update.do"
		;
	}

	@Resource(name="crdnService")
	private CrdnService crdnService;

	@Resource(name="crdnStngService")
	private CrdnStngService crdnStngService;

	@Resource(name="crdnInfoMapper")
	private CrdnInfoMapper crdnInfoMapper;

	/** 단속 자료 검사 메인화면을 연다.
	 * @return fims/crdn/crdn02010-main
	 */
	public ModelAndView crackdownDataInspectionMain() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/crdn/crdn02010-main");
		mav.addObject("pageName", "crdn02010-main");
		mav.addObject("module", "type='module'");

		Map<String, List<CommonCode>> commonCodes = getCodesOf("FIM002", "FIM003", "FIM010", "FIM026","FIM054");
		mav.addObject("FIM002List", commonCodes.get("FIM002"));
		mav.addObject("FIM003List", commonCodes.get("FIM003"));
		mav.addObject("FIM010List", commonCodes.get("FIM010"));
		mav.addObject("FIM054List", commonCodes.get("FIM054"));
		addCodes(commonCodes, mav, "FIM002", "FIM003", "FIM010", "FIM026");

		return mav;
	}

	/**검사자료 목록을 조회한다.<br />
	 * {@link CrdnService#getCrackdownList(CrdnQuery)} 참고
	 * @param query 검사자료 목록 조회 조건
	 * @return jsonView
	 */
	public ModelAndView getInspectionDataList(CrdnQuery query) {
		switch(query.getInspectionDataType()) {
			case "photo" -> query.setGridType("photo");
			case "sameVehicleMain" -> query.setGridType("sameVehicleMain");
			case "sameVehicleSub" -> query.setGridType("sameVehicleSub");
		}
		setFetchSize(query);
		return setCollectionInfo(new ModelAndView("jsonView"), crdnService.getCrackdownList(query),"");
	}

	/**2시간 초과처리 팝업을 호출한다.<br />
	 * @param crdnId 단속 ID
	 * @return crdn02050-info
	 */
	public ModelAndView getOverTimeInfo(String openerPageName, String savedCallbackFuncName, String crdnId) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/crdn/crdn02050-info");
		mav.addObject("pageName","crdn02050-info");

		DataObject crdnInfo = crdnInfoMapper.selectCrdnInfo(crdnId);

		mav.addObject("crdnInfo", toJson(crdnInfo));
		mav.addObject("openerPageName", openerPageName);
		mav.addObject("savedCallbackFuncName", savedCallbackFuncName);
		return mav;
	}

	/**단속자료의 2시간 초과여부를 변경하고, 금액을 수정한다. <br />
	 * @param chgAmtSe 금액 변경 구분, crdnId 단속 ID
	 * @return jsonView
	 * <pre><code> {
	 *     "saved": 수정되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	public ModelAndView updateOverTimeInfo(String chgAmtSe, String crdnId) {
		boolean saved = crdnService.updateOverTimeInfo(chgAmtSe, crdnId);
		return new ModelAndView("jsonView")
			.addObject("saved", saved);
	}
}
